extracting a column using search term


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extracting a column using search term
# 1  
Old 10-13-2011
Question extracting a column using search term

I am trying to select a column using a search term.

My input file looks like this (tab delimited):

Code:
ABC BJS FDG GHH DGH DFG GHF
95 456 5 266 87 4567 67
3 54 678 4567 45 6 36
232 55 3 5 6 8 34

Code:
cat filename | awk '{print $2}'

above code will give me the second column. However, what I want is to search for a term such as GHH, then extract that column.

Any help would be much appreciate it. Thank you in advance.
# 2  
Old 10-13-2011
Code:
awk -v str='GHH' 'FNR==1{for(i=1;i<=NF;i++) if($i==str) col=i; if(!col) {print "unknown column name"; exit};next} {print $col}' myFile

# 3  
Old 10-13-2011
Code:
awk -v str="GHH" 'NR==1{for (i=1;i<=NF;i++) if ($i==str) c=i} c{print $c}' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search term in nth field and replace kth column

Hi, I have a text file which looks like this a.txt A,12,Apple,Red B,33,Banana,Yellow C,66,Sky,Blue I need to search for a particular field(s) in particular column(s) and for that matching line need to replace the nth column. Sample scenario 1: Search for 66 in second field and Sky in... (5 Replies)
Discussion started by: wahi80
5 Replies

2. UNIX for Dummies Questions & Answers

Search word in 3rd column and move it to next column (4th)

Hi, I have a file with +/- 13000 lines and 4 column. I need to search the 3rd column for a word that begins with "SAP-" and move/skip it to the next column (4th). Because the 3rd column need to stay empty. Thanks in advance.:) 89653 36891 OTR-60 SAP-2 89653 36892 OTR-10 SAP-2... (2 Replies)
Discussion started by: AK47
2 Replies

3. Shell Programming and Scripting

awk based script to print the "mode(statistics term)" for each column in a data file

Hi All, Thanks all for the continued support so far. Today, I need to find the most occurring string/number(also called mode in statistics terminology) for each column in a data file (.csv type). For one column of data(1.txt) like below Sample 1 2 2 3 4 1 1 1 2 I can find the mode... (6 Replies)
Discussion started by: ks_reddy
6 Replies

4. Shell Programming and Scripting

Extracting column value from perl

Hello Kindly help me to find out the first column from first line of a flat file in perl I/P 9869912|20110830|00000000000013009|130|09|10/15/2010 12:36:22|W860944|N|00 9869912|20110830|00000000000013013|130|13|10/15/2010 12:36:22|W860944|N|00... (5 Replies)
Discussion started by: Pratik4891
5 Replies

5. Shell Programming and Scripting

Search term highlighting using "less"

I'm using less to find terms in a 6 gb text file in OS X in the terminal. When I first search for the patter, it finds it, scrolls the document to the correct location, and highlights it. But when I search again, the document scrolls to a new further location (I'm assuming it's found the pattern... (2 Replies)
Discussion started by: garethson
2 Replies

6. UNIX for Advanced & Expert Users

extracting/copy a column into a new column

Hello, Anybody out there knows how to copy a column data into a blank column using unix command? Thanks (1 Reply)
Discussion started by: folashandy
1 Replies

7. Shell Programming and Scripting

Extracting a column using AWK

Hi, I've a text file like ABC,,100 A,100,200 In the above example, I have 3 columns. I want to extract the second column. I'm expecting a value like 100 i.e first record will not have any value but still it has to give me null value. second record should give 100. Can anybody... (2 Replies)
Discussion started by: ronald_brayan
2 Replies

8. Shell Programming and Scripting

Search term and output term in desired field

Hi All, I have an input_file below and i would like to use Perl to search for the term "aaa" and output the 3rd term in the same row as "aaa".For Example, i want to search for the term "ddd" and would want the code to ouput the 3rd term in the same row which is "fff". Can somebody help ? ... (28 Replies)
Discussion started by: Raynon
28 Replies

9. Shell Programming and Scripting

Extracting one column from a ps -ef command

Hi, I want to extract one value/column from a ps -ef command. Here's an example of the output: mqm 14552 1 0 15:48:43 - 0:00 amqpcsea SWNETTQ1 mqm 57082 1 0 15:48:42 - 0:00 amqpcsea SWNETDQ1 mqm 88104 1 0 15:26:37 - 0:00 amqpcsea SWNETEQ1... (6 Replies)
Discussion started by: m223464
6 Replies

10. Programming

Create a Term & Run chars on this Term

hi floks ! i'd like to know how can i transmete a character or a string from my source code to a term and make it interpret or un by the shell wich is running in my term. I'd like to create a Term from my code (and get its file descriptor) and then transmete each char typed on the keyboard to... (1 Reply)
Discussion started by: the_tical
1 Replies
Login or Register to Ask a Question