Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to find and replace some patterns in some files, but I need it to return 1 or something if a pattern is not found.

Can I do this with sed alone or do I need to check whether the pattern exists with another command?

Any suggestions?

share|improve this question

Check out this answer: How to check if sed has changed a file.

Its pretty much the same as you are asking, its suggesting you use awk or output to a different file and diff the two files.

share|improve this answer
If using GNU sed, I would start with the least voted answers which use sed's q command to set the exit code. (Sadly q's exit code argument is not POSIX compliant.) – manatwork Nov 15 '12 at 13:09
The least voted answer has changed. I think the answer @manatwork is referring to is stackoverflow.com/a/12144882/1033422 – SHC Jan 7 '14 at 8:52

Your Answer


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.