sed/awk : need just the first column in a multi-column file

sed/awk : need just the first column in a multi-column file

Post by bob ange » Tue, 09 Feb 1993 04:46:45




Quote:

>Hello.

>Could someone please tell me
>how I could use sed or awk
>to strip a file of all but
>it's first column?

>e.g.
>           i want a dog                  i
>           don't want a cat        ===>  don't
>           scratching it's claws         scratching
>           all over my back              all

assuming you will have a space as the delimiter for your data ...
here it is in AWK.

AWK script would be ...
{ print $1 }

and then you would execute this on the command-line ...

AWK -f [script file] [data file] > output.file (if you want to save the
output)

note:  there are other elegent ways of doing the above, but this
        example is probably the easiest way to think about it.

-Bob-

Bob Angell                               | Data Integration (multi-platform)
Principal, Management Systems Engineer   | Health Systems Engineering
Applied Information & Management Systems | Database design/development
1238 Fenway Avenue - SLC, UT 84102-3212  | Simulation/Modeling/Neural Nets

     [Standard Disclaimer: Speaking for the University of Utah, NOT!]

 
 
 

sed/awk : need just the first column in a multi-column file

Post by Jim Dav » Tue, 09 Feb 1993 12:00:51



Quote:>Could someone please tell me
>how I could use sed or awk
>to strip a file of all but
>it's first column?

awk '{print $1}' file

or

sed 's/\([^     ][^     ]*\)\(.*\)/\1/' file

(where [^     ] contains three characters inside the brackets:  
^, blank, and tab).

Both will preserve empty lines; you can delete empty lines as well with

awk 'NF > 0 {print $1}' file

and

sed '/^$/d
     s/\([^     ][^     ]*\)\(.*\)/\1/' file

respectively.
--
Jim Davis             | "Put the underwears in the bag and nobody gets hurt!"


 
 
 

1. How to change one part on a multi-column, multi-line

I have this ascii file with the following format: This is just a tiny
part of a huge file.

LINE:    0     CDP:   288     X:            0.00     Y:            0.00
VELRMS         4  2203    172  2360    374  2594    513  2788    750
3096  1056  3416
VELRMS   1258  3556  1634  3666  4971  4459  5004  4466
LINE:    0     CDP:   368     X:            0.00     Y:            0.00
VELRMS       11  2211    220  2348    339  2451    506  2647    799
3042    924  3250
VELRMS   1182  3683  1349  3874  1808  4074  1969  4148  2825  4332
4985  4656
VELRMS   5004  4658
LINE:    0     CDP:   448     X:            0.00     Y:            0.00
VELRMS       25  2229    325  2527    541  2773    694  2954    882
3180  1077  3416
VELRMS   1335  3677  1690  3893  2575  4139  4985  4485  5004  4487
LINE:    0     CDP:   528     X:            0.00     Y:            0.00
VELRMS       18  2218    283  2416    520  2640    659  2811    757
2957    966  3269
VELRMS   1258  3568  1815  3870  2763  4325  3578  4531  4971  4739
5004  4741

I would like to multiply the number right after CDP with a constant, but
not change anything else. Is there an easy way to do it?

Thanks,
Rosana

2. NT Disk Administrator destroys Linux drives???

3. Newbie: adding and averaging columns, based on feature in column 2 (awk?)

4. NVidia driver

5. Sorting By Second Column With Unique First Column

6. root/boot disk? (newbie)

7. AIX Script to Summarize By First Column By Adding Values in Numerical Columns

8. 3D Blaster PCI and X-windows, what happens?

9. Multi-column output?

10. multi-column output

11. KDE 2.2beta1 crashes with multi-column fancy menus

12. arrange columns and find average of each column

13. setting 100-column & 132-column modes