Row to column and column to row using awk

后端 未结 4 1911
小鲜肉
小鲜肉 2020-12-19 00:14

I have two files containing as below

cat file1.txt
a b c 
1 2 3

cat file2.txt
a
b
c
1
2
3

I want file1 to be arranged as

a         


        
4条回答
  •  南笙
    南笙 (楼主)
    2020-12-19 01:04

    Some awk version

    awk 1 RS=" |\n" file1  # gnu awk version
    awk '{for (i=1;i<=NF;i++) print $i}' file1 # portable version
    a
    b
    c
    1
    2
    3
    


    awk '{printf "%s" (NR%3==0?RS:FS),$1}' file2
    a b c
    1 2 3
    

    printf "%s" # print pararameter #1 ($1)
    NR%3==0?"RS:FS # add extra formatting. Test if line is number 3. If its not, use FS (a blank space), if it is use RS, a new line.
    So this adjust the next parameter after every 3 line.

提交回复
热议问题