Grep from multiple files by column name


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep from multiple files by column name
# 1  
Old 03-21-2013
Code Grep from multiple files by column name

I have 40 files with varying number of columns
They however do have identical column names for the overlapping columns
I would like to grep some info from these specific colums

e.g
file1

id beta se direction N
2 .5 .01 + 1000
5 -.6 .02 - 2000


file2

id beta se N
2 .6 .015 3000
5 .3 .01 2000

So as you can see file 1 has a column with direction of effect while file2 hasn't

I would like to grep all beta values for ID2 from the files and put them in one file

Any suggestions?
Thanks a lot in advance

Last edited by MFAB; 03-21-2013 at 09:39 AM..
# 2  
Old 03-21-2013
Have a look at the paste command. That may have what you need.


Robin
# 3  
Old 03-21-2013
Thanks,
I've tried the paste command, but I do not know the exact column numbers, so need to specify the column name somehow
# 4  
Old 03-21-2013
If you just want the first two columns of all the files, you could use the cut command.

Code:
grep "^2 " file1 file2|cut -f-2 -d " "

The ^ in the grep signifies the start of line.



Is this better?

Robin
# 5  
Old 03-21-2013
Thanks,

the problem with the files is that the column I'm looking for is sometimes the 4th column and sometimes the 6th or 2nd. So there is not regularity in the files
# 6  
Old 03-21-2013
You might need a two step process in a loop then. Are the various files each the same format each time? If so, then you can build a case statement to collect it all together into one:-
Code:
#!/bin/ksh

for file in <list of files>
do
   case $file in
      file1)   cols="-2"    ;;
      file2)   cols="1,4"  ;;
      file3)   cols="3,6"   ;;
      *)       cols="1-"    ;;
   esac

   cut -f$cols -d " " $file
done

Obviously adjust the file names and columns required to suit your needs.

The wild-card option at the end of the case statement just completes the syntax to that cut will get fields from the first to the end, i.e. everything.

You can add a | grep after the done statement to select the rows you want, in this example above you would end up with:-
Code:
::
::
::
   esac

   cut -f$cols -d " " $file
done | grep "^2 "


Does this help?


Robin
# 7  
Old 03-21-2013
Pls use code tags as required by forum rules.

And, I'm pretty sure you don't insist on using grep.

If you want the values identified by column header, and then by one column that contains a search value, try
Code:
awk     'FNR==1         {for (n=1; n<=NF; n++) {if ($n==ID) IDCOL=n; if ($n==RES) RESCOL=n}; next}
         $IDCOL==SRCH   {print $RESCOL}
        '  RES=beta  ID=id  SRCH=2   file1 file2
.5
.6

based on your sample files.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to copy a column of multiple files and paste into new excel file (next to column)?

I have data of an excel files as given below, file1 org1_1 1 1 2.5 100 org1_2 1 2 5.5 98 org1_3 1 3 7.2 88 file2 org2_1 1 1 2.5 100 org2_2 1 2 5.5 56 org2_3 1 3 7.2 70 I have multiple excel files as above shown. I have to copy column 1, column 4 and paste into a new excel file as... (26 Replies)
Discussion started by: dineshkumarsrk
26 Replies

2. Shell Programming and Scripting

Grep strings on multiple files and output to multiple files

Hi All, I want to use egrep on multiple files and the results should be output to multiple files. I am using the below code in my shell script(working in Ksh shell). However with this code I am not attaining the desired results. #!/bin/ksh ( a="/path/file1" b="path/file2" for file in... (4 Replies)
Discussion started by: am24
4 Replies

3. UNIX for Dummies Questions & Answers

Stack data from multiple files into one, with variable column files

Hello Gurus, Im new to scripting. Got struck with a file merge issue in Unix. Was looking for some direction and stumbled upon this site. I saw many great posts and replies but couldnt find a solution to my issue. Greatly appreciate any help.. I have three csv files -> Apex_10_Latest.csv,... (1 Reply)
Discussion started by: wamshi
1 Replies

4. Shell Programming and Scripting

Select multiple column from multiple files

Hi Friends, $ cat test1.txt emeka:1438 shelley:1439 dmeyer:1440 kurtarn:1441 abdul:1442 $ cat test2.txt 1:a 2:b 3:c 4:d $ cat test3.txt cat:dog:bat man:hot:cold (5 Replies)
Discussion started by: Jewel
5 Replies

5. Shell Programming and Scripting

Grep multiple strings in multiple files

Hi, every one! I have a file with multiple strings. file1 ATQRGNE ASQGVKFTE ASSQYRDRGGLET SPEQGARSDE ASSRDFTDT ASSYSGGYE ASSYTRLWNTGE ASQGHNTD PSLGGGNQPQH SLDRDSYNEQF I want to grep each string in hundreds of files in the same directory, further, I want to find out the string... (7 Replies)
Discussion started by: xshang
7 Replies

6. Shell Programming and Scripting

Column extraction from multiple files to multiple files

I have roughly ~30 .txt files in a directory which all have unique names. These files all contain text arranged in columns separated by whitespace (example file: [#YY MM DD hh mm WDIR WSPD GST WVHT DPD APD MWD PRES ATMP WTMP DEWP VIS TIDE #yr mo dy hr mn degT m/s m/s m sec ... (5 Replies)
Discussion started by: aozgaa
5 Replies

7. Shell Programming and Scripting

Average of a column in multiple files

I have several sequential files with name stat.1000, stat.1001....to stat.1020 with a format like this 0.01 1 3822 4.97379915032e-14 4.96982253992e-09 0 0.01 3822 1 4.97379915032e-14 4.96982253992e-09 0 0.01 2 502 0.00993165137406 993.165137406 0 0.01 502 2 0.00993165137406 993.165137406 0... (6 Replies)
Discussion started by: kayak
6 Replies

8. UNIX Desktop Questions & Answers

How do you [e]grep for multiple values within multiple files?

Hi I'm sure there's a way to do this, but I ran out of caffeine/talent before getting the answer in a long winded alternate way (don't ask ;) ) The task I was trying to do was scan a directory of files and show only files that contained 3 values: I940 5433309 2181 I tried many variations... (4 Replies)
Discussion started by: callumw
4 Replies

9. Shell Programming and Scripting

combine multiple files by column into one files already sorted!

I have multiple files; each file contains a certain data in a column view simply i want to combine all those files into one file in columns example file1: a b c d file 2: 1 2 3 4 file 3: G (4 Replies)
Discussion started by: ahmedamro
4 Replies

10. UNIX for Dummies Questions & Answers

Grep multiple strings in multiple files using single command

Hi, I will use below command for grep single string ("osuser" is search string) ex: find . -type f | xarg grep -il osuser but i have one more string "v$session" here i want to grep in which file these two strings are present. any help is appreciated, Thanks in advance. Gagan (2 Replies)
Discussion started by: gagan4599
2 Replies
Login or Register to Ask a Question