Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Want to 1st test sed stmt

Status
Not open for further replies.

cptk

Technical User
Mar 18, 2003
305
US
How can I 1st test to see if a sed command works before actually running the command?

For eaxmple:
> sed '1,5 s/\<searchdata\>/replacedata/' filename

One solution I've come-up is ...
> sed '1,5 s/\<searchdata\>/replacedata/' filename | grep replacedata

then check the exit status (e.g. $?)

...is there a better way?


 
You could pipe the output to diff which would show you the changes, without affecting the file:
[tt]
sed '1,5s/\<searchdata\>/replacedata/' filename | diff - filename
[/tt]
 
And what about this ?
sed [!]-n[/!] '1,5 s/\<searchdata\>/replacedata/[!]p[/!]' filename

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PHV -

I went with your recommendation ...

if [ -n "`sed -n '1,5 s/\<searchdata\>/replacedata/p' filename`" ]
then ...

thanks!!!

[Note: -n checks if string is non-zero length]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top