Solved

Short sed script

Posted on 2011-09-20
2
366 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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.

724 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