I would like to print the 55-60th character on each line of a file. I have to do this because I have a FORTRAN formatted file, and I don't have the same number of field separators on each line:
HETATM 3109 O HOH B 999 10.307 26.441 12.306 0.26 30.00 O
HETATM 3110 O HOH B1000 10.905 26.874 14.064 0.20 30.00 O
Run Code Online (Sandbox Code Playgroud)
字符55-60是顶行的第10个字段,底行的第9个字段.如何55-60使用awk 打印字符?
使用-c来自cut:
$ cut -c55-60 file
0.26
0.20
Run Code Online (Sandbox Code Playgroud)
来自man cut:
-c, - characters = LIST
只选择这些字符
或者sed:
$ sed -r 's/.{54}(.{6}).*/\1/' file
0.26
0.20
Run Code Online (Sandbox Code Playgroud)
或awk用substr():
$ awk '{print substr($0,55,6)}' file
0.26
0.20
Run Code Online (Sandbox Code Playgroud)