sed (163 Views)
Reply
Super Advisor
himacs
Posts: 611
Registered: ‎01-27-2009
Message 1 of 8 (163 Views)
Accepted Solution

sed

[ Edited ]

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

Please use plain text.
Honored Contributor
Steven Schweda
Posts: 9,084
Registered: ‎02-23-2005
Message 2 of 8 (163 Views)

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.
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 3 of 8 (163 Views)

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...




Please use plain text.
Super Advisor
himacs
Posts: 611
Registered: ‎01-27-2009
Message 4 of 8 (163 Views)

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
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 25,001
Registered: ‎03-06-2006
Message 5 of 8 (163 Views)

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).
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 6 of 8 (163 Views)

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...
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 7 of 8 (163 Views)

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...
Please use plain text.
Honored Contributor
Mel Burslan
Posts: 3,213
Registered: ‎08-26-1998
Message 8 of 8 (163 Views)

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...
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation