Home > front end >  Append data from 1 file to another using AWK
Append data from 1 file to another using AWK

Time:08-03

I have an already existing script to check the exclusive data between 2 files and load it in 3rd file. The command is below.

var='FNR == NR {keys[$1 $2]; next} !($1 $2 in keys)'
awk -F\| $var file1.dat file2.dat > file3.dat

The requirement is to reuse the same but just append the data from file2 to file3 ignoring file1. I tried to do the below but it is spooling the data from both file1 and file2. All I need is, though there are 2 file names provided in the awk command, only the 2nd file data to be appended.

var='{print $0}'
awk -F\| $var file1.dat file2.dat > file3.dat    

Can anyone help with the exact command. Below is the data in each file and expected output.

File1 (Can have 0 or more) - We should not look at this file at all

123
456
789

File2:

123
ABC
XYZ
456

Expected output in File3 (All from file2 and just ignore file1 input, but I have to have the file1 name in awk command)

123
ABC
XYZ
456

CodePudding user response:

All from file2 and just ignore file1 input, but I have to have the file1 name in awk command.

If you must use file1 and file2 in arguments to awk command and want to output content from file2 only then you can just use:

awk 'BEGIN {delete ARGV[1]} 1' file1 file2 > file3

123
ABC
XYZ
456

delete ARGV[1] will delete first argument from argument list.

CodePudding user response:

With your shown samples and attempts please try following awk code. Written and tested in GNU awk. Simply use nextfile to skip first Input_file named file1 itself and read 2nd file onwards.

awk 'NR==1{nextfile} 1' file1 file2
  • Related