- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: sed'ing or greping until blank line
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2001 08:23 AM
10-12-2001 08:23 AM
/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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2001 08:43 AM
10-12-2001 08:43 AM
Re: sed'ing or greping until blank line
/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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2001 08:49 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2001 08:50 AM
10-12-2001 08:50 AM
Re: sed'ing or greping until blank line
Try:
/usr/bin/sed -n '/'ABC'/,/^$/p' $3 > /tmp/temp
Rgds, Robin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2001 09:09 AM
10-12-2001 09:09 AM
Re: sed'ing or greping until blank line
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2001 12:04 PM
10-12-2001 12:04 PM
Re: sed'ing or greping until blank line
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2001 05:22 PM
10-13-2001 05:22 PM
Re: sed'ing or greping until blank line
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2001 07:06 PM
10-13-2001 07:06 PM
Re: sed'ing or greping until blank line
Doesn't this work for you?
sed '/abc/,/^$/s/^/#/' your_stuff
-Sri
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2001 07:08 PM
10-13-2001 07:08 PM
Re: sed'ing or greping until blank line
-Sri
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2001 09:36 PM
10-13-2001 09:36 PM
Re: sed'ing or greping until blank line
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2001 11:16 PM
10-14-2001 11:16 PM
Re: sed'ing or greping until blank line
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.