Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
Hi All i have a text in a file i have to grep for the second occurencre (DESCRIPTION) of the file and and append to a file can you please ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2012
    Posts
    4

    Grep for the second occurence in the file


    Hi All i have a text in a file i have to grep for the second occurencre (DESCRIPTION) of the file and and append to a file

    can you please help

    DESCRIPTION
    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).
    While in some ways similar to an editor which permits scripted edits
    (such as ed), sed works by making only one pass over the input(s), and
    is consequently more efficient. But it is sedās ability to filter text
    in a pipeline which particularly distinguishes it from other types of
    editors.

    DESCRIPTION
    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).
    While in some ways similar to an editor which permits scripted edits
    (such as ed), sed works by making only one pass over the input(s), and
    is consequently more efficient. But it is sedās ability to filter text
    in a pipeline which particularly distinguishes it from other types of
    editors.

  2. #2
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    232
    hi,

    I suppose you also want the text below the pattern, right?

    then, grep wil not help, but sed will!

  3. #3
    Just Joined!
    Join Date
    Nov 2012
    Location
    Romania
    Posts
    5
    Hi, do you know if your description is in one line?

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Nov 2012
    Posts
    4
    Yes i want to grep the below text after the DESCRIPTION to the last line "editors." yes it sin the same file

  6. #5
    Just Joined!
    Join Date
    Nov 2012
    Posts
    4
    No its not in one line

    Example : DESCRIPTION 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).While in some ways similar to an editor which permits

    DESCRIPTION sed works by making only one pass over the input(s), andis consequently more efficient. But it is sedās ability to filter text in a pipeline

  7. #6
    Linux Newbie mactruck's Avatar
    Join Date
    Apr 2012
    Location
    City of Salt
    Posts
    187
    Im not sure if I understand this correctly or not. But I think this might work;

    Code:
    grep -v ^DESCRIPTION

  8. #7
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    232
    did some search about "sed print last occurrence" and get an answer:
    Code:
    $ cat UrFile
    DESCRIPTION
    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).
    While in some ways similar to an editor which permits scripted edits
    (such as ed), sed works by making only one pass over the input(s), and
    is consequently more efficient. But it is sedās ability to filter text
    in a pipeline which particularly distinguishes it from other types of
    editors.
    
    DESCRIPTION last
    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).
    While in some ways similar to an editor which permits scripted edits
    (such as ed), sed works by making only one pass over the input(s), and
    is consequently more efficient. But it is sedās ability to filter text
    in a pipeline which particularly distinguishes it from other types of
    editors. 
    $ sed -n 'H; /^DESCRIPTION/h; ${g;p;}' UrFile
    DESCRIPTION last
    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).
    While in some ways similar to an editor which permits scripted edits
    (such as ed), sed works by making only one pass over the input(s), and
    is consequently more efficient. But it is sedās ability to filter text
    in a pipeline which particularly distinguishes it from other types of
    editors.

  9. #8
    Just Joined!
    Join Date
    Nov 2012
    Location
    Romania
    Posts
    5
    hi again, try this:

    awk '/DESCRIPTION/{n++}{print >"output" n ".txt" }' YOURFILE

    this will generate a file for each description that will find untill the next pattern will be found, so if you have

    $: cat YOURFILE
    DESCRIPTION
    This
    is
    a
    nice
    description

    DESCRIPTION
    Indeed
    a
    nice
    row
    here

    when you will execute the command you will get

    output1.txt with

    DESCRIPTION
    This
    is
    a
    nice
    description

    and output2.txt with

    DESCRIPTION
    Indeed
    a
    nice
    row
    here

    Hope this help

  10. #9
    Just Joined!
    Join Date
    Nov 2012
    Posts
    4
    Thank you Watale & covidium for getting me what i was looking for . Thanks a lot for your help

    How can i grep until gawk

    suppose from the below file output.xt
    Code:
    DESCRIPTION last
    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).
    While in some ways similar to an editor which permits scripted edits
    (such as ed), sed works by making only one pass over the input(s), and
    is consequently more efficient. But it is sedās ability to filter text
    in a pipeline which particularly distinguishes it from other types of
    editors. 
    
    
    gawk - pattern scanning and processing language
    
    DESCRIPTION
           Gawk  is  the  GNU Projectās implementation of the AWK programming lan-
           guage.  It conforms to the definition of  the  language  in  the  POSIX
           1003.2  Command  Language And Utilities Standard.  This version in turn
           is based on the description in The AWK Programming  Language,  by  Aho,
           Kernighan,  and  Weinberger,  with the additional features found in the
           System V Release 4 version of UNIX awk.  Gawk also provides more recent
           Bell  Laboratories  awk extensions, and a number of GNU-specific exten-
           sions.
    Desired o/p :
    Code:
    DESCRIPTION last
    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).
    While in some ways similar to an editor which permits scripted edits
    (such as ed), sed works by making only one pass over the input(s), and
    is consequently more efficient. But it is sedās ability to filter text
    in a pipeline which particularly distinguishes it from other types of
    editors.
    Last edited by atreyu; 11-07-2012 at 03:19 AM. Reason: added code tags for readability

  11. #10
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Hello and welcome!

    You can use awk's range operators, which are basically two regexps. Here is an example:

    Code:
    cat output.txt|awk '/DESCRIPTION last/,/gawk/'
    If your text really does not have the "last" in "DESCRIPTION last", then we might have to get trickier...
    Last edited by atreyu; 11-07-2012 at 03:22 AM. Reason: typo

Page 1 of 2 1 2 LastLast

Posting Permissions

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