Solved

Short sed script

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.

932 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

20 Experts available now in Live!

Get 1:1 Help Now