Grepping a column from multiple file


 
Thread Tools Search this Thread
Top Forums Programming Grepping a column from multiple file
# 1  
Old 08-29-2012
Grepping a column from multiple file

I have 20 files that look pretty much like this:
Code:
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
0.01 4 33 0.00189645523539 189.645523539 0
0.01 33 4 0.00189645523539 189.645523539 0
0.01 4 548 0.00357382134942 357.382134942 0
0.01 548 4 0.00357382134942 357.382134942 0
0.01 4 1225 0.00088154447822 88.154447822 0
0.01 1225 4 0.00088154447822 88.154447822 0
0.01 4 868 0.00295939726649 295.939726649 0
0.01 868 4 0.00295939726649 295.939726649 0

I would like to search through the second column of each file and grep every line that has the value '4' in the second column. I want to then write all these lines in a single file. I have struggled with this for some time with no success.

Could somebody help with awk or grep or any thing that can do this??

Thanks!!!!
Moderator's Comments:
Mod Comment Please view this code tag video for how to use code tags when posting code and data.

Last edited by vbe; 08-29-2012 at 10:10 AM..
# 2  
Old 08-29-2012
Code:
awk '$2=="4"' file1 file2 file3...file20 > output

# 3  
Old 08-29-2012
Code:
 $ awk '$2 == 4' inputfile
0.01 4 33 0.00189645523539 189.645523539 0
0.01 4 548 0.00357382134942 357.382134942 0
0.01 4 1225 0.00088154447822 88.154447822 0
0.01 4 868 0.00295939726649 295.939726649 0

# 4  
Old 08-29-2012
Thanks a million! That was fast and works perfectly !!!

---------- Post updated at 10:17 AM ---------- Previous update was at 07:46 AM ----------

Now is it possible to use the command

Code:
awk '$2=="4"' file1 file2 file3...file20 > output

but specify a file range? My files are numbered serially say 'force.1000 force.1001 force.1002' and so on. Can I do an awk from force.100* to force.1020 for example?


Moderator's Comments:
Mod Comment You were already asked for in a mod note: Please use code tags next time for your code and data.

Last edited by zaxxon; 08-29-2012 at 12:25 PM.. Reason: code tags
# 5  
Old 08-29-2012
Code:
awk '$2=="4"' force.100[0-2][0-9]*

# 6  
Old 08-29-2012
Code:
for i in $(seq 1000 1020); do awk '$2==4' force.$i >> output.txt; done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grepping multiple XML tag results from XML file.

I want to write a one line script that outputs the result of multiple xml tags from a XML file. For example I have a XML file which has below XML tags in the file: <EMAIL>***</EMAIL> <CUSTOMER_ID>****</CUSTOMER_ID> <BRANDID>***</BRANDID> Now I want to grep the values of all these specified... (1 Reply)
Discussion started by: shubh752
1 Replies

2. 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

3. Shell Programming and Scripting

Grepping or awking multiple lines in a file - regex

data.txt: hellohellohello mellomello1mello tellotellotellotello bellobellowbellow vellow My attempts: egrep ".*mello1\n.*bellow" data.txt awk '/.*mello1.*\nbellow/' data.txt how can i search for patterns that are on different lines using simple egrep or awk? i only want the... (7 Replies)
Discussion started by: SkySmart
7 Replies

4. Shell Programming and Scripting

Grepping non-alpa-numerics from first column only

I have data in the following tab-separated format (consists of 200 columns all together, this is just a sampling) </s> 0.001701 0.002025 0.002264 0.001430 -0.001300 . -0.205240 0.177341 -0.426209 -0.661049 -0.048884 0.027032 the -0.159145 0.084377 0.056968 0.050934 0.160689 of -0.230698... (7 Replies)
Discussion started by: owwow14
7 Replies

5. UNIX for Dummies Questions & Answers

Grepping al values of a particular column in a file

Name Num_free Num_active Pct_act Max_Used Reuse_cnt Instance_Name --------------------------------- --------------- ----------- ------- ----------- ----------- ------------------------------ additional network memory 0 ... (2 Replies)
Discussion started by: Rajeshneemkar
2 Replies

6. Shell Programming and Scripting

Grepping multiple strings from one column

I have 3-column tab separated data that looks like the following: act of+n-a-large+vn-tell-v 0.067427 act_com of+n+n-a-large-manufacturer-n 0.129922 act-act_com-com in+n-j+vn-pass-aux-restate-v 0.364499666667 com nmod+n-j+ns-invader-n 0.527521 act_com-com obj+n-a-j+vd-contribute-v 0.091413... (2 Replies)
Discussion started by: owwow14
2 Replies

7. Shell Programming and Scripting

Grepping one file column from another file

Hi all, I want to search the second col of a file as a sub-part of 4th col of another file and produce a joint output. In the example, search if B is contained as a sub-part in E:B:C (sub-parts separated by colons). Note the second row is not found doesnt find a match as F isnt there in col 4... (19 Replies)
Discussion started by: newbie83
19 Replies

8. Shell Programming and Scripting

Grepping multiple lines in a file

HI I have a file with output as System: cu=4 ent=0.1 mode=on cu min u s w i 0 500 0.1 0.3 0.5 0.1 1 200 0.5 0.2 0.3 0.0 I need to grep the values of following column fields u, s, w and i from each row sum them up and store in a variable..:( Please help.. (3 Replies)
Discussion started by: Priya Amaresh
3 Replies

9. UNIX for Dummies Questions & Answers

Grepping A Specific Column

Hello, I have a log file that outputs the data below. I would like to grep and display the data where column is equal '148.' I've searched the forum, and couldn't find any answers. I've tried all the grep switches and I get the same result as the log. I'm thinking I might have to use an... (4 Replies)
Discussion started by: ravzter
4 Replies

10. Shell Programming and Scripting

Grepping using multiple wildcards

Is there anyway you can grep using multiple wildcards? When I run the below line the results return fine; grep 12345 /usr/local/production/soccermatchplus/distributor/clients/*/out/fixtures.xml | awk -F/ '{print $8}' However ideally, I need it to grep for; grep 12345... (3 Replies)
Discussion started by: JayC89
3 Replies
Login or Register to Ask a Question