Matching pattern in two files and print the line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Matching pattern in two files and print the line
# 1  
Old 10-14-2013
Matching pattern in two files and print the line

Hi,

I want to match the pattern in file1 with file2 and print the value in file2 and paste in file1
file1:

Code:
ISHO RT SR                                Major     96.46778
Drop Call Rate CS                       Critical  0.5072662
ISHO RT SR                                Major     97.754364
Active HS-DSCH MAC-d          Critical  1979.25
ISHO RT SR                                Major     97.31076

file2:
Code:
RRC Stup RNC Failures   0.1
Active HS-DSCH MAC-d    2000    
Drop Call Rate PS       0.8
ISHO RT SR    96

I want the output:
Code:
ISHO RT SR                                Major     96.4        96
Drop Call Rate PS                       Critical  0.50         0.8
ISHO RT SR                                Major     97.75      96
Active HS-DSCH MAC-d          Critical  1979.25   2000
ISHO RT SR                                Major     97.3       96

I tried grep each pattern:
Code:
grep -i 'pattern' file1 file2 ',awk '{$0}''

It's not working

Last edited by Scrutinizer; 10-14-2013 at 04:36 AM.. Reason: code tags
# 2  
Old 10-14-2013
Your data format is not scripting friendly (no clear delimiter between fields). Anyway... Try this:
Code:
awk 'NR==FNR{x=$NF;$NF="";sub(" $","",$0);a[$0]=x;next}{for (i in a) if ($0~i) $(NF+1)=a[i]}1' file2 file1

# 3  
Old 10-14-2013
The example you've provided is ambiguous or need some precisions.

example : in your file1 :

Code:
Drop Call Rate CS ...

in your file2 :
Code:
Drop Call Rate PS ...

in your output :

Code:
Drop Call Rate PS ...

Should we suppose that you check the matching on the 3 first words only?
Or is you file TAB delimited and should the matching be based on the whole first field ?
# 4  
Old 10-17-2013
Thanks,

I used :
Code:
awk 'NR==FNR{x=$NF;$NF="";sub(" $","",$0);a[$0]=x;next}{for (i in a) if ($0~i) $(NF+1)=a[i]}1' file2 file1

and paste the values fine.

What about line up the last two vaules (How I can interpret \t tab for insertion to line up the value in same column and get the output:
Code:
ISHO RT SR  Major     96.4  96
Drop Call Rate PS Critical  0.50  0.8
ISHO RT SR  Major     97.75  96
Active HS-DSCH MAC-d Critical  1979.25 2000
ISHO RT SR  Major     97.3  96


Last edited by Franklin52; 10-17-2013 at 09:00 AM.. Reason: Please use code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print 2 matching pattern

How to a first 3 lines lines that matches 1st pattern and 1 line that matches 2nd line. Sample.txt Line 1: /*--- ABC_BA_ABCABC -----*/ Line 2: Line 3: insert_job: ABC_BA_ABCABC job_type: BOX Line 4: blah blah Line 5: max_run_alarm: 5 Line 6: blah blah ---- ----- Line 20: /*---... (4 Replies)
Discussion started by: bobbygsk
4 Replies

2. Shell Programming and Scripting

Print lines after matching two pattern

would like to print everything after matching two patterns AAA and BBB. output : CCC ZZZ sample data : AAA BBB CCC ZZZ (4 Replies)
Discussion started by: jhonnyrip
4 Replies

3. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

4. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

5. Shell Programming and Scripting

How to print all the lines after pattern matching?

I have a file that contains... Number -------------------- 1 2 3 4 i want to print all the numbers after the hyphen ... (6 Replies)
Discussion started by: ankitknit
6 Replies

6. UNIX for Dummies Questions & Answers

sed print matching pattern

Hi, i have data file like: START1 a b STOP c d START2 e STOP f START3 g STOP When one of the START<count> variable is passed, i should print all lines matching this until the first 'STOP' for example if 'START2' is provided for match, i should get the result as: START2 (1 Reply)
Discussion started by: ysrini
1 Replies

7. Shell Programming and Scripting

print range of lines matching pattern and previous line

Hi all, on Solaris 10, I'd like to print a range of lines starting at pattern but also including the very first line before pattern. the following doesn't print the range starting at pattern and going down to the end of file: cat <my file> | sed -n -e '/<pattern>{x;p;}/' I need to include the... (1 Reply)
Discussion started by: siriche
1 Replies

8. Shell Programming and Scripting

I want to print next 3 lines after pattern matching.

Dear Experts, I have file called file1 in which i am greping a pattern after that i want to next 3 lines when that pattern is matched. Ex:- file1 USA UK India Africa Hello Asia Europe Australia Hello Peter Robert Jo i want to next 3 lines after matching Hello... (12 Replies)
Discussion started by: naree
12 Replies

9. Shell Programming and Scripting

comment/delete a particular pattern starting from second line of the matching pattern

Hi, I have file 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433... (4 Replies)
Discussion started by: imas
4 Replies

10. Shell Programming and Scripting

pattern matching and print with sed

Hi It is possible with sed to print a pattern within a line matching regexp? So, the line looks like : 19:00:00 blablablabla jobid 2345 <2> the regexp is "jobid 2345" and the pattern is 56434. That the code for find... (2 Replies)
Discussion started by: nymus7
2 Replies
Login or Register to Ask a Question