Need solution to compare two file and update and append the output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need solution to compare two file and update and append the output
# 1  
Old 11-14-2017
Need solution to compare two file and update and append the output

Hi All,

I have two files

File1

Code:
frame,007C1 server1_Parent
frame,007C3 server2_Silver
frame,007EE server3_Bronze
frame,00855 server4_Parent
frame,00856 server4_Parent
frame,00858 server5_Parent
frame,008FA server6_Silver
frame,008FB server6_Silver
frame,008FC server6_Silver
frame,008FD server6_Silver
frame,008FE server6_Silver
frame,008FF server6_Silver
frame,00900 server6_Silver
frame,00901 server6_Silver
frame,00902 server6_Silver
frame,00903 server6_Silver
frame,00904 server6_Silver
frame,00905 server6_Silver
frame,00955 server7_Silver
frame,00956 server7_Silver
frame,00957 server7_Silver

File2
Code:
00856 00E5C   R1:3   RW RW RW  A..1.        0        0 RW  WD   Consistent  
00858 000E2   R1:1   RW RW RW  S..1.        0        0 RW  WD   Synchronized
008FA 00AE8   R1:1   ?? RW RW  C.D1.        0        0 RW  WD   Synchronized
008FE 00AEC   R1:1   ?? RW RW  C.D1.        0        0 RW  WD   Synchronized
008FF 00AED   R1:1   ?? RW RW  C.D1.        0        0 RW  WD   Synchronized
00905 00AF3   R1:1   ?? RW RW  C.D1.        0        0 RW  WD   Synchronized
00955 00BE1   R1:1   RW RW RW  S..1.        0        0 RW  WD   Synchronized
00956 00BE2   R1:1   RW RW RW  S..1.        0        0 RW  WD   Synchronized
00957 00BE3   R1:1   RW RW RW  S..1.        0        0 RW  WD   Synchronized
00900 00AEE   R1:1   ?? RW RW  C.D1.        0        0 RW  WD   Synchronized
00901 00AEF   R1:1   ?? RW RW  C.D1.        0        0 RW  WD   Synchronized
00902 00AF0   R1:1   ?? RW RW  C.D1.        0        0 RW  WD   Synchronized
00903 00AF1   R1:1   ?? RW RW  C.D1.        0        0 RW  WD   Synchronized
00904 00AF2   R1:1   ?? RW RW  C.D1.        0        0 RW  WD   Synchronized
007C1 005CD   R1:1   ?? RW RW  S..1.        0        0 RW  WD   Synchronized
007C3 005CE   R1:1   ?? RW RW  C.D1.        0        0 RW  WD   Synchronized
007EE 000EE   R1:1   RW RW RW  S..1.        0        0 RW  WD   Synchronized
00855 00E5B   R1:3   RW RW RW  A..1.        0        0 RW  WD   Consistent  
008FB 00AE9   R1:1   ?? RW RW  C.D1.        0        0 RW  WD   Synchronized
008FC 00AEA   R1:1   ?? RW RW  C.D1.        0        0 RW  WD   Synchronized
008FD 00AEB   R1:1   ?? RW RW  C.D1.        0        0 RW  WD   Synchronized

I need output

Code:
frame,007C1 server1_Parent                  007C1 005CD   R1:1   ?? RW RW  S..1.        0        0 RW  WD   Synchronized  
frame,007C3 server2_Silver                  007C3 005CE   R1:1   ?? RW RW  C.D1.        0        0 RW  WD   Synchronized  
frame,007EE server3_Bronze                  007EE 000EE   R1:1   RW RW RW  S..1.        0        0 RW  WD   Synchronized  
frame,00855 server4_Parent                  00855 00E5B   R1:3   RW RW RW  A..1.        0        0 RW  WD   Consistent    
frame,00856 server4_Parent                  00856 00E5C   R1:3   RW RW RW  A..1.        0        0 RW  WD   Consistent    
frame,00858 server5_Parent                  00858 000E2   R1:1   RW RW RW  S..1.        0        0 RW  WD   Synchronized  
frame,008FA server6_Silver                  008FA 00AE8   R1:1   ?? RW RW  C.D1.        0        0 RW  WD   Synchronized  
frame,008FB server6_Silver                  008FB 00AE9   R1:1   ?? RW RW  C.D1.        0        0 RW  WD   Synchronized  
frame,008FC server6_Silver                  008FC 00AEA   R1:1   ?? RW RW  C.D1.        0        0 RW  WD   Synchronized  
frame,008FD server6_Silver                  008FD 00AEB   R1:1   ?? RW RW  C.D1.        0        0 RW  WD   Synchronized  
frame,008FE server6_Silver                  008FE 00AEC   R1:1   ?? RW RW  C.D1.        0        0 RW  WD   Synchronized  
frame,008FF server6_Silver                  008FF 00AED   R1:1   ?? RW RW  C.D1.        0        0 RW  WD   Synchronized  
frame,00900 server6_Silver                  00900 00AEE   R1:1   ?? RW RW  C.D1.        0        0 RW  WD   Synchronized  
frame,00901 server6_Silver                  00901 00AEF   R1:1   ?? RW RW  C.D1.        0        0 RW  WD   Synchronized  
frame,00902 server6_Silver                  00902 00AF0   R1:1   ?? RW RW  C.D1.        0        0 RW  WD   Synchronized  
frame,00903 server6_Silver                  00903 00AF1   R1:1   ?? RW RW  C.D1.        0        0 RW  WD   Synchronized  
frame,00904 server6_Silver                  00904 00AF2   R1:1   ?? RW RW  C.D1.        0        0 RW  WD   Synchronized  
frame,00905 server6_Silver                  00905 00AF3   R1:1   ?? RW RW  C.D1.        0        0 RW  WD   Synchronized  
frame,00955 server7_Silver                  00955 00BE1   R1:1   RW RW RW  S..1.        0        0 RW  WD   Synchronized  
frame,00956 server7_Silver                  00956 00BE2   R1:1   RW RW RW  S..1.        0        0 RW  WD   Synchronized  
frame,00957 server7_Silver                  00957 00BE3   R1:1   RW RW RW  S..1.        0        0 RW  WD   Synchronized

Need the check the device from colume2 of file1 and check if the device was there in file-two from colume1 and i need to append both line to one line. I dont want for loop to each device since it was taking time any other solution.
# 2  
Old 11-14-2017
Try something like this:
Code:
awk -F'[ ,]' 'NR==FNR{A[$1]=$0; next} $2 in A{print $0, A[$2]}' file2 file1


Last edited by Scrutinizer; 11-14-2017 at 10:50 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 11-14-2017
hi Scrutinizer

Working great thanks million
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to update file with partial matching line in another file and append text

In the awk below I am trying to cp and paste each matching line in f2 to $3 in f1 if $2 of f1 is in the line in f2 somewhere. There will always be a match (usually more then 1) and my actual data is much larger (several hundreds of lines) in both f1 and f2. When the line in f2 is pasted to $3 in... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Append to a file repeating output

Hello, i'm trying to force a command to read every second from an interface watch -n1 (command) /dev/x | cat >> output but it continue to overwrite the file, without append the content Thanks and advace for help as usual regards (4 Replies)
Discussion started by: Board27
4 Replies

3. Shell Programming and Scripting

Compare a common field in two files and append a column from File 1 in File2

Hi Friends, I am new to Shell Scripting and need your help in the below situation. - I have two files (File 1 and File 2) and the contents of the files are mentioned below. - "Application handle" is the common field in both the files. (NOTE :- PLEASE REFER TO THE ATTACHMENT "Compare files... (2 Replies)
Discussion started by: Santoshbn
2 Replies

4. UNIX for Dummies Questions & Answers

Noob questions.. Append output to a file in different directory

Noob question! I know almost nothing so far, and I'm trying to teach myself from books, on a typical command line without using scripts how would I append output from a sort to a file in a completely different directory? example: If I'm sorting a file in my documents directory but I... (2 Replies)
Discussion started by: Byrang
2 Replies

5. Shell Programming and Scripting

problem with print append to output file syntax

I'm trying to output the contents of the infile to the outfile using Append. I will want to use append but the syntax doesn't seem to be working ! Input file (called a.txt) contains this: a a a b b b I'm running shell script (called k.sh) from Unix command-line like this: ./k.sh .... (1 Reply)
Discussion started by: script_op2a
1 Replies

6. Shell Programming and Scripting

append an output file with two columns

Hi All, can you help me with this: grep XXX dir/*.txt|wc -l > newfile.txt - this put the results in the newfile.txt, but I want to add another column in the newfile.txt, string 'YYYYY', separated somehow, which corresponds on the grep results? For example grep will grep XXX dir/*.txt|wc -l >... (5 Replies)
Discussion started by: apenkov
5 Replies

7. UNIX for Dummies Questions & Answers

Append file with grep output but add timestamp?

I've setup a cron job that greps a file every five minutes and then writes (appends) the grep output/result to another file: grep "monkey" zoo.log | tail -1 >> cron-zoo-log Is there any way I can add the date and time (timestamp) to the cron-zoo-log file for each time a new line was added? ... (12 Replies)
Discussion started by: Sepia
12 Replies

8. UNIX for Dummies Questions & Answers

Output to file but append rather than overwrite?

I am running a command which has a parameter that outputs the results to a file each time it is run. Here is the command: --fullresult=true > importlog.xml Can I add the output to the file rather than creating a new one which overwrites the existing one? If not can I make the file name... (2 Replies)
Discussion started by: Sepia
2 Replies

9. Shell Programming and Scripting

Append Output to another file in Perl

Hi All, I am writing a Perl script such that the output from "perl myscript.pl file1" to be appended to another file name called file2. I tried out with the below code but couldn't work. Can any expert give me some advice? open(OUTPUT, 'perl myscript.pl file1 |'); close OUTPUT;... (7 Replies)
Discussion started by: Raynon
7 Replies

10. Shell Programming and Scripting

Append output to file

Hi, I have a script below. It get's the data from the output of a script that is running hourly. My problem is every time my script runs, it deletes the previous data and put the current data. Please see output below. What I would like to do is to have the hourly output to be appended on the... (3 Replies)
Discussion started by: ayhanne
3 Replies
Login or Register to Ask a Question