awk to print all columns from the nth to the last with spaces

后端 未结 4 616
不知归路
不知归路 2021-01-23 21:26

I have the following input file:

a 1  o p
b  2 o p p
c     3 o p p  p

in the last line there is a double space between the last p\'s

4条回答
  •  爱一瞬间的悲伤
    2021-01-23 22:06

    GNU sed

    remove first n fields

    sed -r 's/([^ ]+ +){2}//' file
    

    GNU awk 4.0+

    awk '{sub("([^"FS"]"FS"){2}","")}1' file
    

    GNU awk <4.0

    awk --re-interval '{sub("([^"FS"]"FS"){2}","")}1' file
    

    Incase FS one doesn't work(Eds suggestion)

    awk '{sub(/([^ ] ){2}/,"")}1' file
    

    Replace 2 with number of fields you wish to remove

    EDIT

    Another way(doesn't require re-interval)

    awk '{for(i=0;i<2;i++)sub($1"[[:space:]]*","")}1' file
    

    Further edit

    As advised by EdMorton it is bad to use fields in sub as they may contain metacharacters so here is an alternative(again!)

    awk '{for(i=0;i<2;i++)sub(/[^[:space:]]+[[:space:]]*/,"")}1' file
    

    Output

    o p
    o p p
    o p p  p
    

提交回复
热议问题