Solved

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

Posted on 2001-08-26
4
42,264 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
Comment Utility
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
Comment Utility

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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now