Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

learn linux and unix commands - unix shell scripting

need help on sed (replace string without changing filename)

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 10-21-2004   -   Original Discussion by jjoves
jjoves's Unix or Linux Image
jjoves jjoves is offline
Registered User
 
Join Date: Aug 2004
Last Activity: 22 April 2005, 3:26 AM EDT
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
need help on sed (replace string without changing filename)

I have awhole bunch of files and I want to edit stringA with stringB without changing the filename.

I have tried the following sed commands:

sed "s/stringA/stringB/g" *

This will print the correct results but does not actually save it with the new content of the file. when I do a cat on the filename it still shows stringA.

THANKS
Sponsored Links
    #2  
Old Unix and Linux 10-21-2004   -   Original Discussion by jjoves
zazzybob's Unix or Linux Image
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
 
Join Date: Dec 2003
Last Activity: 2 October 2017, 12:19 PM EDT
Location: /dev/null
Posts: 2,187
Thanks: 3
Thanked 25 Times in 23 Posts
That's because sed (by default) doesn't change the contents of the file in place.

Do something like


Code:
for file in *
do
  sed 's/stringA/stringB/g' $file > $file.new
  mv $file.new $file
done

Cheers
ZB
Sponsored Links
    #3  
Old Unix and Linux 10-21-2004   -   Original Discussion by jjoves
jjoves's Unix or Linux Image
jjoves jjoves is offline
Registered User
 
Join Date: Aug 2004
Last Activity: 22 April 2005, 3:26 AM EDT
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Im not familiar with script writing - but I tried to create a file with the following code you had then ran

sed -f script

and nothing happen.

how would I execute those script?

THANKS -- I dont normally do this type of work but your help is much appreciated.
    #4  
Old Unix and Linux 10-21-2004   -   Original Discussion by jjoves
zazzybob's Unix or Linux Image
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
 
Join Date: Dec 2003
Last Activity: 2 October 2017, 12:19 PM EDT
Location: /dev/null
Posts: 2,187
Thanks: 3
Thanked 25 Times in 23 Posts
Create a script, let's say "foo.sh" with the following code



Code:
#!/bin/sh

for file in /path/to/my/files/*
do
  sed 's/stringA/stringB/g' $file > $file.new
  mv $file.new $file
done

exit 0

Make the script executable
$ chmod u+x foo.sh

Then run it (assuming it's in the current directory)
$ ./foo.sh

Cheers
ZB
Sponsored Links
    #5  
Old Unix and Linux 10-21-2004   -   Original Discussion by jjoves
jjoves's Unix or Linux Image
jjoves jjoves is offline
Registered User
 
Join Date: Aug 2004
Last Activity: 22 April 2005, 3:26 AM EDT
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
THANK YOU VERY MUCH ZB -- now I know how to write a script....
Sponsored Links
    #6  
Old Unix and Linux 10-24-2004   -   Original Discussion by jjoves
encrypted's Unix or Linux Image
encrypted encrypted is offline Forum Advisor  
Registered User
 
Join Date: Feb 2004
Last Activity: 19 November 2015, 4:17 AM EST
Location: Oslo, Norway
Posts: 220
Thanks: 0
Thanked 1 Time in 1 Post
I personaly prefer perl for string replacement instead of sed

#/bin/bash
printf "Enter the String to be replaced: "
read ostring
printf "Enter the String to be substituted: "
read rstring
printf "Enter the Path of the files: "
read path
cd $path
files = `ls`
for file in $files
do
perl -p -i -e 's/$ostring/$rstring/' $file
done

Chill
enc;-)
Sponsored Links
Closed


Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Find String in FileName and move the String to new File if not found us_pokiri Linux 1 07-20-2011 04:03 AM
replace multiple patterns in a string/filename kerppz UNIX for Dummies Questions & Answers 4 09-12-2010 05:05 PM
changing filename extension Hiso Shell Programming and Scripting 3 01-24-2009 09:02 AM
Replace text with filename calrog Shell Programming and Scripting 2 01-17-2009 07:45 PM
Parse and replace string in filename chengwei Shell Programming and Scripting 17 09-05-2008 12:06 PM



All times are GMT -4. The time now is 07:34 PM.