试试这一行:
awk '$4=="B34"' file
您的休息命令(sed,cat ...)可以合并到上面的单行中:
awk '$4=="B34"{gsub(/B/,"");print $1,$4,$5}' file
用你的例子测试它:
kent$ echo " B346879 length: 12 B34 L677
B111879 length: 32 B33 L677
B545879 length: 34 B34 L677
B344879 length: 98 B33 L677
B090879 length: 45 B33 L677"|awk '$4=="B34"{gsub(/B/,"");print $1,$4,$5}'
346879 34 L677
545879 34 L677
编辑
awk 将空格作为FS
,因此第三个字段的长度无关紧要,例如:
kent$ echo " B346879 length: 17777777777777777772 B34 L677
B111879 length: 32 B33 L677
B545879 length: 34 B34 L677
B344879 length: 98 B33 L677
B090879 length: 45 B33 L677"|awk '$4=="B34"{gsub(/B/,"");print $1,$4,$5}'
346879 34 L677
545879 34 L677
编辑
好的,看看你的意思,所以这应该工作:
awk -F'length:[ 0-9]*' '$2~/^B34/{sub(/B/,"",$1);sub(/B/,"",$2);print $1,$2}'
见测试:第一行是特殊情况。
kent$ echo " B346879 length:212 B34 L677
B111879 length: 32 B33 L677
B545879 length: 34 B34 L677
B344879 length: 98 B33 L677
B090879 length: 45 B33 L677"|awk -F'length:[ 0-9]*' '$2~/^B34/{sub(/B/,"",$1);sub(/B/,"",$2);print $1,$2}'
346879 34 L677
545879 34 L677