Skip to main content
Syntax highlighting was goofy, now I'm done.
Source Link
> ./sed.sh data.txt
file has some
content, be kind meanies
don't eat meanies after 8
because of the pepperoni monsters

> echo $?
0
> ./sed.sh data.txt
file has some
content, be kind meanies
don't eat meanies after 8
because of the pepperoni monsters

> echo $?
0
> ./sed.sh data.txt
file has some
content, be kind meanies
don't eat meanies after 8
because of the pepperoni monsters

> echo $?
0
> ./sed.sh data.txt
file has some
content, be kind meanies
don't eat meanies after 8
because of the pepperoni monsters

> echo $?
0
Added a shell script with an interpreter and a positive match output. I'm probably done now.
Source Link

Note that the sed command uses -n to indicate that the pattern space is not printed out each time you get to the end of the sed script. -f indicates you read the sed commands from the following file.

If you wanted to be crazy, you could wrap this in a shell script by using sed as the interpreter (discovering its location with which sed). I omitted the comments this time for brevity and replaced "crayons" as the replacement pattern with "pizza".

shell script: sed.sh

#!/usr/bin/sed -nf
s/pizza/meanies/ 
p                
t loop           
$!d              
x                
/meanies/q 0     
q 1              
                 
:loop            
x                

invoke with:

> ./sed.sh data.txt
file has some
content, be kind meanies
don't eat meanies after 8
because of the pepperoni monsters

> echo $?
0

Note that the sed command uses -n to indicate that the pattern space is not printed out each time you get to the end of the sed script. -f indicates you read the sed commands from the following file.

If you wanted to be crazy, you could wrap this in a shell script by using sed as the interpreter (discovering its location with which sed). I omitted the comments this time for brevity and replaced "crayons" as the replacement pattern with "pizza".

shell script: sed.sh

#!/usr/bin/sed -nf
s/pizza/meanies/ 
p                
t loop           
$!d              
x                
/meanies/q 0     
q 1              
                 
:loop            
x                

invoke with:

> ./sed.sh data.txt
file has some
content, be kind meanies
don't eat meanies after 8
because of the pepperoni monsters

> echo $?
0
Adding syntax tags
Source Link
sed '/if-the-pattern-space-has-this/<do this command>' <file>
sed '/if-the-pattern-space-has-this/<do this command>' <file>
> sed -n '/good-pattern/q 0;$q 1' <<< "bad-pattern"
> echo $?
1

> sed -n '/good-pattern/q 0;$q 1' <<< "good-pattern"
> echo $?
0
> sed -n '/good-pattern/q 0;$q 1' <<< "bad-pattern"
> echo $?
1

> sed -n '/good-pattern/q 0;$q 1' <<< "good-pattern"
> echo $?
0
sed -n '/^block:/,/^$/{/crazy/q 0};$q 1' <<END && echo -e "\n--------------\n FOUND" || echo "\n----------------\n NOT FOUND"
this thing
block:
   is crazy
END

--------------
 FOUND
sed -n '/^block:/,/^$/{/crazy/q 0};$q 1' <<END && echo -e "\n--------------\n FOUND" || echo "\n----------------\n NOT FOUND"
this thing
block:
   is crazy
END

--------------
 FOUND
> sed -n '/^block:/,/^$/{/crazy/q 0};$q 1' <<END && echo -e "\n--------------\n FOUND" || echo -e "\n----------------\n NOT FOUND"
this thing
block:

   is crazy
END

----------------
 NOT FOUND

> sed -n '/^block:/,/^$/{/crazy/q 0};$q 1' <<END && echo -e "\n--------------\n FOUND" || echo -e "\n----------------\n NOT FOUND"
this thing
block:

   is crazy
END

----------------
 NOT FOUND

# Searching for the word "crayons", replace this
# with "pizza" to see a positive match.  The replacement
# word is "meanies", which you can also replace.
s/crayons/meanies/

# We print each line regardless of a pattern match
p

# If the most recent 's' command did a substitution, take
# the branch to the loop.
t loop

# If we're not on the last line, delete the pattern space and
# start processing the next line.
$!d

# This only happens on the last line.  If we get here, swap the
# last hold space in and then return positive or negative based
# on the content in the hold space.  The hold space should only
# have matching content if we matched once before and swapped
# the matching pattern into the hold space.
x
/meanies/q 0

# If we don't match the replacement pattern then return falsy.
q 1

# This loop is only taken on a positive match, effectively
# just swapping the most recent match into the hold space.
:loop
x
# Searching for the word "crayons", replace this
# with "pizza" to see a positive match.  The replacement
# word is "meanies", which you can also replace.
s/crayons/meanies/

# We print each line regardless of a pattern match
p

# If the most recent 's' command did a substitution, take
# the branch to the loop.
t loop

# If we're not on the last line, delete the pattern space and
# start processing the next line.
$!d

# This only happens on the last line.  If we get here, swap the
# last hold space in and then return positive or negative based
# on the content in the hold space.  The hold space should only
# have matching content if we matched once before and swapped
# the matching pattern into the hold space.
x
/meanies/q 0

# If we don't match the replacement pattern then return falsy.
q 1

# This loop is only taken on a positive match, effectively
# just swapping the most recent match into the hold space.
:loop
x
sed -nf substitutions.sed data.txt && echo -e '\nGood' || echo -e '\nBad'
sed -nf substitutions.sed data.txt && echo -e '\nGood' || echo -e '\nBad'
sed '/if-the-pattern-space-has-this/<do this command>' <file>
> sed -n '/good-pattern/q 0;$q 1' <<< "bad-pattern"
> echo $?
1

> sed -n '/good-pattern/q 0;$q 1' <<< "good-pattern"
> echo $?
0
sed -n '/^block:/,/^$/{/crazy/q 0};$q 1' <<END && echo -e "\n--------------\n FOUND" || echo "\n----------------\n NOT FOUND"
this thing
block:
   is crazy
END

--------------
 FOUND
> sed -n '/^block:/,/^$/{/crazy/q 0};$q 1' <<END && echo -e "\n--------------\n FOUND" || echo -e "\n----------------\n NOT FOUND"
this thing
block:

   is crazy
END

----------------
 NOT FOUND

# Searching for the word "crayons", replace this
# with "pizza" to see a positive match.  The replacement
# word is "meanies", which you can also replace.
s/crayons/meanies/

# We print each line regardless of a pattern match
p

# If the most recent 's' command did a substitution, take
# the branch to the loop.
t loop

# If we're not on the last line, delete the pattern space and
# start processing the next line.
$!d

# This only happens on the last line.  If we get here, swap the
# last hold space in and then return positive or negative based
# on the content in the hold space.  The hold space should only
# have matching content if we matched once before and swapped
# the matching pattern into the hold space.
x
/meanies/q 0

# If we don't match the replacement pattern then return falsy.
q 1

# This loop is only taken on a positive match, effectively
# just swapping the most recent match into the hold space.
:loop
x
sed -nf substitutions.sed data.txt && echo -e '\nGood' || echo -e '\nBad'
sed '/if-the-pattern-space-has-this/<do this command>' <file>
> sed -n '/good-pattern/q 0;$q 1' <<< "bad-pattern"
> echo $?
1

> sed -n '/good-pattern/q 0;$q 1' <<< "good-pattern"
> echo $?
0
sed -n '/^block:/,/^$/{/crazy/q 0};$q 1' <<END && echo -e "\n--------------\n FOUND" || echo "\n----------------\n NOT FOUND"
this thing
block:
   is crazy
END

--------------
 FOUND
> sed -n '/^block:/,/^$/{/crazy/q 0};$q 1' <<END && echo -e "\n--------------\n FOUND" || echo -e "\n----------------\n NOT FOUND"
this thing
block:

   is crazy
END

----------------
 NOT FOUND

# Searching for the word "crayons", replace this
# with "pizza" to see a positive match.  The replacement
# word is "meanies", which you can also replace.
s/crayons/meanies/

# We print each line regardless of a pattern match
p

# If the most recent 's' command did a substitution, take
# the branch to the loop.
t loop

# If we're not on the last line, delete the pattern space and
# start processing the next line.
$!d

# This only happens on the last line.  If we get here, swap the
# last hold space in and then return positive or negative based
# on the content in the hold space.  The hold space should only
# have matching content if we matched once before and swapped
# the matching pattern into the hold space.
x
/meanies/q 0

# If we don't match the replacement pattern then return falsy.
q 1

# This loop is only taken on a positive match, effectively
# just swapping the most recent match into the hold space.
:loop
x
sed -nf substitutions.sed data.txt && echo -e '\nGood' || echo -e '\nBad'
Adding comments
Source Link
Loading
Adding an example and some explanations.
Source Link
Loading
Source Link
Loading