尝试用在同一行开头找到的模式替换文本行内的模式。
有一些文字:
number4456 reports a high level of error
number3458 taking a proper error warning
number9273 is not an error prone software
期望的输出:
reports a high level of error_number4456
taking a proper error_number3458 warning
is not an error_number9273 prone software
答案1
和sed
:
LC_ALL=C sed -e 's/^\([^ ]*\) \(.* error\)/\2_\1/' <file
error
假设一行中只出现一个。
答案2
awk '{a = "";for(i=2;i<=NF;i++){if($i ~ /error/)a = a$i"_"$1" ";else a = a$i" "}print a}' yourfile