Click to See Complete Forum and Search --> : BASH question
alexdg
08-01-2001, 02:06 PM
i need to insert a few lines of text in the middle of a file, usually i would just use patch for that, but the text must be insterted *before* a certain line, not after, and as far as i know patch won't do it..
anyhow,
for str in `cat ./file`; do
if [ "$str" = "</mytag>" ]; then
echo "lah" >> ./newfile
echo "</mytag>" >> ./newfile
else
echo "$str" >> ./newfile
fi
done
unfortunately, str is being assigned each word in the file, instead of each line
so, is there a way to go through file line by line?
and no, no perl allowed :)
thnx
You might me able to use the sed command. It can process files line by line. Inserting a newline in a substitution is annoying, but it should get the job done.
Take a look at this sed faq (http://www.cornerstonemag.com/sed/sedfaq.html), particularly section 4.6 (part e).
To summarize, the following worked on our Solaris machine at work:
sed -e "/<mytag>/{x;s/.*/lah/;G;}" test.file
but it makes some assumptions. The biggest is that <mytag> only APPEARS ON lines you want to change. If you have "somestuff <mytag> somemorestuff" on other lines this won't work.
Hope this helps some, I'm not too familar with sed. I'd use perl, but you said no perl.
[ 01 August 2001: Message edited by: feve ]
TheLinuxDuck
08-01-2001, 05:54 PM
Originally posted by feve:
<STRONG> I'd use perl, but you said no perl.
</STRONG>
LOL! That was EXACTLY what I thought, until I saw the last part of the message.
Hee hee!
Why not perl, out of curiousity? This is one of the many things that perl was designed
for!
(^=
alexdg
08-01-2001, 06:05 PM
feve, thnx... i figured it before i noticed you have added an example to your reply :) now i'm trying to figure out how to add multiple lines using sed ;)
no perl is simply because i'm targeting my scripts for a totally naked installation
alexdg
08-01-2001, 06:07 PM
ok... i got that part as well... thnx everybody :)
The Kooman
08-02-2001, 02:24 AM
#!/bin/bash
# Note, its "<newline>", no space after the newline
IFS="
"
for str in `cat myfile`
do
if [ "$str" = "</mytag>" ]
then
echo "lah" >> ./newfile
fi
echo "$str" >> ./newfile
done
Did it work?
[ 02 August 2001: Message edited by: dchkoo ]
alexdg
08-02-2001, 10:41 AM
#!/bin/bash
# Note, its "<newline>", no space after the newline
IFS="
"
for str in `cat myfile`
do
if [ "$str" = "</mytag>" ]
then
echo "lah" >> ./newfile
fi
echo "$str" >> ./newfile
done
Did it work?
[ 02 August 2001: Message edited by: dchkoo ][/qb]<HR></BLOCKQUOTE>
almost! it now reading full lines, but for some reason it's omitting some empty lines... kinda weird
[ 02 August 2001: Message edited by: alexdg ]
The Kooman
08-02-2001, 11:35 PM
Originally posted by alexdg:
<STRONG>almost! it now reading full lines, but for some reason it's omitting some empty lines... kinda weird</STRONG>
Aaah yes, that will happen - I overlooked that!
It happens because I've used the newline to seperate fields now instead of the conventional space-or-tab-or-newline! Bash recongnises consecutive occurences separator characters as a single seperator and hence a blank line (which has only a newline - the seperator character in this case) will go for a toss! Perhaps if your "blank" line wasn't blank but had a space character!!!!!
I don't see how I can make work otherwise!
Perhaps this script can still be useful in other situations!!! Sorry for the false lead!
alexdg
08-02-2001, 11:43 PM
oh, no worries, i'll keep that script handy for sure, and for now i'm using sed :) thnx
[ 02 August 2001: Message edited by: alexdg ]