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
But what if I want to print chars 2 through 8? or 3 through 7? Is that possible with awk?
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
But what if I want to print chars 2 through 8? or 3 through 7? Is that possible with awk?
awk '{print substr($0,2,6)}' file
the syntax for substr() is
substr(string,start index,length)
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
If Perl is an option:
perl -lne 'print substr($_,1,6)' file
Output is identical to answer from @nims