AWK截取字符串
文件file的每一行的格式如下:
600001 200712 SH 20080413
希望吧格式调整为:
600001 2007 12 2008 04 13
可以实用以下shell命令:
cat file | awk '{print $1 "\t" substr($2,1,4) "\t" substr($2,5,2) "\t" substr($4,1,4) "\t" substr($4,5,2) "\t" substr($4,7,2)}' > newfile
Awk substr function
Let's look at the substr function of awk.
This function has the form substr(s, a, b) and returns b number of chars from string s, starting at position a. The parameter b is optional.
Assume that we have a one line file called temp:
Every good boy.
Then, here are some substr examples:
nawk '{print substr($1,1,1)}' temp returns E
nawk '{print substr($1,3)}' temp returns ery
nawk '{print substr($2,3)}' temp returns od
nawk '{print substr($0,7,2)}' temp returns go