0

I need to add a line of text after each occurence of a pattern. I need this:

pattern1
pattern2

To look like this

pattern1
pattern2
new line data here

The new line data will always come after pattern2. pattern2 will always be the same and I have well over 100 occurences of patter2. And I only want to do this when I seen pattern 2 between lines 226 and 2858. I would like to do an in-place modification and do all this without wiping out anything else in the file. (already did that once while testing, don't want to do it again)

UPDATE - I need to be more precise as my patterns contain special characters. So I'm actually changing this:

pattern1
pattern2 { text; };

to this

pattern1
pattern2 { text; };
new data { text; text; text; };

So matching on pattern 2 that contains special characters and adding the new data line with the special characters. Should have disclosed that before. Sorry for any confusion.

3
  • 1
    So, how does pattern come into play? Commented Aug 29, 2016 at 17:55
  • Does it have to be sed or awk, or would using a different tool be acceptable?
    – DJMcMayhem
    Commented Aug 29, 2016 at 18:09
  • Does not have to be sed or awk just was the first thing to come to mind.
    – user53029
    Commented Aug 29, 2016 at 18:11

2 Answers 2

2

You can do this with vim!

vim file.txt -c "226,2858g/pattern2/normal onew line data here" -c "wq"

You can also do this interactively by doing this:

vim file.txt
:226,2858g/pattern2/normal onew line data here
:wq

Which is the way I would prefer to do it, but I would understand if you prefer the first, being unfamiliar with vim.

The way both of these work is by calling vim's :global command:

                        *:g* *:global* *E147* *E148*
:[range]g[lobal]/{pattern}/[cmd]
            Execute the Ex command [cmd] (default ":p") on the
            lines within [range] where {pattern} matches.

In this case, the range is lines 226-2858, the pattern is "pattern2", and the ex command is

normal onew line data here

What this does is it calls "normal" which means "Enter the following keys as if I had typed them". Typing the 'o' key (o)pens a new line below the current one, and enters insert mode. Then, while in insert mode, the text "new line data here" is inserted into the file.

The next command -c "wq" is just (w)rite and (q)uit.

If your "pattern" uses regex, you should probably read through this wonderful page to help you troubleshoot if the pattern is matching correctly.

3
  • explain "normal onew" please
    – user53029
    Commented Aug 29, 2016 at 18:59
  • @user53029 Sure! See my edit. Does that make it more clear?
    – DJMcMayhem
    Commented Aug 29, 2016 at 19:05
  • Much better thanks. I think I can work with this.
    – user53029
    Commented Aug 29, 2016 at 19:08
0
sed "226,2858s/^\(pattern2.*\)/\1\nnew line data here/g" -i data.txt
1
  • This did not work. It may have been due to special characters. See my updated question.
    – user53029
    Commented Aug 29, 2016 at 18:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .