3

Does sed have an option similar to grep -q, where it returns a 1 or 0 depending on if it actually finds a string match or not?

The reason I ask is in some cases I use sed in particular ways, like finding the first match after a given line number, or setting a capture group and acquiring a string that way, so I wanted to see if sed had anything built in that would check if anything is returned because I want to throw an error back to the user and exit my bash script if a match isn't found

Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
user2150250
  • 4,797
  • 11
  • 36
  • 44
  • 6
    why not just use grep? – metalhead Apr 22 '13 at 19:15
  • Well, in some cases I use sed in particular ways, like finding the first match after a given line number, or setting a capture group and acquiring a string that way, so I wanted to see if sed had anything built in that would check if anything is returned because I want to throw an error back to the user and exit my bash script if a match isn't found. – user2150250 Apr 22 '13 at 19:19
  • Votes down without comments? A comment would be appreciated. – user2150250 Apr 22 '13 at 19:21
  • 1
    @user2150250 not with `sed` I don't believe, you can with `awk` however. – Chris Seymour Apr 22 '13 at 19:24
  • 2
    @sudo_O Thank you for your helpful answer. That's all I was asking. I don't see why my inquiry deserved two votes down, but it's been said before, the sed community is picky (no pun intended). – user2150250 Apr 22 '13 at 19:27
  • 1
    Could you chain it like this grep -q 'pattern' file && sed 's/pattern/' file || echo "cannot make requested change" (example found at http://www.unix.com/shell-programming-scripting/154429-exit-status-sed-when-fails.html) or can you 100% not use grep? – eldris Apr 22 '13 at 19:28
  • 1
    @user2150250 I believe if you would have asked the question in the manner of your first comment which gives sane reason for why you would do this i.e. you want to do more than a simple match, then you wouldn't have got such a negative response, people can be quick to vote, something to bare in mind – Chris Seymour Apr 22 '13 at 19:31
  • 1
    @eldris yeah I suppose I could use something like that in most of my cases but in my more complicated sed's like using capture groups or finding first match after line #, I'm not sure grep could utilize the syntax in the same way and I'm not too familiar with porting "sed fucntionality" over to grep unfortunately. Thank you for the helpful link and comment though. – user2150250 Apr 22 '13 at 19:42

2 Answers2

6

You are much better off using grep -q for this, but if you ABSOLUTELY want to use sed then here is how you could replicate the functionality...

cat myFile | sed -n "/myString/p" | wc -l

This will pipe out the number of occurences of "myString" in a file. From here you could do a simple test to see if the number is greater then 0, if so return 1, if not return 0. Here is a script demoing how to accomplish this...

#!/bin/bash
count=$(cat myFile | sed -n "/\myString/p" | wc -l)
final=0
if [ count -gt 0 ]; then
    final=1
else
    final=count
fi
echo final

The script above will print out 1 or 0, 1 if the string was found and 0 if it wasn't (note: I haven't checked this, but there's no reason why it wouldn't work).

Again, grep -q is the tool for the job and I would recommend using it if at all possible.

Atlas Wegman
  • 569
  • 2
  • 10
3

You can set the exit code using the q command in sed.

q [exit-code]

Immediately quit the sed script without processing any more input, except that if auto-print is not disabled the current pattern space will be printed. The exit code argument is a GNU extension.

See also this answer for a clue.

EDIT:

As @cbuckley kindly linked to, you can find directions here.

Community
  • 1
  • 1
  • @0A0D Would you mind showing an example of how this could be used to echo an error message to the user and exit the script if sed doesn't find what it's looking for? – user2150250 Apr 22 '13 at 19:50
  • @user2150250 see http://stackoverflow.com/q/15965073 – cmbuckley Apr 22 '13 at 20:30
  • Thanks! I had seen that and was having trouble getting it to work for my more particular cases like I mentioned in the problem description, but I'm gonna tinker around with it a bit more and see what I can squeeze out of it. – user2150250 Apr 22 '13 at 20:50