1

Here is my 1.file

             id
             a1
             a2
             a3
             a4

Here is my 2.file

             DW  1  2  3  4
             KD  2  3  4  5
             LBJ 4  4  4  4

I want to get my final file

             id  a1 a2 a3 a4
             DW  1  2  3  4
             KD  2  3  4  5
             LBJ 4  4  4  4
            

And I try to

cat 1.file |tr "\n" "\t"|sed -e 's/,$/\n/'

and then

cat 1.file 2.file >> fina.file

but I want to get the awk way

2
  • 1
    don't step down my reputation, please. LOL
    – loki
    Jul 8, 2022 at 14:32
  • 2
    Never use tr to convert newlines to some other character and then pipe the result to a different text processing tool because removing all of the newlines turns your text into something that can't reliably be read by any text processing tool as it's now undefined behavior (all text processing tools are only guaranteed to work with valid text file input and a valid text file MUST have a newline at the end. Also see porkmail.org/era/unix/award - cat file | cmd can always be written as either cmd file or cmd < file for any command cmd.
    – Ed Morton
    Jul 9, 2022 at 2:20

4 Answers 4

5
$ column -t <( paste -s 1.file ) 2.file
id   a1  a2  a3  a4
DW   1   2   3   4
KD   2   3   4   5
LBJ  4   4   4   4

The lines of 1.file are turned into a single line of headers by means of paste -s and then column -t is used to align these headers with the data in 2.file.

The above assumes that you are using a shell that understands process substitutions with <(...). If you are not, then use the following instead:

paste -s 1.file | column -t /dev/stdin 2.file
2

Assuming that by I want to get the awk way you mean you want to learn how to do everything in one awk script:

$ awk 'NR==FNR{ hdr=hdr sep $0; sep=OFS; next} FNR==1{ print hdr } 1' 1.file 2.file
id a1 a2 a3 a4
DW  1  2  3  4
KD  2  3  4  5
LBJ 4  4  4  4
3
  • Best answer, but doesn't handle the leading whitespace correctly for the header.
    – anick
    Jul 9, 2022 at 12:59
  • 1
    @anick in cases like this there invariably is no leading white space in the real data, it's just a formatting error in the question. In 40+ years of programming I've never come across real data where every line starts with a bunch of white space that has to be retained.
    – Ed Morton
    Jul 9, 2022 at 13:02
  • You are, of course, correct. But still.
    – anick
    Jul 9, 2022 at 13:08
1

Something like can do the work:

awk '{ORS=(NR%5?FS:RS)}1' 1.file >final.file
cat 2.file >>final.file

The awk code print the 5 consecutive rows as one. And then with cat you add the rest of the file

0
1

Keep indentation:

cat <( fmt 1.file ) 2.file
             id a1 a2 a3 a4
             DW  1  2  3  4
             KD  2  3  4  5
             LBJ 4  4  4  4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.