1752734 Members
5587 Online
108789 Solutions
New Discussion юеВ

sed

 
SOLVED
Go to solution
himacs
Super Advisor

sed

Hi i have handfull doubts in scripting.Now m stuck up with sed.

I have heard the sed is non-interactive and used to replace words in scripts where vi is not practical.

sed 's/abc/ABC/g' test.sh

I want to know this will modify the same script and save it or create new script with new modifications or we need to direct the output new file ?

and plz also tell me other use of sed.Bcoz man page is not helpful.

regards
himacs

 

P.S. This thread has been moved from HP-UX>System Administration to HP-UX > languages. -HP Forum Moderator

7 REPLIES 7
Steven Schweda
Honored Contributor

Re: sed

> I want to know [...]

Your fingers are apparently _not_ broken.
What happens when you run this command?

> and plz also tell me other use of sed.Bcoz
> man page is not helpful.

Why, because you can't read (or write)
English, or because you're too lazy to read
the stuff, or what? Perhaps a Forum search
for keywords like
sed
would find an example or two. Buy a book.
Look around on this new Inter-Web thing.

"I'm helpless. Please do my job for me."

Give me a break.
James R. Ferguson
Acclaimed Contributor
Solution

Re: sed

Hi:

> I have heard the sed is non-interactive and used to replace words in scripts where vi is not practical.

That's true. The 'sed' utility is designed to be used to edit a stream of data; hence the name 'sed' from "Stream EDitor". The actions are intended to occur non-interactively.

> I want to know this will modify the same script and save it or create new script with new modifications or we need to direct the output new file ?

No and yes.

The GNU version of 'sed' which would be found in Linux distributions offers an in-place edit using the '-i' or '--in-place' option. Versions of 'sed' that are provided by HP-UX and AIX, for example, lack this feature. Perhaps that is why you have found the manpages "not helpful" --- you may have seen examples using the GNU extensions.

Thus, for tranditional 'sed' implementations you have to redirect the output to a new file and subsequently rename ('mv') the new file to the old file. Do _not_ try to use the same output file name as the input file too. This will not work.

Regards!

...JRF...




himacs
Super Advisor

Re: sed

Hi JRF,

Thanx for ur reply.. cleared my doubt.


I have always found ur replies are more informative and very simple to understand.

Thanx a lot


regards
himacs
Dennis Handly
Acclaimed Contributor

Re: sed

>JRF: subsequently rename (mv) the new file to the old file.

If these are scripts, you'll also have to make them executable with chmod(1).
James R. Ferguson
Acclaimed Contributor

Re: sed

Hi (again):

>Dennis: If these are scripts, you'll also have to make them executable with chmod(1).

Of course that's true if you use the traditional methods of shelll redirection to a new output file since the shell uses a creation mode mask of 0666 to which the process's umask is then applied. THus following the 'mv' ('rename') a 'chmod' could have to be used to add executable permissions.

_IF_, however, instead of 'sed' one uses Perl's inplace edit feature [ and I'm not sure if this works the same in GNU sed ] then this is not an issue. Perl will leave the permissions of the modified file unchanged:

# perl -pi -e 's/abc/ABC/g' myfile

...edits "myfile" in-place.

# perl -pi.old -e s/abc/ABC/g' myfile

...edits "myfile" in-place but makes a backup copy of the original file suffixed with ".old".

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: sed

Hi (again):

...and as one might expect, the permissions of the file to be edited are also preserved when using a GNU 'sed' in-place edit:

# sed -ie 's/abc/ABC/g' test.sh

# sed --version
GNU sed version 4.1.5
...

Regards!

...JRF...
Mel Burslan
Honored Contributor

Re: sed

I know this is not the question here but I found this a better alternative than mv command to preserve the permissions of the original file in cases like the original poster is facing:

sed 's/abc/ABC/g' test.sh > some_temp_file
cat some_temp_file > test.sh

if the file is something other than text, this method has shortcomings but for text files, such as shell scripts, it works mighty fine.

Hope this helps
________________________________
UNIX because I majored in cryptology...