Trying to replace a pattern inside text lines with a pattern found at the very beginning of the same line.

Having some text:

number4456 reports a high level of error
number3458 taking a proper error warning
number9273 is not an error prone software

Desired output:

reports a high level of error_number4456
taking a proper error_number3458 warning
is not an error_number9273 prone software

With sed:

LC_ALL=C sed -e 's/^\([^ ]*\) \(.* error\)/\2_\1/' <file

assuming that there's only one error occurs in a line.

awk '{a = "";for(i=2;i<=NF;i++){if($i ~ /error/)a = a$i"_"$1" ";else a = a$i" "}print a}' yourfile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.