Attach multiple files in gawk

I have a large number of files (about 500). Each file contains two columns. The first column is the same for each file. I want to merge all files into one file using gawk.
For instance,

File 1  
a 123  
b 221  
c 904 

File 2 
a 298  
b 230  
c 102  

etc. I need the final file as shown below:

Final file
a 123 298  
b 221 230  
c 904 102  

I found scripts that can join two files, but I need to merge several files.

+4
source share
4 answers

For these samples:

$ head f*
==> f1 <==
a 123
b 221
c 904

==> f2 <==
a 298
b 230
c 102

==> f3 <==
a 500
b 600
c 700

Method 1:

$ awk '{a[FNR]=((a[FNR])?a[FNR]FS$2:$0)}END{for(i=1;i<=FNR;i++) print a[i]}' f*
a 123 298 500
b 221 230 600
c 904 102 700

Method 2: (It will probably be faster since you are not loading 500 files into memory)

paste awk . (, ). paste f* :

$ paste f*
a 123   a 298   a 500
b 221   b 230   b 600
c 904   c 102   c 700

awk, .

$ paste f* | awk '{printf "%s ",$1;for(i=2;i<=NF;i+=2) printf "%s%s",$i,(i==NF?RS:FS)}'
a 123 298 500
b 221 230 600
c 904 102 700

.

+5

.

getline gawk.

getline var < filename

.

, . 5 .

j=1;
j=getline x < "filename";
if(j==0) {
      break;
}
... (Commands involving x such as split and print).
+1

- :

$ ls
f1.txt  f2.txt  f3.txt
$ awk '($0 !~ /#/){a[$1]=a[$1]" "$2} END {for(i in a){print i""a[i]}}' *.txt
a 123 298 299
b 221 230 231
c 904 102 103
0
awk 'FNR==NR{arr[$1]=$2; next;}{printf "%s%s%s%s%s",$1,OFS,arr[$1],OFS,$2; print"";}' file1 file2

0

Source: https://habr.com/ru/post/1537225/


All Articles