Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

My application will be running on all versions of Unix-based OS, say, Linux, MacOS, HP-UX, Sun Solaris , OSX, etc.

I'm using the following sed command in a common place to delete the string matched in a text file:

sed -i '/string1/d' mytextfile

Will this work in all *nixes?

share|improve this question
    
As long as the program source is available, and is not platform dependent, then of course! –  Torger597 Jul 18 at 6:15
    
Nit: MacOS is a superset of OSX, and not all versions of MacOS are Unix-based. en.wikipedia.org/wiki/Mac_OS –  Ogre Psalm33 Jul 18 at 15:21
    
On OS X, the -i option requires an argument, so you'd have to use -i'' to edit in place. –  Waleed Khan Jul 18 at 18:38

2 Answers 2

Yes, sed is on all Unix versions. It is part of the POSIX standard: every "official" Unix includes it, and all Linux and BSD distributions include it as well.

There are several different versions of the sed command, with different extensions. If you stick to the common subset of sed options and language described by POSIX then your command will work on all systems.

share|improve this answer
    
Thanks Michael. i couldnt find sed "-i" option in the POSIX standard page, does it means -i there's a possibility that " -i " option might not be available on all versions –  user2172617 Jul 18 at 7:18
    
That's correct. sed -i is not supported by Solaris sed, for example. –  Michael Homer Jul 18 at 7:20
    
YES Michael, sed -i , it says illegal option in solaris –  user2172617 Jul 18 at 8:07
    
how do i simualte -i option wih -e or any other options in solaris, –  user2172617 Jul 18 at 8:08
    
i tried with sed -e '/string/d' file, it deletes, but not updated in the text file, appreciate any help Thanks –  user2172617 Jul 18 at 8:08

POSIX does not mandate sed -i because it is redundant, and also not in line with the original purpose of sed. sed was originally meant to edit streams (hence its name stream editor), not files. The correct tool in POSIX to edit files non-interactively is ed

printf "/text/d\nw" | ed file.txt

Sadly, many current systems think ed is obsolete and don't provide it by default, even though it is mandated by POSIX... But at least it should be easily installable on most systems (unlike GNU sed, which might requre compilation from the source code).

EDIT: Also, contrary to what the wording of the question implies, the d command of (s)ed does more than deleting the given string, it deletes any line which contains it.

share|improve this answer
    
thanks fkraiem, but no this isn't working fine in linux now, but this is available in POSIX,OK, how safe is below cmd, sed '/string/d' txtfile > tmpfile && mv tmpfile txtfile –  user2172617 Jul 18 at 13:07
    
The only reason it couldn't be working is if your system is one of those I alluded to, in which case ed should be in the official repositories. As for the sed + mv option, it's probably safe, but like the proverbial alcohol-free beer, "it tastes the same, but you know it ain't right". –  fkraiem Jul 18 at 17:18
    
what abt this, this falls under POSIX satndard, right, perl -i -l -p -e 's/pattern/<NULL>/s;' textfile the above will do what i'm looking for via sed , i.e. del entire line if pattern matches , via cmd line infile –  user2172617 Jul 20 at 16:09
    
@user2172617 perl isn't POSIX at all; if it is present it will probably be consistent for a simple oneliner, but that one won't do what your sed did. With -p but not -l you can use -e '$_="" if /pattern/' but I think it's clear to use -n -e 'print unless /pattern/' –  dave_thompson_085 Jul 20 at 23:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.