Thursday, February 3, 2022

[SOLVED] How to extract the first column from a tsv file?

Issue

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
....

Solution

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:



Answered By - codeforester
Answer Checked By - Terry (WPSolving Volunteer)