-1

I'm trying to find a multiline pattern within a file and either add 2 line after it or replace it.

This is what the input and output should look like.

INPUT

$ DATE: Fri Apr 20 16:36:56 2012
$---------------------------$
$ FILE MANAGEMENT SECTION   $
$---------------------------$
$
$---------------------------$
$ EXECUTIVE CONTROL SECTION $
$---------------------------$

OUTPUT

$ DATE: Fri Apr 20 16:36:56 2012
$---------------------------$
$ FILE MANAGEMENT SECTION   $
$---------------------------$
$
BLKABLA
$
$---------------------------$
$ EXECUTIVE CONTROL SECTION $
$---------------------------$

Thanks

4 Answers4

1
awk '1;/^\$$/{print "BLKABLA\n$"}' file

One way to do it just the first time the pattern is seen:

awk '1;/^\$$/ && !done{print "BLKABLA\n$"; done=1}' file
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
0

As this is Linux, you probably have GNU sed. Try:

sed -e '/^\$ FILE MANAGEMENT SECTION   \$$/,+2{/^\$-\+\$/,+1{/^\$$/afoo\
$
}
}' input

It matches on the three regexps

^\$ FILE MANAGEMENT SECTION   \$$
^\$-\+\$$
^\$$

Then appends

foo
$
kmkaplan
  • 18,655
  • 4
  • 51
  • 65
0

You can also use the holding buffer and read the whole file into it first and then apply the regular expression across the whole file. I described the solution already here - multiline sed using backreferences

Community
  • 1
  • 1
Hardy
  • 18,659
  • 3
  • 49
  • 65
0

This might work for you (GNU sed):

sed '/^$ FILE MANAGEMENT SECTION   $$/!b;n;/^$--*$$/a$\nBLKABLA' file

or

sed '/^$ FILE MANAGEMENT SECTION   $$/!b;n;$!N;/^$--*$\n$$/aBLKABLA\n$' file
potong
  • 55,640
  • 6
  • 51
  • 83