4 column tsv file, output 1 specific column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting 4 column tsv file, output 1 specific column
# 1  
Old 11-12-2008
4 column tsv file, output 1 specific column

Hello all

siteexplorer.search.yahoo.com can output results in tsv format, when opened in excel I get 4 columns.

I would like to wget that file, which I can do. I would then like to pull the 2nd column and output it only.

I've searched around and found a few bits and pieces but nothing I've been able to get working as yet.

If it matters I'm doing this on a Centos 5.1 machine.

Any help would be great.

Thanks
# 2  
Old 11-12-2008
Hammer & Screwdriver tsv = tab separated values ?

Perhaps the following will assist...
(I changed the tab to a ~ so I can more easily cut on fields.)

Code:
> cat file66.tsv
13      Oct     Joe     smile
18      Jan     Linda   laugh
11      Nov     Vets    march

> cat file66.tsv | tr "\t" "~" | cut -d"~" -f2
Oct
Jan
Nov

# 3  
Old 11-12-2008
Alternatively:

Code:
cat file66.tsv|awk '{print $2}'

# 4  
Old 11-12-2008
Hammer & Screwdriver awk works by default with either tab or space delimiter

So, if your input file was slightly different, you might not get your desired results with a 'plain' awk. See below where there is a single space between the month and a number:

Code:
> cat file66c.tsv
13      Oct 1   Joe     smile
18      Jan 2   Linda   laugh
11      Nov 3   Vets    march

> cat file66c.tsv | tr "\t" "~" | cut -d"~" -f2
Oct 1
Jan 2
Nov 3

> cat file66c.tsv | awk '{print $2}'
Oct
Jan
Nov

# 5  
Old 11-12-2008
Quote:
Originally Posted by juheimbu
Alternatively:

Code:
cat file66.tsv|awk '{print $2}'

Using awk in that way isn't going to work for the task, it's going to assume space is the seperator and if column 1 has more than 1 word etc it will get the wrong information.
# 6  
Old 11-12-2008
You're right. I missed the extra space. Nice catch.

It would work this way, though:

Code:
cat file66c.tsv|awk -F"\t" '{print $2}'


Last edited by juheimbu; 11-12-2008 at 04:14 PM..
# 7  
Old 11-12-2008
Thanks, the tr and cut command seems to be doing the trick. I did notice a few results missed the correct entry but this may be due to bad formating in the tsv file?

thanks

Quote:
Originally Posted by joeyg
So, if your input file was slightly different, you might not get your desired results with a 'plain' awk. See below where there is a single space between the month and a number:

Code:
> cat file66c.tsv
13      Oct 1   Joe     smile
18      Jan 2   Linda   laugh
11      Nov 3   Vets    march

> cat file66c.tsv | tr "\t" "~" | cut -d"~" -f2
Oct 1
Jan 2
Nov 3

> cat file66c.tsv | awk '{print $2}'
Oct
Jan
Nov

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Overwrite specific column in xml file with the specific column from adjacent line

I have an xml file dumped from rrd file, that I want to "patch" so the xml file doesn't contain any blank hole in the resulting graph of the rrd file. Here is the file. <!-- 2015-10-12 14:00:00 WIB / 1444633200 --> <row><v> 4.0419731265e+07 </v><v> 4.5045912770e+06... (2 Replies)
Discussion started by: rk4k
2 Replies

2. Shell Programming and Scripting

Counting specific column and add result in output

Hi all, I have a quick question: I have a 4 column tab-separated file. I want to count the number of times each unique value in column 2 appears and add that number in a 5th column. I have the following input file: waterline-n below-sheath-v 14.8097 A dock-n below-sheath-v ... (4 Replies)
Discussion started by: owwow14
4 Replies

3. Shell Programming and Scripting

Converting Single Column into Multiple rows, but with strings to specific tab column

Dear fellows, I need your help. I'm trying to write a script to convert a single column into multiple rows. But it need to recognize the beginning of the string and set it to its specific Column number. Each Line (loop) begins with digit (RANGE). At this moment it's kind of working, but it... (6 Replies)
Discussion started by: AK47
6 Replies

4. Shell Programming and Scripting

Split each column in TSV file to be new line?

My TSV looks like: Hello my name is John \t Hello world \t Have a good day! \t See you later! Is there a simple bash script that splits the tsv on tab to: Hello my name is John Hello world Have a good day! See you later! I'm really stuck, would appreciate any help! (5 Replies)
Discussion started by: pxalpine
5 Replies

5. Shell Programming and Scripting

Awk: Need help replacing a specific column in a file by part of a column in another file

Hi, I have two input files as File1 : ABC:client1:project1 XYZ:client2-aa:project2 DEF:client4:proj File2 : client1:W-170:xx client2-aa:WT-04:yy client4:L-005A:zz Also, array of valid values can be hardcoded like Output : ABC:W:project1 XYZ:WT:project2 (1 Reply)
Discussion started by: aa2601
1 Replies

6. Shell Programming and Scripting

Replace column that matches specific pattern, with column data from another file

Can anyone please help with this? I have 2 files as given below. If 2nd column of file1 has pattern foo1@a, find the matching 1st column in file2 & replace 2nd column of file1 with file2's value. file1 abc_1 foo1@a .... abc_1 soo2@a ... def_2 soo2@a .... def_2 foo1@a ........ (7 Replies)
Discussion started by: prashali
7 Replies

7. UNIX for Dummies Questions & Answers

Replacing a specific column of a text file with another column

Hi, I have a text file in the following format: Code: 13412 NA06985 0 0 2 46.6432798439 4 4 4 4 13412 NA06991 NA06993 NA06985 2 48.8478948517 4 4 2 4 13412 NA06993 0 0 1 45.8022601455 4 4 2 4 13401 NA06994 0 0 1 48.780669145 4 4 4 4 13401 NA07000 0 0 2 47.7312017846 2 4 4 4 ... (2 Replies)
Discussion started by: evelibertine
2 Replies

8. UNIX for Dummies Questions & Answers

Replacing a specific column of a text file with another column

I have a text file in the following format: 13412 NA06985 0 0 2 46.6432798439 4 4 4 4 13412 NA06991 NA06993 NA06985 2 48.8478948517 4 4 2 4 13412 NA06993 0 0 1 45.8022601455 4 4 2 4 13401 NA06994 0 0 1 48.780669145 4 4 4 4 13401 NA07000 0 0 2 47.7312017846 2 4 4 4 13402 NA07019... (3 Replies)
Discussion started by: evelibertine
3 Replies

9. Shell Programming and Scripting

Question about sort specific column and print other column at the same time !

Hi, This is my input file: ali 5 usa abc abu 4 uk bca alan 6 brazil bac pinky 10 utah sdc My desired output: pinky 10 utah sdc alan 6 brazil bac ali 5 usa abc abu 4 uk bca Based on the column two, I want to do the descending order and print out other related column at the... (3 Replies)
Discussion started by: patrick87
3 Replies
Login or Register to Ask a Question