Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2001-08-26
4
Medium Priority
?
42,286 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 200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Screencast - Getting to Know the Pipeline

783 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