Using awk to print characters of specific index on a line

前端 未结 3 1248
野的像风
野的像风 2020-12-05 09:25

Alright, so I know it is quite simple to print specific arguments of a line using $:

$ cat file
hello world

$ awk \'{print $1}\' file
hello
相关标签:
3条回答
  • 2020-12-05 09:57
    awk '{print substr($0,2,6)}' file
    

    the syntax for substr() is

    substr(string,start index,length)

    0 讨论(0)
  • 2020-12-05 09:59

    Yes. You can use substr function :

    http://www.starlink.rl.ac.uk/docs/sc4.htx/node38.html

    In your case - for print chars from 2 through 8:

    echo "hello" | awk '{ print substr( $0, 2, 6 ) }'
    

    result is:

    ello

    0 讨论(0)
  • 2020-12-05 10:17

    If Perl is an option:

    perl -lne 'print substr($_,1,6)' file
    

    Output is identical to answer from @nims

    0 讨论(0)
提交回复
热议问题