Mar 102009

In a world of Perl, sed seems to be an archaic method of doing things reserved to the real brave geeks!

But sometimes sed does perfectly fantastic job, like it did for me few minutes ago… with some caveats.

My goal was to replace $i with the value of the variable, determined by the first part of the script:

for i in `seq 1 50`;

the problem was that I made rest of the cycle as follows:

do cat newtest.xml|sed –e ‘s/variable/$i/’ >> multifirewallimport.xml; done

and the result was that through all my file the word variable was replaced with the word $i.

What was wrong? Well… Googling for it I discovered the issue was in usage of single quote instead of double quote.

The correct form is:

for i in `seq 1 50`; do cat newtest.xml|sed -e "s/variable/$i/" >> multifirewallimport.xml; done

Feb 032009

You might need to do something with a list of files contained in a file… and maybe these file names include spaces, therefore using a standard

for i in `cat filename`
echo $i

does not work since it will consider every word as a single variable.

This second script does the job:

IFS=\$    ==> this sets the line separator as EOL
while read i    ==> this read variables separated by EOL and stores value in $i
echo $i    ==> this echoes the value of $i. Replace echo command with whatever you want to do with filenames
done < file-containing-the-list    ==> this ends the while do cycle and takes input from file where filenames are stored