08-09-2009 04:06 AM - last edited on 01-20-2014 02:04 AM by Lisa198503
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.
P.S. This thread has been moved from HP-UX>System Administration to HP-UX > languages. -HP Forum Moderator
Solved! Go to Solution.
08-09-2009 04:49 AM
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
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.
08-09-2009 05:36 AM
> 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.
08-09-2009 02:46 PM
>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".
08-09-2009 03:58 PM
...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
08-10-2009 05:08 AM
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...