Operating System - HP-UX
1833780 Members
2688 Online
110063 Solutions
New Discussion

Re: sed'ing or greping until blank line

 
SOLVED
Go to solution
Bill McNAMARA_1
Honored Contributor

sed'ing or greping until blank line

I use the following to get input for a script.

/usr/bin/sed -n '/'$1'/,$p' $3 |sed -n '1,'$2'p' > /tmp/temp

where $1 is the search string and $2 is the number of lines.

if I have an input file $3 like follows:

[XYZ]
hello = bonjour

[ABC]
RunDirectory = /opt/ABC/bin
RunString =/opt/ABC/bin/startABC
ProcessType = RS
RestartDelay = 15
Resources = DISK+MEM+LAN
StdoutFile = /tmp/ABC.out
StderrFile = /tmp/ABC.err
AutoStart = Yes
PreferredProcessor = 0

[BOB]
bob = robert

and search for ABC without specifying how many lines after I want the output file for the sed to include all the content up to the blank line.

I could use grep -n ABC in this case, but that won't work if there is an ABC1 and ABC2 so I'm hoping the sed line can be modified easily.

Thanks!
Bill
It works for me (tm)
10 REPLIES 10
Bill McNAMARA_1
Honored Contributor

Re: sed'ing or greping until blank line

to add if I do the following

/usr/bin/sed -n '/'$1'/,$p' $3 > /tmp/temp

I get
# /sed.scr "ABC" /tmp/abc.conf "#"
[XYZ]
hello = bonjour

#[ABC]
#RunDirectory = /opt/ABC/bin
#RunString = /opt/ABC/bin/startABC
#ProcessType = RS
#RestartDelay = 15
#Resources = DISK+MEM+LAN
#StdoutFile = /tmp/ABC.out
#StderrFile = /tmp/ABC.err
#AutoStart = Yes
#PreferredProcessor = 0

#[BOB]
#bob = robert

and I don't want the [BOB] bit commented, just the [ABC] bit. ie. just up to the blank line
It works for me (tm)
James R. Ferguson
Acclaimed Contributor
Solution

Re: sed'ing or greping until blank line

Hi Bill:

How about this:

# sed -n '/ABC/,/^$/p' myfile

...This will output everything between the two regular expressions, the second of which is a blank line.

Regards!

...JRF...
Robin Wakefield
Honored Contributor

Re: sed'ing or greping until blank line

Hi Bill,

Try:

/usr/bin/sed -n '/'ABC'/,/^$/p' $3 > /tmp/temp

Rgds, Robin
Bill McNAMARA_1
Honored Contributor

Re: sed'ing or greping until blank line

okay

here's my script again..
SEA=$1
# LEN=$2
FIL=$2
INS=$3
cp $FIL ${FIL}.orig

/usr/bin/sed -n '/'$SEA'/,/^$/p' $FIL > /tmp/temp

cat /tmp/temp | while read LINE
do
CHANGETO="${INS}${LINE}"
echo $LINE | sed 's/\[/\\\\[/g' | read LINE
sed "s:$LINE:$CHANGETO:g" ${FIL} > ${FIL}.bak
mv ${FIL}.bak $FIL
done

diff ${FIL} ${FIL}.orig
echo "file modified: backup at ${FIL}.orig"
rm /tmp/temp

Now I get a problem if the input file looks like this:

[XYZ]
hello = bonjour
PreferredProcessor = 0

[LMN]
hello = bonjour
PreferredProcessor = 1

[ABC]
RunDirectory = /opt/ABC/bin
RunString = /opt/ABC/bin/startABC
ProcessType = RS
RestartDelay = 15
Resources = DISK+MEM+LAN
StdoutFile = /tmp/ABC.out
StderrFile = /tmp/ABC.err
AutoStart = Yes
PreferredProcessor = 0

[BOB]
bob = robert

and I run the script to attempt to comment out the ABC section:

[XYZ]
hello = bonjour
#PreferredProcessor = 0

[LMN]
hello = bonjour
PreferredProcessor = 1

#[ABC]
#RunDirectory = /opt/ABC/bin
#RunString = /opt/ABC/bin/startABC
#ProcessType = RS
#RestartDelay = 15
#Resources = DISK+MEM+LAN
#StdoutFile = /tmp/ABC.out
#StderrFile = /tmp/ABC.err
#AutoStart = Yes
PreferredProcessor = 0

[BOB]
bob = robert


Any way around this!

Thanks,
Bill
It works for me (tm)
James R. Ferguson
Acclaimed Contributor

Re: sed'ing or greping until blank line

Hi (again) Bill:

OK. I prefer 'awk', if I understand your objective. Using your file:

#!/usr/bin/sh
#
SEA=ABC #...or SEA=$1
INP=/tmp/stuff #...or INP=$2
OUT=/tmp/stuff.out #...or OUT=$3
#
awk -v SEA=$SEA '$0~SEA {TOG=1}
/^$/ {TOG=0}
{if (TOG==1) {print "#" $0} else {print $0}}' $INP > $OUT
#.end.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: sed'ing or greping until blank line

Hi (again) Bill:

OK, here's a shorter piece of code that I believe accomplishes your objective. It's an alternative to my 'awk' script so you can take your choice. By example, using your data:

# sed -n -e '/ABC/,/^$/ s/^/\#/g' -e 'p' /tmp/stuff > /tmp/stuff.out

Regards!

...JRF...

Regards!

...JRF...







Sridhar Bhaskarla
Honored Contributor

Re: sed'ing or greping until blank line

Bill,

Doesn't this work for you?

sed '/abc/,/^$/s/^/#/' your_stuff

-Sri

You may be disappointed if you fail, but you are doomed if you don't try
Sridhar Bhaskarla
Honored Contributor

Re: sed'ing or greping until blank line

Ooops.. Sorry.. plase replace abc with ABC in the above.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Sridhar Bhaskarla
Honored Contributor

Re: sed'ing or greping until blank line

One more ooops... Didn't verify my previous script. It is going to even comment out the
blank (^$) line that doesn't look good We need to exclude it from being commented out.


sed '/ABC/,/^$/ {/^$/!s/^/#/g;}' your_file

is going to comment out all the lines starting
with the line that has ABC till the blank line excluding the blank line.


-Sri

You may be disappointed if you fail, but you are doomed if you don't try
Robin Wakefield
Honored Contributor

Re: sed'ing or greping until blank line

Hi Bill,

How about this variation on a theme...

SEA=$1
FIL=$2
INS=$3
cp $FIL $FIL.orig

sed -e '/'$SEA'/,/^$/ s/^[^$]/'$INS'&/' $FIL > $FIL.bak
mv $FIL.bak $FIL

diff $FIL $FIL.orig
echo "file modified: backup at $FIL.orig"


Rgds, Robin.