Solved

Short sed script

Posted on 2011-09-20
2
356 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

808 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