coreutils: Header lines
1
1 8.3.5 Header lines
1 ------------------
1
1 The ‘--header’ option can be used when the files to join have a header
1 line which is not sorted:
1
1 $ cat file1
1 Name Age
1 Alice 25
1 Charlie 34
1
1 $ cat file2
1 Name Country
1 Alice France
1 Bob Spain
1
1 $ join --header -o auto -e NA -a1 -a2 file1 file2
1 Name Age Country
1 Alice 25 France
1 Bob NA Spain
1 Charlie 34 NA
1
1 To sort a file with a header line, use GNU ‘sed -u’. The following
1 example sort the files but keeps the first line of each file in place:
1
1 $ ( sed -u 1q ; sort -k2b,2 ) < file1 > file1.sorted
1 $ ( sed -u 1q ; sort -k2b,2 ) < file2 > file2.sorted
1 $ join --header -o auto -e NA -a1 -a2 file1.sorted file2.sorted > file3
1