compare two files, diff the result


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting compare two files, diff the result
# 1  
Old 02-11-2010
compare two files, diff the result

Hi Everyone,
1.txt
Code:
00:01:01 asdf
00:33:33 1234
00:33:33 0987
00:33:33 12
00:33:33 444

2.txt
Code:
vvvv|ee
444|dd33|ee
dddd|ee
12|ee
3ciur|fdd

the output should be:
Code:
00:33:33 12;12|ee
00:33:33 444;444|dd33|ee

means if 1.txt line can be found in 2.txt, then print both line seperated by ";".
Please advice.
Thanks Smilie
# 2  
Old 02-11-2010
Code:
awk -F\| 'NR == FNR {
  k[$1] = ";" $0; next 
  }
$2 in k && $0 = $0 k[$2]
' 2.txt FS='[ \t]*' 1.txt

# 3  
Old 02-11-2010
Quote:
Originally Posted by radoulov
Code:
awk -F\| 'NR == FNR {
  k[$1] = ";" $0; next 
  }
$2 in k && $0 = $0 k[$2]
' 2.txt FS='[ \t]*' 1.txt

SmilieSmilieSmilie

---------- Post updated at 05:52 AM ---------- Previous update was at 05:41 AM ----------

Hi radoulov,

if my 2.txt change to
Code:
vvvv|ee
qqqqq444|dd33|ee
dddd|ee
qq33f12|ee
3ciur|fdd

how to output the same result Smilie
# 4  
Old 02-11-2010
And 1.txt remains the same?

---------- Post updated at 11:58 AM ---------- Previous update was at 11:55 AM ----------

Code:
awk -F\| 'NR == FNR {
  k[$1] = ";" $0; next 
  }
{
  for (K in k) 
    if (K ~ $2) {
      print $0 k[K]
      break
      }
  }
' 2.txt FS='[ \t]*' 1.txt

# 5  
Old 02-11-2010
Quote:
Originally Posted by radoulov
And 1.txt remains the same?

---------- Post updated at 11:58 AM ---------- Previous update was at 11:55 AM ----------

Code:
awk -F\| 'NR == FNR {
  k[$1] = ";" $0; next 
  }
{
  for (K in k) 
    if (K ~ $2) {
      print $0 k[K]
      break
      }
  }
' 2.txt FS='[ \t]*' 1.txt

print $0 k[K] should be print $0; k[K];

Thanks, it works so perfect Smilie i learn a lot awk.

---------- Post updated at 08:50 AM ---------- Previous update was at 08:36 AM ----------

But seems this has a bug.
1.txt (add the 1st line)
Code:
iiiii
00:01:01 asdf
00:33:33 1234
00:33:33 0987
00:33:33 12
00:33:33 444

2.txt (no change)
Code:
vvvv|ee
qqqqq444|dd33|ee
dddd|ee
qq33f12|ee
3ciur|fdd

the output is
Code:
awk -F\| 'NR == FNR {k[$1] = ";" $0; next} {for (K in k) if (K ~ $2) {print $0 k[K]; break}}' 2 FS='[ \t]*' 1
iiiii;vvvv|ee
00:33:33        12;qq12|ee
00:33:33        444;qq444|dd33|ee

as you can see extra "iiii;vvvv|ee" here, but it should not be there. Smilie
where should i put "if (NR!=1)"...
Thanks
# 6  
Old 02-11-2010
Try this one:

Code:
awk -F\| 'NR == FNR {
  k[$1] = ";" $0; next 
  }
NF > 1 {
  for (K in k) 
    if (K ~ $2) {
      print $0 k[K]
      break
      }
  }
' 2.txt FS='[ \t]*' 1.txt

# 7  
Old 02-11-2010
Quote:
Originally Posted by radoulov
Try this one:

Code:
awk -F\| 'NR == FNR {
  k[$1] = ";" $0; next 
  }
NF > 1 {
  for (K in k) 
    if (K ~ $2) {
      print $0 k[K]
      break
      }
  }
' 2.txt FS='[ \t]*' 1.txt

Smilie perfect
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 compare the current result with previous line result.?

Hi Gurus, I have requirement to compare current result with previous reuslt. The sample case is below. 1 job1 1 1 job2 2 1 job3 3 2 job_a1 1 2 job_a2 2 2 job_a3 3 3 job_b1 1 3 job_b2 2 for above sample file, GID is group ID, for input line, the job run... (1 Reply)
Discussion started by: ken6503
1 Replies

2. Shell Programming and Scripting

Compare two CSV files and put the difference in third file with line no,field no and diff value.

I am having two csv files i need to compare these files and the output file should have the information of the differences at the field level. For Example, File 1: A,B,C,D,E,F 1,2,3,4,5,6 File 2: A,C,B,D,E,F 1,2,4,5,5,6 out put file: (12 Replies)
Discussion started by: karingulanagara
12 Replies

3. Shell Programming and Scripting

Shell script to compare ,diff and remove betwen 2 files

Hi Friends Need your expertise. Command to check the difference and compare 2 files and remove lines . example File1 is master copy and File2 is a slave copy . whenever i change, add or delete a record in File1 it should update the same in slave copy . Can you guide me how can i accomplish... (3 Replies)
Discussion started by: ajayram_arya
3 Replies

4. Shell Programming and Scripting

How to compare two files to get correct result.see example?

Hello, I have two files A and B .A file consist of the words that user will put whereas file B consist of all user's LDAP a/c.I want here that if user don't mention LDAP a/c in comment than it will exit otherwise success. File A.I have a solution that I will modify the file A ...just save the... (9 Replies)
Discussion started by: anuragpgtgerman
9 Replies

5. Shell Programming and Scripting

Using Diff to Compare 2 files

Hi I've been trying various methods that I have found online with regards to comparing 2 files using the diff command. Nothing seems to work. The problem is that I'm not too familiar with the proper syntax. Can you please assist me. Here is my script: #!/bin/bash awk -F',' -v file1="$1"... (9 Replies)
Discussion started by: ladyAnne
9 Replies

6. Shell Programming and Scripting

Find diff bet 2 files and store result in another file

Hi I want to compare 2 files. The files have the same amount of rows and columns. So each line must be compare against the other and if one differs from the other, the result of both must be stored in a seperate file. I am doing this in awk. Here is my file1: Blocks... (2 Replies)
Discussion started by: ladyAnne
2 Replies

7. Shell Programming and Scripting

Compare two files and output diff to third file

I have made several attempts to read two files of ip addresses and eliminate records from file1 that are in file2. My latest attempt follows. Everything works except my file3 is exactly the same as file1 and it should not be. # !/usr/bin/bash # # NoInterfaces # Utility will create a file... (8 Replies)
Discussion started by: altamaha
8 Replies

8. Shell Programming and Scripting

compare 2 coloum of 2 diff files using perl script

Hi, i am new to perl scripting.. i am still learing it.. i am asked to write a perl script which should compare 2 coloums of 2 different files. if those 2 coloumn are same the script should store the both the lines in 2 diff files. these are files, file 1: 21767016 226112 char 19136520... (3 Replies)
Discussion started by: vasuki
3 Replies

9. UNIX for Dummies Questions & Answers

Compare 2 files and output result in pop up window

Need help ! how do i compare 2 files and if there is a difference, output a massage in a pop up window? the script meant to run using crontab. (2 Replies)
Discussion started by: hongsh
2 Replies
Login or Register to Ask a Question