Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract the first column from a tsv file?

Tags:

sed

awk

cut

I have a file containing some data and I want to use only the first column as a stdin for my script, but I'm having trouble extracting it. I tried using this

awk -F"\t" '{print $1}' inputs.tsv

but it only shows the first letter of the first column. I tried some other things but it either shows the entire file or just the first letter of the first column.

My file looks something like this:

Harry_Potter    1
Lord_of_the_rings    10
Shameless    23
....
like image 921
Saeko Avatar asked Mar 17 '18 19:03

Saeko


People also ask

How do I awk my first column?

awk to print the first column. The first column of any file can be printed by using $1 variable in awk. But if the value of the first column contains multiple words then only the first word of the first column prints. By using a specific delimiter, the first column can be printed properly.

How do I remove the first column from a Unix file?

Use grep/awk to remove part of column.


1 Answers

You can use cut which is available on all Unix and Linux systems:

cut -f1 inputs.tsv

You don't need to specify the -d option because tab is the default delimiter. From man cut:

 -d delim
         Use delim as the field delimiter character instead of the tab character.

As Benjamin has rightly stated, your awk command is indeed correct. Shell passes literal \t as the argument and awk does interpret it as a tab, while other commands like cut may not.

Not sure why you are getting just the first character as the output.


You may want to take a look at this post:

  • Difference between single and double quotes in Bash
like image 171
codeforester Avatar answered Sep 19 '22 09:09

codeforester