Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have been doing quite a bit of reading on sed over the last day or two, and I am kind of stumped on one particular thing.
Is there any way to save the changes you make ?
Here is a script I was given from nails toremove _hello from a file name:
for var in `ls *_hello*`
do
mv $var `echo $var|sed 's/_hello//g'`
doneI modified it a bit to run as a command at the command line.
ls | sed -e 's/_[^.]*././g'
This one of course does not do anything but list the files with the names changed, but does not actually change them. (Is there away to do this ?)
the other quesiton is tied in with this.
If you do the search and replace, or use sed to make any type of changes to a particular file, how can you save the changes to that file?ex.
sed 's/[^ ]*/(&)/' testFile
# add brackets to the first word of the line
How could you save these changes to testFile?I tried:
sed 's/[^ ]*/(&)/' (lessSign)testFile (greaterSign) testFile
But this just erases testFile.Thanks for any help
PS. How do you put left and right arrows in this forum ?
Peter

There are other ways of doing this, but it you redirect stdout to a temp file then replace the original with the temp file you will save the changes.
sed -e 's/this/that/' infile > outfile
mv outfile infile--warning the following may introduce a race condtion---
cat infile | sed -e 's/this/that/' > infile

To produce an edited file you have to capture sed's output in another file, and then mv that (or whatever) in a second step. For that type of file editing you could also consider scripting a vi/ex/ed session.
"<" signs are a bit of a pain. What I do is post "<" which the board resolves to "<" in the preview screen, where I replace them with "<" again for the final confirm. You could probably just post "&lt;" to start with (if I've got that right - appologies if this comes out unreadable) but I prefer to check it manually because there's no going back...
btw,
for var in `ls *_hello*`
can be simplified a bit to
for var in *_hello*
The `ls` just spawns another subprocess.

Peter, I think you want your modified sed command to locate strings that begin with an underscore and terminate with a dot (and then change that string to a single dot).
Your command will work as long as each string that begins with an underscore is ultimately followed with a dot. But it works not because of that second dot, but rather the bracketed expression that is looking for zero-or-more non-dots.
Inside the brackets, that dot stands for an actual dot (negated in this case). But the dot following the asterisk stands for "any single character". Therefore, the following sed finds an underscore followed by 3 non-dots followed by any single character (a "t" in this case), and changes _test to a dot:
echo myfile_test | sed -e 's/_[^.]*././g'
myfile.To make that pattern look only for strings that terminate with an actual dot, you need to backslash that second dot:
echo myfile_test | sed -e 's/_[^.]*\././g'
myfile_test

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |