1.txt
A
B
C
D
2nd file:
2.txt
1
2
3
4
Output that I want to get:
A1
B2
C3
D4
How to get this output using awk command
1.txt
A
B
C
D
2nd file:
2.txt
1
2
3
4
Output that I want to get:
A1
B2
C3
D4
How to get this output using awk command
Why awk?
paste can do it easily:
$ paste -d"\0" file1 file2
Maybe he like to understand how array in awk
works
awk 'FNR==NR {a[++f]=$0;next} {print a[FNR] $0}' 1.txt 2.txt
A1
B2
C3
D4
paste
would be the easiest solution, but using awk one could also:
awk 'getline $2<f' FS= OFS= f=file2 file1
This was a new one for me, could you pleas explain this.
I see you add second file to a variable f
, but does not get how the getline
get next line in f
Hi Jotne, it is reading a line from file1 and if it can also read a line from file2 and put it in a new $2, then the line gets printed, with $1 containing the line from file1 and $2 the line from file2, the blank field separators make sure there are only two fields...
On rereading I noticed however, that there can a problem with this, because some awk's have a special extension when FS is empty (every character is then split into fields, which is not a POSIX thing), so it should be:
awk 'getline $2<f' FS='\n' OFS= f=file2 file1