Solved

sed....replace  a string and write it to file

Posted on 2001-08-26
4
42,267 Views
Last Modified: 2013-12-26
Hi...
I have a whole bunch of xml files that i want to make a change in.....
I want to replace the FIRST occurance of the word Update with the word Insert. I have about 20,000 files, and I want to overtwrite the in files with the update.

 for i in `ls 1 2 3 4 5 6 7 8`; do sed -e s/Update/Insert/g $i > $i

seenms to wipe out the files.


thanks in advance

.....R
0
Comment
Question by:rickyr
4 Comments
 
LVL 4

Accepted Solution

by:
newmang earned 50 total points
ID: 6427239
You can do this by running a script along the following lines....

do
cat $i | sed 's/Update/Insert/' > $i.tmp
mv $1.tmp $i
done

BUT this will change EVERY occurrence of Update not just the first one.

Personally it seems to me that it would be easier to knock up a quick C program to do the job.

Cheers - Gavin
0
 
LVL 8

Expert Comment

by:shlomoy
ID: 6427615

update2insert.pl
================
#!/usr/bin/perl -w
use strict;
my $file_content = `cat $ARGV[0]`;
$file_content =~ s/Update/Insert/s;
print $file_content;



your commandline (tcsh):
========================
foreach file (`find 1 2 3 4 5 6 7 8 -type f`)
     perl update2insert.pl $file > tmp.$file
     mv tmp.$file $file
end

     
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6428037
do you mean "First occourance" per line, or per file?
Do you insist on sed (awk, perl might be better)?
0
 
LVL 3

Author Comment

by:rickyr
ID: 6429520
This is close enough, I actually did...
(ksh)
for i in `ls`
do
sed -e s/Update/Insert/g $i >> ../tempdir/$i
done

Thanks

....R
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question