Operating System - HP-UX
1755409 Members
4177 Online
108832 Solutions
New Discussion юеВ

ksh? inserting line into files

 
SOLVED
Go to solution
Florian Heigl (new acc)
Honored Contributor

ksh? inserting line into files

Hi,

I know this is an easy one, and will just say that I caught a flu which makes my head ache a bit too much.

I need to insert a css reference into a LOT of html files.

so far I've thought about
mv foo.html foo.html.bak
while read line < foo.html.bak
do
echo $line >> foo.html
if [ echo $line | grep '' 2>dev/null 1>/dev/null ]
then
echo "$CSS_THINGY" >>foo.html
fi
done


but somehow I get the feeling that this might
blow up or just be a very strange way of doing it.

I'd love some recommendations!
yesterday I stood at the edge. Today I'm one step ahead.
9 REPLIES 9
Denver Osborn
Honored Contributor

Re: ksh? inserting line into files

Permissions of the new foo.html may not match the original... instead of mv, how about cp then null the original file?

cp foo.html foo.html.bak
> foo.html
while read line < foo.html.bak
do
echo $line >> foo.html
if [ echo $line | grep '' 2>dev/null 1>/dev/null ]
then
echo "$CSS_THINGY" >>foo.html
fi
done


-denver
Florian Heigl (new acc)
Honored Contributor

Re: ksh? inserting line into files

okay :)

I chose mv because it's not updating the original file at all, but forgot about the new file.
then a
cp -p foo.html.bak foo.html and a
> foo.html
would be needed to keep everything in sync.

also, I noticed the if clause must read
if [` echo $line | grep '' 2>dev/null 1>/dev/null `]

I'll try it later, right now I'm just to dizzy.
yesterday I stood at the edge. Today I'm one step ahead.
Denver Osborn
Honored Contributor

Re: ksh? inserting line into files

same here, it's past my bedtime. I didn't bother to test it before posting... just did a cut/paste to point out cp -vs- mv.

I'm sure one of the perl gurus will wake up and solve this one w/ a few lines of perl script for you.

-denver
Florian Heigl (new acc)
Honored Contributor

Re: ksh? inserting line into files

I knew I shouldn't trust myself :)

-su[4]: dev/null: cannot create [No such file or directory]
-su[4]: []: not found [No such file or directory]
-su[4]: dev/null: cannot create [No such file or directory]
-su[4]: []: not found [No such file or directory]
-su[4]: dev/null: cannot create [No such file or directory]
-su[4]: []: not found [No such file or directory]
-su[4]: dev/null: cannot create [No such file or directory]
-su[4]: []: not found [No such file or directory]
-su[4]: dev/null: cannot create [No such file or directory]

(see You in 12 hrs :)
yesterday I stood at the edge. Today I'm one step ahead.
Biswajit Tripathy
Honored Contributor

Re: ksh? inserting line into files

Replace :
--------
if [ echo $line | grep '' 2>dev/null 1>/dev/null ]
then
.... ....
-----------

with :

-------------
echo $line | grep -q ''
if [ $? -eq 0 ]
then
...... ....
-----------

BTW, the error messages printed by your script was
bacause of typo ('dev/null' instead of '/dev/null')

- Biswajit
:-)
Hein van den Heuvel
Honored Contributor
Solution

Re: ksh? inserting line into files



required perl alternative:

perl -i -ne 'print; print "css\n" if (/<\/title/)' x.html


If you'd need a line before a marker it reduces to:

perl -i -pe 'print "css\n" if (/<\/title/)' x.html

The "-i" creates a temp file. The alternative is the old
perl -ne 'print; print "css\n" if (/<\/title/)' < x.bak > x.html

you can pull in an shell symbol through $ENV{name}

perl -i -ne 'print; print "$ENV{\"XX\"}\n" if (/<\/title/)' x.tmp

and one of many awk solutions:

awk 'BEGIN {css=ENVIRON["CSS_THINGY"]}{print}/<\/title>/{print css}' x.tmp


CHeers,
Hein.





Michael Schulte zur Sur
Honored Contributor

Re: ksh? inserting line into files

Hi,

2>dev/null 1>/dev/null
There is a / missing hence the errors.

Secondly, if you run a faulty script twice, you loose your original.

what about
awk '/<\/title>/ {print $0;print "CSS_THINGY";next;}{print $0}' foo.html

greetings,

Michael
Florian Heigl (new acc)
Honored Contributor

Re: ksh? inserting line into files

Biswajit - thanks for the grep -q :)

And Hein - thank You for trying to build something more reasonable than that 'late night dilemma' I made - I chose Your awk solution, it takes well below a second for 100 files, which is very acceptable!

Michael - You've been right about the fact that I'd lose the originals by every second run. It was no issue because the files will be regenerated quite often but it's well worth noting.
yesterday I stood at the edge. Today I'm one step ahead.
Florian Heigl (new acc)
Honored Contributor

Re: ksh? inserting line into files

see last posting :)
yesterday I stood at the edge. Today I'm one step ahead.