need help on sed (replace string without changing filename)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need help on sed (replace string without changing filename)
# 1  
Old 10-21-2004
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
# 2  
Old 10-21-2004
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
# 3  
Old 10-21-2004
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 10-21-2004
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
# 5  
Old 10-21-2004
THANK YOU VERY MUCH ZB -- now I know how to write a script....
# 6  
Old 10-24-2004
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;-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string in XML file with awk/sed with string from another

Sorry for the long/weird title but I'm stuck on a problem I have. I have this XML file: </member> <member> <name>TransactionID</name> <value><string>123456789123456</string></value> </member> <member> <name>Number</name> ... (9 Replies)
Discussion started by: cozzin
9 Replies

2. UNIX for Dummies Questions & Answers

Sed- Replace space in filename by a \

`echo $file | sed 's/ / /g'` Hey guys I want help in converting the spaces in my file names to '\ ' . Example: UK maps --> UK\ maps Could someone please help me. I have tried the following sequences already (none of them work): 1)s/ /\ /g 2)s/ /\\ /g 3)s/ /\\\ /g Can someone... (7 Replies)
Discussion started by: INNSAV1
7 Replies

3. Shell Programming and Scripting

sed to replace pattern with filename

Hi all, I'm trying to replace a pattern/string in about 100 files with the filename using following commands but getting nowhere: for f in *.fa; do sed "s/^>.*/>$f/g" $f > $f_v1.fa; done for f in *.fa; do sed 's/^>.*/>`echo $f`/' > $fa_v1.fa; done Basically I want to change any line... (5 Replies)
Discussion started by: ivpz
5 Replies

4. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

5. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

6. UNIX for Dummies Questions & Answers

replace multiple patterns in a string/filename

This should be somewhat simple, but I need some help with this one. I have a bunch of files with tags on the end like so... Filename {tag1}.ext Filename2 {tag1} {tag2}.ext I want to hold in a variable just the filename with all the " {tag}" removed. The tag can be anything so I'm looking... (4 Replies)
Discussion started by: kerppz
4 Replies

7. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies

8. Shell Programming and Scripting

extract string portion from filename using sed

Hi All, I posted something similar before but I now have a another problem. I have filenames as below TOP_TABIN240_20090323.200903231830 TOP_TABIN235_1_20090323.200903231830 i need to extract the dates as in bold. Using bash v 3.xx Im trying to using the print sed command but... (11 Replies)
Discussion started by: santam
11 Replies

9. Shell Programming and Scripting

Replace a filename with full path using sed

hi, i need to replace a line a file with a new raw device location.. original file.. /opt/sybase/ASE1502/ASE-15_0/bin/dataserver \ -d/data/TST_AKS1/sybdevices/master.dat \ -e/logs/sybase/TST_AKS1/SFO_TST_AKS1.log \ -c/apps/sybase/ASE1502/ASE-15_0/TST_AKS1.cfg \... (2 Replies)
Discussion started by: aksaravanan
2 Replies

10. Shell Programming and Scripting

Parse and replace string in filename

Hi, I've a filename of this format: "XXX_XXX_TXT.TAR.AS". Need to change the name into this format: "XXX_XXX.TAR.AS". This file resides in a directory. I'm ok with using the find command to search and display it. Essentially I just need to replace the string "_TXT.TAR.AS" to ".TAR.AS". Is awk... (17 Replies)
Discussion started by: chengwei
17 Replies
Login or Register to Ask a Question