Solved

Short sed script

Posted on 2011-09-20
2
343 Views
Last Modified: 2012-05-12
I need a sed script that will look for an occurrence of aaaaa followed two lines later by an occurrence of bbbbbb.
0
Comment
Question by:xoxomos
2 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 250 total points
ID: 36572422
How about this?

This time it's not a one-liner (and it's not sed but grep).
I tried to make it a bit more detailed as usual.

The script will not only tell you verbatim what it found (or not found) but will also set different return codes.
So if you want to rely on these codes instead of the messages just redirect the script's stdout to /dev/null (or to a logfile, if desired).

Adjust the variables according to your needs.

wmp
#!/bin/bash
FILE=/path/to/inputfile
STRING1="aaaaa"
STRING2="bbbbb"
DISTANCE=2
L1=$(grep -n "$STRING1" $FILE |cut -f1 -d":")
if [[ ! -z $L1 ]] ; then
 L2=$(grep -n "$STRING2" $FILE |cut -f1 -d":")
   if [[ ! -z $L2 ]] ; then
     if [[ $(($L2 - $L1)) -eq $DISTANCE ]] ; then
       echo "SUCCESS! $STRING1 found at line $L1, $STRING2 found at line $L2"
       RC=0
       else echo "$STRING1 ($L1) and $STRING2 ($L2) found, but distance is not $DISTANCE lines!"
            RC=1
     fi
     else echo "$STRING1 found, but $STRING2 not found!"
     RC=2
   fi
   else echo "$STRING1 not found!"
   RC=3
fi
exit $RC

Open in new window

0
 

Author Closing Comment

by:xoxomos
ID: 36582396
Thanks.  I'll give it a try.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now