Find the answer to your Linux question:
Results 1 to 4 of 4
if I'd a file like this something somethine123 begin something end begin xyz end somethingelse and I want to find the strings only between begin and end, exluding begin and ...
  1. #1
    Just Joined!
    Join Date
    Aug 2005
    Posts
    2

    Exclamation grep ??

    if I'd a file like this


    something
    somethine123
    begin something end
    begin xyz end
    somethingelse


    and I want to find the strings only between begin and end, exluding begin and end;
    How can I grep that?

  2. #2
    Trusted Penguin Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,392
    I'm no expert at regular expressions, but you could try an Online Regular Expression Builder, such as this one. This can help you understand regex much better. You can feed these into grep as the search sequence.
    Linux user #126863 - see http://linuxcounter.net/

  3. #3
    Linux User Manko10's Avatar
    Join Date
    Sep 2010
    Posts
    250
    Use lookahead and lookbehind expressions:
    Code:
    grep -oP '(?<=begin ).+?(?= end)' file
    Refining Linux Advent calendar: “24 Outstanding ZSH Gems

  4. #4
    Just Joined!
    Join Date
    Aug 2005
    Posts
    2
    Thanks Manko10, It worked for me.
    & Thanks Roxoff, that was really helpful in knowing the regs

Posting Permissions

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