Results 1 to 6 of 6

Thread: How to extract text lines between two strings

  1. #1
    Join Date
    May 2009
    Posts
    42

    How to extract text lines between two strings

    Hi friends,

    I had created a text files and now i want to extract text lines between two strings i know that it's possible but don't know how to do it in Linux does anyone has any idea for the same. Now i want lines which are between Receipt and Canceled. Any idea ?Following are the details of the files which is created by me

    File details

    Receipt
    demo1
    demo2
    demo3
    End

    Receipt
    demo4
    demo5
    demo6
    Canceled

    Receipt
    demo7
    demo8
    demo9
    End

    demo10
    demo11

    Receipt
    demo12
    demo13
    demo14
    Canceled

  2. #2
    Join Date
    Jan 2009
    Posts
    126

    Extract text lines between two strings

    In that case i would suggest you to work around with the following code i am sure you will get some result

    Code:
    awk '\
    /Receipt/,/Canceled/ {
    if (new_group==0)
    {print substr($0,index($0,"Receipt"))
    new_group=1}
    else
    {if (match($0,"Canceled"))
    {print substr($0,1,index($0,"Canceled")-1)
    new_group=0}
    else
    print
    }
    }' test.txt

  3. #3
    Join Date
    May 2009
    Posts
    42

    How to extract text lines between two strings

    Thanks for replying me i had try to use and i had seen that it's working fine with groups but the code which you had provide me is not helping me a lot is there any other solution apart from it.

  4. #4
    Join Date
    Jan 2009
    Posts
    141

    How to extract text lines between two strings

    I think if you want to extract text lines between two strings then you code has to be some what different and the above code will be helpful for me but if you want you can also try to take reference of this code hope you will get some idea from it and will be able to get what exactly you are get

    Code:
    awk '
    /Receipt/ {s=x} 
    {s=s$0"\n"} 
    /Line13/ {p=1} 
    /Canceled/ && p {print s;s=x;p=0}
    ' file

  5. #5
    Join Date
    Jul 2009
    Posts
    18

    How to extract text lines between two strings

    Hello friends,

    I am also facing similar type of issue where i am trying to extract string from a string for example
    Code:
    $string = UPDATE "pqr"."def" FROM "pqr"."def" AS A SET "C1"=A."xy", "y"=A."b", "z"=A."xy"
    Where my result should be the text between UPDATE and FROM which is
    "pqr"."def" and for more information i had try to use substr() but that didn't helped me a lot.

  6. #6
    Join Date
    Jan 2009
    Posts
    150

    Extract text lines between two strings

    If you want to extract text lines between two strings then you can use the below code where and i would suggest you to use UPDATE at the beginning of the string this would be very good for you and here is the code for the same, i hope you would understand what do i mean to say

    Code:
    $string =~ /UPDATE([\s\S]+?)FROM/;
    $between = $1;

Similar Threads

  1. Unable to read Specific Lines of text in c# program
    By Linoo in forum Software Development
    Replies: 9
    Last Post: 21-05-2012, 11:46 AM
  2. Replies: 19
    Last Post: 17-11-2010, 04:19 PM
  3. Can i extract text from jpg file
    By Ransom in forum Windows Software
    Replies: 5
    Last Post: 05-03-2010, 03:49 AM
  4. Regex to extract lines
    By ISAIAH in forum Software Development
    Replies: 5
    Last Post: 16-02-2010, 02:13 AM
  5. Batch Script to parse lines in text file
    By jntoner1@gmail.com in forum Windows Server Help
    Replies: 8
    Last Post: 28-01-2009, 04:38 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,291,309.20820 seconds with 17 queries