Afficher un message
Vieux 21/08/2006, 11h18   #4
Rakesh Sharma
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: awk script to insert blank line between matching patterns


the tool 'sed' normally operates on a line-by-line basis with it's
input. but in your case we need to carry with us two lines all
the time, since we need to base our decision after looking at the
signature line and also the line preceding it (whether it was a blank
or not).

$!N -> will make the pattern space comprise 2 lines separated by
a newline (\n), unless we reach the end of file.

now we need to decide whether the first part (i.e., the part of pattern
space preceding the \n is a blank or not). now, it is a blank line
then we discard any further processing on that part since we are
interested in a non-blank line followed by our signature line.

whence,
/^\n/{P;D;} -> will print the first part (viz., portion preceding the
\n)
and also delete the first part. after the D, we go back to top of
script
in effect read in the next line.

once we jump upto here, that means the first part is a nonblank and we
need further tests to verify whether the next line was a signature
line.

N.B. I didn't know how u look for a signature line. so i just put in
the
regular expression 'signature'.

so we check whether the second portion (viz., portion after the \n)
contains the signature. if it doesn't then just print out the first
portion
and delete that part and go onto read in the next line.

lastly if it matches our condition of a non-blank line followed by
the signature line, *** WHAT WE NEED ***, then we need to insert
a blank line in between these.

s/\n/&&/ -> will insert a blank line.

So we can recast our code as under:

sed -e '
## read in the next line also
$!N

## first part is blank, we dont need this so
## print out first portion n delete that portion
/^\n/{
P
D
}

## first part is blank, but we must test the 2nd part now
## 2nd part doesnot contain signature then print 1st part n delete
it
/\n.*signature/!{
P
D
}

## if we are able to reach here then that means first part was a
nonblank
## and 2nd part had a signature. SO WE MUST INSERT A BLANK HERE
s/\n/&&/
' yourfile

************************************************** ************************************************** ********

  Réponse avec citation
 
Page generated in 0,05749 seconds with 9 queries