Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash shell how to cut the first column out of a file

Tags:

bash

shell

cut

so I have a file named 'file' that contains these characters

a 1 z
b 2 y
c 3 x

how can I cut the first column and put it in it's own file?

I know how to do the rest using the space as a delimiter like this:

cut -f1 -d ' ' file > filecolumn1

but I'm not sure how to cut just the first column since there isn't any character in the front that I can use as a delimiter.

like image 411
PFKrang Avatar asked Sep 13 '25 22:09

PFKrang


2 Answers

The delimiter doesn't have to be before the column, it's between the columns. So use the same delimiter, and specify field 1.

cut -f1 -d ' ' file > filecolumn1
like image 163
Barmar Avatar answered Sep 15 '25 10:09

Barmar


Barmar's got a good option. Another option is awk:

awk '{print $1}' file > output.txt

If you have delimiter, you could use -F switch and provide a delimiter. For example, if your data was like this:

a,1,2
b,2,3
c,3,4

you can use awk's -F switch in this manner:

awk -F',' '{print $1}' file > output.txt
like image 37
zedfoxus Avatar answered Sep 15 '25 11:09

zedfoxus