Need to grep column 2 value from second file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to grep column 2 value from second file
# 1  
Old 08-01-2011
Need to grep column 2 value from second file

Hi,
Here is the scenario. I have 2 files:-
File 1
Code:
I1  1
I2  2
I3 3

File2
Code:
I2 2.1
I1 1.1
I3 3.1

Now what I want is :-
Code:
I1 1 1.1
I2 2 2.2
I3 3 3.1

I tried using simple awk and grep commands and it works on small files, but am facing huge runtime issue with my actual files (in access of 1 GB and its running for last 1 hour).
Could someone let me know a perl script that could help my cause?

Thanks,
Atul

Last edited by Franklin52; 08-01-2011 at 11:10 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 08-01-2011
If it is ok for you to sort those files first, you could try if join is fast enough:
Code:
join -1 1 -2 1 -o 1.1 1.2 2.2 f1 f2
I1 1 1.1
I2 2 2.1
I3 3 3.1

-1 1 ---------> Take file 1, 1st field for join/matching
-2 1 ---------> Take file 2, 1st field for join/matching

1.1 ---------> Display file 1's 1st field
1.2 ---------> Display file 1's 2nd field
1.1 ---------> Display file 2's 1st field
# 3  
Old 08-01-2011
It works with zsh and ksh93 (and bash I think):
Code:
join <(sort INPUT1) <(sort INPUT2)

# 4  
Old 08-01-2011
Code:
$ cat file1
I1 1
I2 2
I3 3

$ cat file2
I2 2.1
I1 1.1
I3 3.1

$ cat file2 | sort -u > file3
 
$ cat file3
I1 1.1
I2 2.1
I3 3.1

$ paste file1 file3 | awk '{print $1" "$2" "$4}'
I1 1 1.1
I2 2 2.1
I3 3 3.1

---------- Post updated at 05:12 AM ---------- Previous update was at 04:39 AM ----------

with join command
Code:
$ join file1 file3
I1 1 1.1
I2 2 2.1
I3 3 3.1


Last edited by Franklin52; 08-01-2011 at 11:10 AM.. Reason: Please use code tags for data and code samples, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to awk or grep the last column in file when date on column contains spaces?

Hi have a large spreadsheet which has 4 columns APM00111803814 server_2 96085 Corp IT Desktop and Apps APM00111803814 server_2 96085 Corp IT Desktop and Apps APM00111803814 server_2 96034 Storage Mgmt Team APM00111803814 server_2 96152 GWP... (6 Replies)
Discussion started by: kieranfoley
6 Replies

2. Shell Programming and Scripting

How to grep an empty line in a specific column of a file?

Suppose i have the following data : cat file.txt 12431,123334,55353,546646,14342234,4646,35234 123123,3535,123434,132535,1234134,13535,123534 123213,545465,23434,45646,2342345,4656,31243 2355425,2134324,53425,342,35235,23434,234535 3423424,234234,65465,,2344,35436,234524,234... (7 Replies)
Discussion started by: Ravi Tej
7 Replies

3. Shell Programming and Scripting

Grep/Awk on 1st 2 Letters in 2nd Column of File

Hi everyone. I need to change a script (ksh) so that it will grep on the 1st 2 letters in the second column of a 5 column file such as this one: 192.168.1.1 CAXY0_123 10ABFL000001 # Comment 192.168.1.2 CAYZ0_123 10ABTX000002 # Comment 192.168.2.1 FLXY0_123 11ABCA000001 ... (4 Replies)
Discussion started by: TheNovice
4 Replies

4. UNIX for Dummies Questions & Answers

Grep from specific column in one file based on another file [Please help!]

Hey Guys, to keep it simple, I have an idFile: 1006006 1006008 1006011 1007002 ...... and famFile: 1006 1006001 1006016 1006017 1 1006 1006006 1006016 1006017 1 1006 1006007 0 0 2 1006 1006008 1006007 1006006 2 1006 1006010 1006016 1006017 2 1006 1006011 1006016 1006017 1 1006... (2 Replies)
Discussion started by: Zoho
2 Replies

5. Shell Programming and Scripting

Script to Grep column 3 from csv file generated yesterday

Hello, Can any one please assist how to scirpt it: Every day a new log file is create and I want to process only the one generated yesterday and get the data of column 3 and 6. For example today's date is 24 then I want to get the data of log file created on 23rd. Log Files in... (7 Replies)
Discussion started by: sureshcisco
7 Replies

6. UNIX for Advanced & Expert Users

grep 7th column from a file seperated with ^

Hi, Can anyone help: I want to get the 7th column of a file seperated by ^ Num:^ 1^ testdevice1^10.1.1.1^PT1X23^true^HD^175^up^false^bad Num:^ 2^ testdevice2^10.1.1.2^ST1X23^true^SN^175^up^false^bad Expected result: HD SN Appreciate your help. (2 Replies)
Discussion started by: sureshcisco
2 Replies

7. Shell Programming and Scripting

Help me! grep the log file without blank lines in column

Hi, I have log file like this: i want grep the log file without blank lines in column 4. So the file is become like this : What is the command? please help me. (1 Reply)
Discussion started by: justbow
1 Replies

8. Shell Programming and Scripting

Awk+Grep Input file needs to match a column and print the entire line

I'm having problems since few days ago, and i'm not able to make it works with a simple awk+grep script (or other way to do this). For example, i have a input file1.txt: cat inputfile1.txt 218299910417 1172051195 1172070231 1172073514 1183135117 1183135118 1183135119 1281440202 ... (3 Replies)
Discussion started by: poliver
3 Replies

9. Shell Programming and Scripting

Grep strings from file and put in Column

Dear Experts, My file contains below- GET:SUB:ISI,432350414557432; RESP:0:MD,019352020633:ISI,432350414557432:T11,1:T21,1:T22,1:B16,1:T62,1:BAIC,0:BAOC,1:BOIC,0:BIRO,0:BORO,0:PAID,1; GET:SUB:ISI,432350414581060;... (2 Replies)
Discussion started by: thepurple
2 Replies
Login or Register to Ask a Question