Find the answer to your Linux question:
Results 1 to 2 of 2
Hey, im new to this forum and to linux generally if i posted this in the wrong place please move I want to use sed to extract words between 2 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2013
    Posts
    1

    search content between 2 identical words using <sed>


    Hey, im new to this forum and to linux generally
    if i posted this in the wrong place please move

    I want to use sed to extract words between 2 identical words
    for example :
    build blala bla la build

    I want "blala bla la" between the two "Hi"

    Anyone have any idea ?

    Thanks in advance

  2. #2
    Just Joined!
    Join Date
    Oct 2007
    Posts
    15
    What you'll want you script to do is set a flag when it sees the first keyword string that you want to trigger the behavior, and then another block of code that detects that the flag is set and the keyword is seen again, which forms the end of your condition. Here's a simple example:
    <code>
    BEGIN {counter = -1; flag = 0; funcname=funct;}
    {
    if (/^build /)
    {
    if(flag) {
    exit; # your exit condition, because you already seen the build keyword once
    }
    else {
    flag = 1;
    }
    mpos = match($3,funcname);
    if(mpos == 2) {
    flag = 1;
    }
    }
    if (flag) print $0; # here anything between the keywords gets printed to std-out
    }
    END {}
    </code>

    So, when the flag is set, the $0 is printed, which would be a whole line. You'll need modify that line to print what fields/components of the line(s) you want to print but this outer structure does what you want.

    -Col P.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •