|
|||||||||
| 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 |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
THANK YOU VERY MUCH ZB -- now I know how to write a script....
|
| Sponsored Links | |
|
|
#6
|
||||
|
||||
|
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 | ||
|
|
![]() |
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 |
|
|