Pick out columns according to list of column names


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Pick out columns according to list of column names
# 1  
Old 01-10-2013
Pick out columns according to list of column names

Hi Everyone:

I'm new to linux and I was wondering what the best way to approach the following problem was.

I have 2 files:

File A:
Code:
ID123
ID234
ID456

File B:
Code:
ID123 ID234 ID345 ID456
A B C D
E F G H 
I J K L

Based on the list of IDs in File A, I want to output only the corresponding columns from file B.

Output:
Code:
ID123 ID234 ID456
A B D
E F H
I J L

Any insight would greatly be appreciated!
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 01-10-2013 at 04:31 PM.. Reason: code tags, please!
# 2  
Old 01-10-2013
how about searching the Forums first - searching for 'column header'...
Starting with thread for example...
# 3  
Old 01-10-2013
try:
Code:
awk '
NR==FNR {col[$1]=$1; next}                   # fill column array with values from File_A
FNR==1  {for (i=1; i<=NF; i++) {             # for record 1 of File_B search columns for
  if (col[$i]) out_col[i]=i}                 # matching columns array values, fill out columns array
}
{for (i=1; i<=NF; i++) {                     # loop thru columns for each record
  if (out_col[i]) printf("%s%s", $i, OFS) }; # print columns numbers that match out columns array
  print ""                                   # print return character for record
}
' File_A File_B

This User Gave Thanks to rdrtx1 For This Post:
# 4  
Old 01-11-2013
Thank you!!!

That seems to do the trick Smilie Thanks a lot!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to pick max values of the columns

Hi, I have sar disk reports like below sample: 01:01:00 hdisk24 0 0.0 0 0 0.0 0.0 hdisk15 0 0.0 0 3 0.0 5.5 hdisk20 0 0.0 2 1 0.0 1.9 hdisk19 1 ... (3 Replies)
Discussion started by: reddyr
3 Replies

2. UNIX for Dummies Questions & Answers

Creating a two column list of date pairs form a single column list

Hi all, looking for some help here. I'm what you'd call a dirty programmer. my shell scripts might be ugly, but they (usually) function... Say I have a single column text file with a list of dates (yyyymmdd) that represent the elevation of a point on that date (I work with land subsidence, so... (2 Replies)
Discussion started by: jbrandt1979
2 Replies

3. Shell Programming and Scripting

Pick the column value based on another column from .csv file

My scenario is that I need to pick value from third column based on fourth column value, if fourth column value is 1 then first value of third column.Third column (2|3|4|6|1) values are cancatenated. Main imp point, in my .csv file, third column is having price value with comma (1,20,300), it has... (2 Replies)
Discussion started by: Ganesh L
2 Replies

4. Shell Programming and Scripting

Pick the column value including comma from csv file using awk

Source 1 column1 column2 column 3 column4 1,ganesh,1,000,1 222,ram,2,000,5 222,ram,50,000,5 33,raju,5,000,7 33,raju,5,000,7 33,raju,5,000,8 33,raju,5,000,4 33,raju,5,000,1 In my .csv file, third column is having price value with comma (20,300), it has to be considered 1,000 as... (1 Reply)
Discussion started by: Ganesh L
1 Replies

5. Shell Programming and Scripting

Pick the column value based on another column using awk or CUT

My scenario is that I need to pick value from third column based on fourth column value, if fourth column value is 1 then first value of third column.Third column (2|3|4|6|1) values are cancatenated. Please someone help me to resolve this issue. Source column1 column2 column3 column4... (2 Replies)
Discussion started by: Ganesh L
2 Replies

6. Shell Programming and Scripting

Pick value from row after a column value

Hi, my input abc def "."; ghi abc kgk mhi def "="; klm nop abc kgg def "j"; klm mno pwd rst I would like to get the content in the inverted commas after def. So, my output would be . = j How do I do it? Please make a note that my input field separator is space and not tab. ... (10 Replies)
Discussion started by: jacobs.smith
10 Replies

7. Shell Programming and Scripting

Transpose field names from column headers to values in one column

Hi All, I'm looking for a script which can transpose field names from column headers to values in one column. for example, the input is: IDa;IDb;IDc;PARAM1;PARAM2;PARAM3; a;b;c;p1val;p2val;p3val; d;e;f;p4val;p5val;p6val; g;h;i;p7val;p8val;p9val; into the output like this: ... (6 Replies)
Discussion started by: popesk
6 Replies

8. Shell Programming and Scripting

pick columns

I want to be able pick columns from 2 files with similar reference data: File 1 Last NameFirst NameSalaryCityDunnJohn $42,000.00 ChicagoGrantSuzy $95,000.00 GaryLoweMike $80,000.00 MilwaukeeGrantMike $59,000.00 JolietLoweKaren $48,000.00 South BendFile 2 TitleFirst NameLast... (2 Replies)
Discussion started by: sigh2010
2 Replies

9. Shell Programming and Scripting

rearrange the column names with comma as column delimiter

Hi, I am new to shell scripting, i have requirement can any one help me out in this regrads, in directory i have file like invoice1.txt, invoice2.txt in each file i have fixed number of columns, 62 in number but they are randomly arranged.like for first file invoice1.txt can have columns... (5 Replies)
Discussion started by: madhav62
5 Replies

10. Shell Programming and Scripting

Change names in a column based on the symbols in another column

If the 4th column has - sign then the names in 3rd column has to change to some user defined names (as shown in output). Thanx input1 1 a aaaaa + 2 b bbbbb + 3 c ccccc + 4 d ddddd + 5 e eeeee + 6 f xxxxx + 8 h hhhhh +... (8 Replies)
Discussion started by: repinementer
8 Replies
Login or Register to Ask a Question