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 > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to extract only first column from the file selamba_warrior Shell Programming and Scripting 11 05-21-2008 02:52 AM
How to extract a column from two different files in AWK? solracq Shell Programming and Scripting 7 04-29-2008 06:21 AM
column extract help cvm Shell Programming and Scripting 1 04-24-2008 04:19 PM
column based search user_007 Shell Programming and Scripting 8 07-01-2007 05:52 AM
filter based on column value rraajjiibb Shell Programming and Scripting 2 05-25-2004 09:09 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-29-2007
t27 t27 is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 2
extract column based on name

I need to extract a column from a tab delimited text file based on the string in the first row. (i.e. extract the column labeled "reaction_time"). I know that this can be done with awk by determining the column #, but I want to do it based on a search for the string b/c the column number changes for different subjects. Thanks for your help.
  #2 (permalink)  
Old 08-29-2007
namishtiwari namishtiwari is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2007
Location: Bangalore
Posts: 377
can you provide one sample how your input file looks like that will be easy for us.

Thanks
  #3 (permalink)  
Old 08-29-2007
robotronic's Avatar
robotronic robotronic is offline Forum Advisor  
Can I play with madness?
  
 

Join Date: Apr 2002
Location: Italy
Posts: 370
Code:
input.txt (tab delimited fields)
--------------------------------
Name	Surname	Age
AAA	aaa	20
BBB	bbb	25
CCC	ccc	30



nawk -v col="Surname" -F'\t' '
   {
      if (NR == 1) {
         for (i=1; i<=NF; i++) {
            if ($i == col) { colnum=i; break; }
         }
         if (! colnum) {
            print("Specified column does not exist!");
            exit;
         }
      } else {
         print($colnum);
      }
   }
' input.txt



Output
------
aaa
bbb
ccc
It's tested on Solaris, so you may substitute "nawk" with "gawk" or "awk" or whatever depending on your OS.
  #4 (permalink)  
Old 08-29-2007
t27 t27 is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 2
Perfect. Many thanks.
Sponsored Links
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 04:32 AM.


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