Operating System - Linux
1753278 Members
5434 Online
108792 Solutions
New Discussion юеВ

update several files using sed

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

update several files using sed

Hi

I want to add the following to the top of several files for debugging:

PID=$$
SCRIPT=$(basename $0)
echo "$PID \n $SCRIPT" > /tmp/christemp/log

what would be the best sed solution to update these several files all ending in *.cron

Thanks

Chris.
hello
6 REPLIES 6
Michael Steele_2
Honored Contributor
Solution

Re: update several files using sed

Hi lawrenzo:

Well I think your on the wrong path about sed, which is a line editor and therefore more like vi. But the way to do this is not difficult at all if you're using a temp file, an input file and an output file along with cat and '>>'.

vi a new file and add in :
PID=$$
SCRIPT=$(basename $0)
echo "$PID \n $SCRIPT" > /tmp/christemp/log

And now cat old file >> to the bottom of the new file.

If you have several files to update then a for loop can be added.

Forget about sed.
Support Fatherhood - Stop Family Law
lawrenzo_1
Super Advisor

Re: update several files using sed

ok thanks - I did think about that but thought there may be a more efficient way of doing this.

I have to insert the data after the first line as these files are scripts so after

#!/bin/ksh

I will have a look

hello
James R. Ferguson
Acclaimed Contributor

Re: update several files using sed

Hi Chris:

A little Perl can help you update all of your files in one pass, keeping backup copies too:

# cat .filter
#!/usr/bin/perl -i.old
use strict;
use warnings;

my $insertion = <<'EOF';
PID=$$
SCRIPT=$(basename $0)
echo "$PID \n $SCRIPT" > /tmp/christemp/log
EOF

while (<>) {
print $insertion if $. == 1;
print;
}
continue {
close ARGV if eof;
}
1;

...Now simple run:

# ./filter file1 file2 ...

...for as many file names as you require (one or more!).

In the above example, backup copies of file1 and file2 will be named with the ".old" suffix. The three lines will be added to the top of the original file as requested.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: update several files using sed

Hi (again) Chris:

> I have to insert the data after the first line as these files are scripts so after #!/bin/ksh

OK, I should have assumed that. Use this slightly modified version of what I first provided. The same rules of engagement apply:

# cat .filter
#!/usr/bin/perl -i.old
use strict;
use warnings;

my $insertion = <<'EOF';
PID=$$
SCRIPT=$(basename $0)
echo "$PID \n $SCRIPT" > /tmp/christemp/log
EOF

while (<>) {
if (m{^#!/}) {
print $_, $insertion;
}
else {
print $_;
}
}
continue {
close ARGV if eof;
}
1;

...now run the script passing the filenames to be filtered.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: update several files using sed

If you really want to use sed, it isn't that hard.

for file in *.cron; do
sed -e '
1a\
PID=$$\
SCRIPT=$(basename $0)\
echo "$PID \n $SCRIPT" > /tmp/christemp/log
' $file > $file.new
if [ -s $file.new ]; then # see if ok?
mv $file.new $file
fi
done
lawrenzo_1
Super Advisor

Re: update several files using sed

thanks all for the help ...

I used James' solution however I can now adopt the other solutions to other tasks.

Chris.
hello