- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- update several files using sed
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
Discussions
Discussions
Forums
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
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
тАО02-08-2008 09:09 AM
тАО02-08-2008 09:09 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-08-2008 09:22 AM
тАО02-08-2008 09:22 AM
SolutionWell 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-08-2008 09:33 AM
тАО02-08-2008 09:33 AM
Re: update several files using sed
I have to insert the data after the first line as these files are scripts so after
#!/bin/ksh
I will have a look
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-08-2008 10:09 AM
тАО02-08-2008 10:09 AM
Re: update several files using sed
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...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-08-2008 12:02 PM
тАО02-08-2008 12:02 PM
Re: update several files using sed
> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-08-2008 05:04 PM
тАО02-08-2008 05:04 PM
Re: update several files using sed
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
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-11-2008 07:09 AM
тАО02-11-2008 07:09 AM
Re: update several files using sed
I used James' solution however I can now adopt the other solutions to other tasks.
Chris.