- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- problem with sed 'ing a section of text
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 01:38 AM
10-12-2001 01:38 AM
refering to the script, the
for LINE in .. cmd works fine without the for.
That is, I get the right output from the sed command until it goes into the for statement.
The for LINE seems to look for white spaces and delimit further..
see op of script
# ./sed.scr ABC 5 /etc/opt/ABC/config/abc.conf "#"|more
---------
[ABC]
RunDirectory = /opt/ABC/bin
RunString = /opt/ABC/bin/start
ProcessType = RS
RestartDelay = 15
---------
# [ABC]
# RunDirectory
# =
# /opt/ABC/bin
# RunString
# =
# /opt/ABC/bin/start
# ProcessType
# =
# RS
# RestartDelay
# =
# 15
and script:
#!/usr/bin/ksh
if [ $# -lt 3 ];
then
echo "\n================================================================"
echo " ERROR: Cannot continue."
echo " You must supply a search string,number of lines and file:"
echo " Example: "
echo " $0 \"ABC\" 5 /etc/opt/ABC/config/abc.conf \"insert\" "
echo "================================================================\n"
exit 1
fi
SEA=$1
LEN=$2
FIL=$3
INS=$4
/usr/bin/sed -n '/'$SEA'/,$p' $FIL |sed -n '1,'$LEN'p' > /tmp/temp
echo "---------"
cat /tmp/temp
echo "---------"
if [ $# = 4 ]; then
for LINE in $(cat /tmp/temp)
do
CHANGETO="$INS $LINE"
echo $CHANGETO
done
elif [ ! $4 = "" ]; then
echo "$4"
fi
echo "do the move...."
rm /tmp/temp
Thanks for any help to get the op looking like this:
# [ABC]
# RunDirectory = /opt/ABC/bin
# RunString= /opt/ABC/bin/start
# ProcessType = RS
# RestartDelay = 15
Later,
Bill
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2001 01:54 AM
10-12-2001 01:54 AM
Re: problem with sed 'ing a section of text
What I need is something that'll take line
then I need to replace the line in the original.
Bill
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2001 02:07 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2001 02:09 AM
10-12-2001 02:09 AM
Re: problem with sed 'ing a section of text
Try:
cat /tmp/temp | while read LINE
do
...
done
HtH
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2001 02:25 AM
10-12-2001 02:25 AM
Re: problem with sed 'ing a section of text
sed that will do the replacement now only replaces the last occurance and not then entire lot.
cat /tmp/temp | while read LINE
do
CHANGETO="$INS $LINE"
echo $CHANGETO >> /tmp/temp.out
sed "s/$LINE/$CHANGETO/g" $FIL > ${FIL}.bak
done
# cat /tmp/temp.out
# [ABC]
# RunDirectory = /opt/ABC/bin
# RunString = /opt/ABC/bin/start
# ProcessType = RS
# RestartDelay = 15
the only line replaced with the sed is
# RestartDelay = 15
Later,
Bill
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2001 02:52 AM
10-12-2001 02:52 AM
Re: problem with sed 'ing a section of text
The sed does the same input file all the time.. so that's normal.. but now I'm having trouble trying to find a way to do this right..
Later,
Bill
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2001 02:59 AM
10-12-2001 02:59 AM
Re: problem with sed 'ing a section of text
cat filename | while read LINE
do
echo "$INS $LINE" >> ${FILE}.bak
done
HtH
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2001 03:02 AM
10-12-2001 03:02 AM
Re: problem with sed 'ing a section of text
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2001 03:10 AM
10-12-2001 03:10 AM
Re: problem with sed 'ing a section of text
You have a single redirect ">" within your loop instead of ">>", and so you'll only ever get the last line written to the .bak file.
Rgds, Robin.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2001 03:12 AM
10-12-2001 03:12 AM
Re: problem with sed 'ing a section of text
do
TEMP="${FIL}.tmp"
echo $LINE > $TEMP
sed s"/$LINE/$CHANGETO/g" ${TEMP >>FIL.bak
rm $TEMP
done
HtH
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2001 03:23 AM
10-12-2001 03:23 AM
Re: problem with sed 'ing a section of text
I end up with an output that
has the input file $FIL
cat'd $2 times with each occurance of $FIL having just one change.. the current line the while was on.
# more /tmp/abc.conf
[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
ie:
[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
[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
[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
[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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2001 03:35 AM
10-12-2001 03:35 AM
Re: problem with sed 'ing a section of text
---------
[ABC]
RunDirectory = /opt/ABC/bin
RunString = /opt/ABC/bin/startABC
ProcessType = RS
RestartDelay = 15
---------
[# [ABC]# [ABC]# [ABC]]
# RunDirectory = /opt/ABC/bin
RunString = /opt/ABC/bin/startABC
ProcessType = RS
# RestartDelay = 15
kibo(root) /
#!/usr/bin/ksh
if [ $# -lt 4 ];
then
echo "\n================================================================"
echo " ERROR: Cannot continue."
echo " You must supply a search string,number of lines and file:"
echo " Example: "
echo " $0 \"ABC\" 5 /etc/opt/ABC/config/abc.conf \"#\" "
echo "================================================================\n"
exit 1
fi
SEA=$1
LEN=$2
FIL=$3
INS=$4
export LINES=1
/usr/bin/sed -n '/'$SEA'/,$p' $FIL |sed -n '1,'$LEN'p' > /tmp/temp
echo "---------"
cat /tmp/temp
echo "---------"
if [ $# = 4 ]; then
cat /tmp/temp | while read LINE
do
TEMP="${FIL}.tmp"
echo $LINE > $TEMP
CHANGETO="$INS $LINE"
sed "s:$LINE:$CHANGETO:g" ${TEMP} >> ${FIL}.bak
rm $TEMP
done
fi
rm /tmp/temp
cat ${FIL}.bak
rm ${FIL}.bak
# cat /tmp/abc.conf
[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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2001 03:55 AM
10-12-2001 03:55 AM
Re: problem with sed 'ing a section of text
Maybe easier:
sed "/^/$INS /" $FILE >$FILE.bak
without the while loop
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2001 04:08 AM
10-12-2001 04:08 AM
Re: problem with sed 'ing a section of text
?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2001 04:37 AM
10-12-2001 04:37 AM
Re: problem with sed 'ing a section of text
Change the central loop to:
cat /tmp/temp | while read LINE
do
TEMP="${FIL}.tmp"
echo $LINE > $TEMP
CHANGETO="$INS $LINE"
echo $LINE | sed 's/\[/\\\\[/g' | read LINE
sed "s:$LINE:$CHANGETO:g" ${TEMP} >> ${FIL}.bak
rm $TEMP
done
fi
The problem is the leading "[" of [ABC] is causing [ABC] to be interpreted as "one of A,B or C", so you need to escape the "[" in the search string.
Rgds, Robin.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2001 04:45 AM
10-12-2001 04:45 AM
Re: problem with sed 'ing a section of text
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2001 06:41 AM
10-12-2001 06:41 AM
Re: problem with sed 'ing a section of text
cat $FIL |
awk '
/'$SEA'/ { occurance++;
if ( occurance <= '$LEN' ) {
print "'$INS'" " " $0;
next;
}
}
{print $0}' > $FIL.bak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2001 07:33 AM
10-12-2001 07:33 AM
Re: problem with sed 'ing a section of text
ie the first sed works to find the search string plus $2 lines after and the second sed is meant to replace those lines not add them on to the end.
The awk was very close, although seems to be a bug..
see output:
# cat /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
modified with awk in place of loop:
# [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
It works fine up to 3 lines!?
Later,
Bill
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2001 07:52 AM
10-12-2001 07:52 AM
Re: problem with sed 'ing a section of text
But the SEA search string is used to find $LEN lines after the first SEA occurance.
so occurance needs to be calculated on LEN..
Bill
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2001 08:03 AM
10-12-2001 08:03 AM
Re: problem with sed 'ing a section of text
but my results for file:
[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
with the script:
#!/usr/bin/ksh
FIL=./f2
SEA=ABC
LEN=5
INS=#
cat $FIL |
awk '
/'$SEA'/ { occurance++;
if ( occurance <= '$LEN' ) {
print "'$INS'" " " $0;
next;
}
}
{ print $0;} ' > $FIL.bak
results in:
[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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2001 08:07 AM
10-12-2001 08:07 AM
Re: problem with sed 'ing a section of text
SEA=$1
LEN=$2
FIL=$3
INS=$4
/usr/bin/sed -n '/'$SEA'/,$p' $FIL |sed -n '1,'$LEN'p' > /tmp/temp
if [ $# = 4 ]; then
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
fi