Solved

Short sed script

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
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…
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.

734 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