Solved

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

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

632 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