The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Changing one column of delimited file column to fixed width column manneni prakash Shell Programming and Scripting 5 06-22-2009 05:27 AM
Replacing column with column of another file manneni prakash UNIX for Dummies Questions & Answers 1 06-24-2008 11:20 PM
How to check Null values in a file column by column if columns are Not NULLs Mandab Shell Programming and Scripting 7 03-15-2008 09:57 AM
(cont) Retrieve line from a file based on a value in specific column efernandes UNIX for Dummies Questions & Answers 0 01-27-2007 01:00 PM
Retrieve line from a file based on a value in specific column efernandes UNIX for Dummies Questions & Answers 1 01-27-2007 11:04 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-12-2008
casphar casphar is offline
Registered User
  
 

Join Date: Dec 2005
Location: Vancouver, Canada
Posts: 13
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 (permalink)  
Old 11-12-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink 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 (permalink)  
Old 11-12-2008
juheimbu juheimbu is offline
Registered User
  
 

Join Date: Oct 2008
Location: St. Louis, MO
Posts: 13
Alternatively:

Code:
cat file66.tsv|awk '{print $2}'
  #4 (permalink)  
Old 11-12-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink 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 (permalink)  
Old 11-12-2008
casphar casphar is offline
Registered User
  
 

Join Date: Dec 2005
Location: Vancouver, Canada
Posts: 13
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 View Post
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
  #6 (permalink)  
Old 11-12-2008
casphar casphar is offline
Registered User
  
 

Join Date: Dec 2005
Location: Vancouver, Canada
Posts: 13
Quote:
Originally Posted by juheimbu View Post
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.
  #7 (permalink)  
Old 11-12-2008
juheimbu juheimbu is offline
Registered User
  
 

Join Date: Oct 2008
Location: St. Louis, MO
Posts: 13
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..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:25 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0