Awk: extract different columns from many different files

前端 未结 3 1338
北恋
北恋 2020-12-09 06:40

File Example

I have a 3-10 amount of files with:

 - different number of columns
 - same number of rows
 - inconsistent spacing (sometimes o         


        
3条回答
  •  离开以前
    2020-12-09 07:38

    The combination of cut and paste should work:

    $ cat f1
    foo
    bar
    baz
    $ cat f2
    1 2 3
    4 5 6
    7 8 9
    $ cat f3
    a b c d
    e f g h
    i j k l
    $ paste -d' ' <(cut -f1 f1) <(cut -d' ' -f2 f2) <(cut -d' ' -f3 f3)
    foo 2 c
    bar 5 g
    baz 8 k
    

    Edit: This works with tabs, too:

    $ cat f4
    a       b       c       d
    e       f       g       h
    i       j       k       l
    $ paste -d' ' <(cut -f1 f1) <(cut -d' ' -f2 f2) <(cut -f3 f4)   
    foo 2 c
    bar 5 g
    baz 8 k
    

提交回复
热议问题