Sed Find and Display Text Between Two Strings or Words

Q. How do I find the text between the strings FOO and BAR inclusive using sed command line option?

A. Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream – a file or input from a pipeline.

Advertisement

To output all the text from file called test.xt’ between ‘FOO’ and ‘BAR’, type the following command at a shell prompt. The -n option suppress automatic printing of pattern space:
$ sed -n '/WORD1/,/WORD2/p' /path/to/file
$ sed -n '/FOO/,/BAR/p' test.txt

You can easily find out all virtual host entries from httpd.conf, type
# sed -n '/<VirtualHost*/,/<\/VirtualHost>/p' /etc/httpd/conf/httpd.conf

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

45 comments… add one
  • Shashank Jan 31, 2015 @ 11:23

    Hi My problem is.
    a test.txt file having following list without quotes:

    “a12312
    a3
    a123
    a12
    a12312
    a12312
    a12
    a12
    a12
    a123
    a123
    a123
    a12312
    a3
    a123
    a12
    a12312
    a12312
    a12
    a12
    a12
    a123
    a1
    a1
    a1
    a1”

    I want a sed query that it prints all lines found in between “a12312” & “a123” matched at the last.

    So desired output should be:
    a12312
    a12
    a12
    a12
    a123

  • poomariappan Feb 11, 2015 @ 11:17

    Same Problem how to write in batch file. In windows
    I need get the file content from two particular strings.
    any one please success some examples

  • stormventures Apr 24, 2015 @ 17:18

    I have the following file of format:
    aaa
    1
    2
    3
    bbb
    aaa
    4
    5
    6
    bbb
    aaa
    7
    8
    9
    bbb
    what I want for output is separate file for each occurrence of such that:
    out1.txt contains:
    1
    2
    3

    out2.txt contains:
    4
    5
    6

    out3.txt contains:
    7
    8
    9

    Can anyone please help? Thanks!

    • Swami Sep 15, 2017 @ 12:20

      Hey,

      Did you get an solution to this, if so please share it with me in my email id please . I am having a same scenario and this would be a very helpful. Thanks.

  • Ganga May 3, 2016 @ 3:41

    Hello I want to extract text between from below xml file ..
    I tried all option using sed command but did not worked your help is highly appreciated .. Thank you . here is sample file
    ——-
    FRE 3.0.02015-12-28T13:34:19

    2478 FOX CT
    Kingman
    86409
    AZ

    false

    false

    152849 LoanDeliveryFilePreparer

  • jay Jun 18, 2016 @ 4:46

    Good explaination. I have a challenge i have a fie where i have some thing like this . i need to read the file and crate a txt file as below :
    Input file
    Begin search for: aaa.com
    Found Email Entry for aaa.com
    version: 1
    dn: uid=WP.Head,ou=people,o=internet
    PrimaryEmailAddress: bbb.com
    EmailAlias: bbb.com
    EmailAlias: ccc.com
    EmailAlias: ddd.com
    EmailAlias: eeee.com
    AccountState: ACTIVE
    AccountType: PRIMARY
    OwnerAccount: 123456
    Search complete for: aaa.co.n

    Begin search for: gz.com
    Found Email Entry for kan.co
    version: 1
    dn: uid=keenan,ou=people,d,o=internet
    PrimaryEmailAddress: z.co
    AccountState: ACTIVE
    AccountType: SUB
    PrimaryAccountID: don
    Search complete for: kan.co

    Begin search for: cory.co
    Found Email Entry for cor.co
    version: 1
    dn: uid=corey.reddish,ou=people,o=internet
    xtraPrimaryEmailAddress: cory.co
    AccountState: ACTIVE
    AccountType: SUB
    PrimaryAccountID: kkjan
    Search complete for: cory.co

    Output:
    uid |PrimaryEmailAddress |EmailAlias1 |Accountstate | Accounttype |OwnerAccount
    wp |bbb.com |bbb.com,ccc.com,ddd.com,eeee.com | Active | Primary | 123456
    keenan |z.co | | Active | Sub | don
    corey |cory.co | | Active | Sub | kkjan

  • Fabi Oct 27, 2016 @ 11:44

    I tryed the commad abut it is always printing the the file:
    I used sed -n ‘//,//p’ file and sed -n ‘/<link*/,//p’ file .
    I want to extract the links and then i want to use wget for downlaoding the files.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.