Solved

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

Posted on 2001-08-26
4
42,271 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Creating Labels and Frequency list style reports in SAS code 9 130
has22 challenge 11 111
Making an alias 7 121
Bartender label printing - switch on and off graphics 3 94
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Suggested Courses

751 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