Computing.Net > Forums > Unix > sed question

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

sed question

Reply to Message Icon

Name: Peter
Date: July 9, 2003 at 07:03:39 Pacific
OS: my
CPU/Ram: computer
Comment:

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'`
done

I 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



Sponsored Link
Ads by Google

Response Number 1
Name: David Perry
Date: July 9, 2003 at 10:40:44 Pacific
Reply:

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


0

Response Number 2
Name: WilliamRobertson
Date: July 9, 2003 at 10:56:48 Pacific
Reply:

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 "&lt;" which the board resolves to "<" in the preview screen, where I replace them with "&lt;" again for the final confirm. You could probably just post "&amp;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.


0

Response Number 3
Name: James Boothe
Date: July 9, 2003 at 11:50:57 Pacific
Reply:

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


0

Response Number 4
Name: nails
Date: July 14, 2003 at 12:44:05 Pacific
Reply:

TEST:

done file


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: sed question

sed question www.computing.net/answers/unix/sed-question-/5518.html

Sed question www.computing.net/answers/unix/sed-question/6826.html

general sed question www.computing.net/answers/unix/general-sed-question/3935.html