-2

I have two files.

file 1-- this is sql generate file with | delimiter

sabari|27|22-12-1990|CHENNAI|
Siva|29|13-11-1989|CHENNAI|

File 2 - I have created hard core file

Sabari|-|-|-|-|

Siva|-|-|-|-|

Ravi|-|-|-|-|

Bali|-|-|-|-|

I want to compare and place the value in File-1 from File-2 if the value not in f1.

Desired Out put

sabari|27|22-12-1990|CHENNAI|

Siva|29|13-11-1989|CHENNAI|

Ravi|-|-|-|-|

Bali|-|-|-|-|

Can some one help me /

Regular Jo
  • 5,190
  • 3
  • 25
  • 47
NewBee
  • 11
  • 6

1 Answers1

0

I would do:

awk '{k=tolower($1)}NR==FNR{a[k]=$0;next} k in a{$0=a[k]}1' FS=\| file1 file2

Make a pass over the first file, storing everything in memory hashed on the first entry. Then pass over the second file, checking if the line was given in the first file and print the line from that file if it was seen. The "tolower" just makes comparisons case insensitive.

Maybe it's clearer written as:

awk '{k=tolower($1)}NR==FNR{a[k]=$0;next}{print k in a ? a[k] : $0}' FS=\| file1 file2

The NR==FNR technique is a common way to pre-load data from the first input file.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • [sabarinathan] awk '{k=tolower($1)}NR==FNR{a[k]=$0;next}{print k in a ? a[k] : $0}' FS=\| MF_STATUS.txt JOBNULL.txt awk: syntax error near line 1 awk: illegal statement near line 1 – NewBee Mar 09 '17 at 08:42