editing files

Is there any command which I can apply from the command line to find and replace a particular text say "00:00:00:00" with "00" from all the files( where ever this text exists) of the current directory?

Yes, but be warned this does ALL files in a given directory.
It also does not change files in subdirectories.

cd /directory/I/want/to/change
ls -1 * |
while read fname
do
   sed 's/00:00:00:00/00/g'  "$fname" > tmp.tmp
   mv tmp.tmp "$fname"
done

# For multiple files
perl -pi -e 's/searchterm/replaceterm/' *.txt

#for single file
perl -pi -e 's/searchterm/replaceterm/' myfile.txt

Or very similarly:

sed -i -e s/searchterm/replaceterm/g *.txt

You may need to quote the expression. You can also edit files in place with ed.

Thanks for the reply. But this command will just display the term replaced by the new term, not that the changes actually takes place in the file. Even if we redirect the output to a different file this can be done only for one file at a time and also getting back to the original file name is at the cost of another command.

isn't there any way the changes can directly take place in those files?

Thanks in advance

This is what the -i switch is for. From the sed man page:

 -i[SUFFIX], --in-place[=SUFFIX]

              edit files in place (makes backup if extension supplied)

This is GNU-ism - not all sed-s support that.

This is true, so if that isn't available I would use ed.

ed -s /target/file <<EOF
g/search/s/search/replace/g
w
q
EOF

This is a pretty simple example and you can loop through your target files. The reason for the two search fields in the above example is so you can search for something on a line and then search/replace something else on that particular line. Much more detail in Ch25 of Amazon.com: Expert Shell Scripting (Expert's Voice in Open Source): Ron Peters: Books

when i give sed -i. It throws an error

$sed -i -e s/see/saw/g *.txt

sed: illegal option -- i

Thanks

see the above discussion.

whats wrong with this code..please

#!/bin/sh
allone=`ls *.txt`
echo $allone
for i in ${allone}
do
echo $i
sed -s /${i} <<EOF
g/see/s/see/saw/g
w
q
EOF
done

i have two text files in which i have 'see' and i want to make it 'saw'

the scripts executes fine but i dont see any replacement done.

Thanks

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

---------- Post updated at 03:01 PM ---------- Previous update was at 02:57 PM ----------

#!/bin/sh

for i in *.txt 
do
  echo $i
  printf 's/see/saw/g\nwq!\n' | ex - "${i}" 
done

Even this was not successful

onetest.sh
r1.txt r2.txt

Substitute pattern match failed
r1.txt
Substitute pattern match failed
r2.txt
Substitute pattern match failed

Is there anything wrong with the command

Thanks

sorry:

#!/bin/sh

for i in *.txt 
do
  echo $i
  printf '1,$s/see/saw/g\nwq!\n' | ex -s "${i}" 
done

Thank you so much.. This is working fine.

One more request please could you explain the command line
printf '1,$s/see/saw/g\nwq!\n' | ex -s "${i}" as i am not much familiar with this. Is it possible to execute this line at the command prompt as i am getting an error like "bash: ./r1.txt: Permission denied"

Thanks a lot

Most likely you don't have 'write' access write for ./r1.txt file - check its permissions.
'ex' is very similar to 'sed' in terms of the command options.
If I do:

printf '1,$s/see/saw/g\nwq!\n'

I get:

1,$s/see/saw/g
wq!

The first line looks familiar, doesn't it? Very similar to sed's.
The second line simply instructs 'ex' to write (w) the file and quit (q) 'ex'.
The loop takes care of iterating through the files.
'man ex' for more details.

Got most of it. Thanks a lot. Only one which i did not get is the meaning of 1,$ after the printf. s/see/saw/g is same as we use for stream ed.

Thanks and regards

What I see is that you're using 'sed' instead of 'ed'. I haven't tested your specific code, but otherwise is looks ok.

Also, the 1,$ question. This just means perform the search/replace from the beginning (1) to the end ($) in terms of line numbers. Actually, though functionally I'm sure this definition is probably accurate enough, but I've been wrong plenty of times and it may not tell the whole story.

What I really like about this thread is that it demonstrates so well that the question is not whether I can do something via command line/script, but more of which solution do I want to use.