Skip to main content
added 8 characters in body
Source Link
Inian
  • 13.1k
  • 2
  • 42
  • 55

You can just make awk run on all those files in one shot, by grouping on the first column entries. The part map[$1]?(map[$1] FS $2):($2) is a ternary statement, meaning add to the array map indexed by $1, if it was empty or append to the already existing values if it is non-empty.

awk '{ map[$1] = map[$1]($1 in map)?(map[$1] FS $2):($2); } 
     END { for(i in map) print i, map[i] }' file*

To make the output a bit more readable than the output produced by awk, pipe the output as

awk '{ map[$1] = map[$1]($1 in map)?(map[$1] FS $2):($2); } 
     END { for(i in map) print i, map[i] }' file* | column -t > mergedfile.txt

You can just make awk run on all those files in one shot, by grouping on the first column entries. The part map[$1]?(map[$1] FS $2):($2) is a ternary statement, meaning add to the array map indexed by $1, if it was empty or append to the already existing values if it is non-empty.

awk '{ map[$1] = map[$1]?(map[$1] FS $2):($2); } 
     END { for(i in map) print i, map[i] }' file*

To make the output a bit more readable than the output produced by awk, pipe the output as

awk '{ map[$1] = map[$1]?(map[$1] FS $2):($2); } 
     END { for(i in map) print i, map[i] }' file* | column -t > mergedfile.txt

You can just make awk run on all those files in one shot, by grouping on the first column entries. The part map[$1]?(map[$1] FS $2):($2) is a ternary statement, meaning add to the array map indexed by $1, if it was empty or append to the already existing values if it is non-empty.

awk '{ map[$1] = ($1 in map)?(map[$1] FS $2):($2); } 
     END { for(i in map) print i, map[i] }' file*

To make the output a bit more readable than the output produced by awk, pipe the output as

awk '{ map[$1] = ($1 in map)?(map[$1] FS $2):($2); } 
     END { for(i in map) print i, map[i] }' file* | column -t > mergedfile.txt
Source Link
Inian
  • 13.1k
  • 2
  • 42
  • 55

You can just make awk run on all those files in one shot, by grouping on the first column entries. The part map[$1]?(map[$1] FS $2):($2) is a ternary statement, meaning add to the array map indexed by $1, if it was empty or append to the already existing values if it is non-empty.

awk '{ map[$1] = map[$1]?(map[$1] FS $2):($2); } 
     END { for(i in map) print i, map[i] }' file*

To make the output a bit more readable than the output produced by awk, pipe the output as

awk '{ map[$1] = map[$1]?(map[$1] FS $2):($2); } 
     END { for(i in map) print i, map[i] }' file* | column -t > mergedfile.txt