'Delete or Replace nth line in a file after a pattern match using shell scripts

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 1:[1]

Easiest way i can think of

delete

awk '/pattern/{x=12}x--!=1' file

'replace'

awk '/pattern/{x=12}x--==1{$0="REPLACEMENT"}1' file

Solution 2:[2]

My take: sed '/msq_Query/,+11 {1,11n;s/1477361456/1234/}' file

explanation:

/msq_Query/,+11 --> take block starting with the line mathing 'msq_Query' and the next 11 lines
{ ... }         --> on that 12-line block execute the commands within curly braces, which are:
1,11n           --> skip lines 1 through 11
s/old/new/      --> in the remainder of the block substitute 'old' with 'new'

I used this file as test:

*msq_Query
{
    { BaconOutput1, 0419000000, 0144567891,
        <
        OIN ,
        TIN ,
        BPARTY
        >
    ,
        <
        >
    , 1477361456, 0}
}
1477361456
*msq_Query
{
    { BaconOutput1, 0419000000, 0144567891,
        <
        OIN ,
        TIN ,
        BPARTY
        >
    ,
        <
        >
    , 1477361456, 0}
}
1477361456

and the sed command turned it into

*msq_Query
{
    { BaconOutput1, 0419000000, 0144567891,
        <
        OIN ,
        TIN ,
        BPARTY
        >
    ,
        <
        >
    , 1234, 0}
}
1477361456
*msq_Query
{
    { BaconOutput1, 0419000000, 0144567891,
        <
        OIN ,
        TIN ,
        BPARTY
        >
    ,
        <
        >
    , 1234, 0}
}
1477361456

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Alexander Stumpf