Issue
I need to replace 11th line after the pattern match "msq_Query" in a file, the original text should remain intact. A one-liner sed or awk would be helpful !!
*msq_Query
{
{ BaconOutput1, 0419000000, 0144567891,
<
OIN ,
TIN ,
BPARTY
>
,
<
>
, 1477361456, 0}
}
In the 11th line in the file, number (whihc could be random) should be replaced by 0 OR 11th line should be deleted .
Solution
I think the easiest way to do this would be with awk like this:
awk -v lines=11 'BEGIN { ignore = -1 } /pattern/ { ignore = NR + lines } NR != ignore { print }' filename
Substitute pattern
with your own pattern.
This works as follows:
BEGIN { ignore = -1 } # initialize ignore with -1 so NR will never
# be equal to it by accident
/pattern/ { ignore = NR + lines } # when the pattern is found, set ignore to the
# line we want to ignore
NR != ignore { print } # unless the current line is that line, print
# it.
In order to replace something in the line instead of deleting it, the script can be amended as follows:
awk -v lines=11 'BEGIN { mark = -1 } /pattern/ { mark = NR + lines } NR == mark { sub(/[0- 9]+/, "") } { print }' filename
that is:
BEGIN { mark = -1 } # initialize mark with -1 so NR will never
# be equal to it by accident
/pattern/ { mark = NR + lines } # when the pattern is found, set mark to the
# line we want to ignore
NR == mark { sub(/[0-9]+/, "0"); } # if that line is the current line, substitute
# stuff (stuff can be freely chosen)
{ print } # print all the lines.
Answered By - Wintermute Answer Checked By - Marilyn (WPSolving Volunteer)