grep for this text - Unix

This is a discussion on grep for this text - Unix ; hi, ...... ABC (x,y,z,a,b,c) ABC (x,y,z,a,b,c) ABC (x,y,z,a,b,c) ...... how can i grep for z here, z can be anything but it always occurs at the same position. ABC is a constant here....

+ Reply to Thread
Results 1 to 10 of 10

Thread: grep for this text

  1. grep for this text

    hi,
    ......
    ABC (x,y,z,a,b,c)
    ABC (x,y,z,a,b,c)
    ABC (x,y,z,a,b,c)
    ......

    how can i grep for z here, z can be anything but it always occurs at
    the same position.
    ABC is a constant here.


  2. Re: grep for this text

    Hi

    On Sun, 14 Sep 2008 21:57:59 -0700, sinbad wrote:
    > .....
    > ABC (x,y,z,a,b,c)
    > ABC (x,y,z,a,b,c)
    > ABC (x,y,z,a,b,c)
    > .....
    >
    > how can i grep for z here, z can be anything but it always occurs at the
    > same position.
    > ABC is a constant here.


    grep '^ABC ([^,]*,[^,*],z,[^,*],[^,*],[^,*])$'


  3. Re: grep for this text

    On Sep 15, 12:01*pm, viza
    wrote:
    > Hi
    >
    > On Sun, 14 Sep 2008 21:57:59 -0700, sinbad wrote:
    > > .....
    > > ABC (x,y,z,a,b,c)
    > > ABC (x,y,z,a,b,c)
    > > ABC (x,y,z,a,b,c)
    > > .....

    >
    > > how can i grep for z here, z can be anything but it always occurs at the
    > > same position.
    > > ABC is a constant here.

    >
    > grep '^ABC ([^,]*,[^,*],z,[^,*],[^,*],[^,*])$'



    i think i didn't explain well,

    ......
    ABC (x,y,one,a,b,c)
    ABC (x,y,two,a,b,c)
    ABC (x,y,three,a,b,c)
    ......

    In the above text i need to get the output as

    one
    two
    three

    thanks

  4. Re: grep for this text

    sinbad writes:

    > i think i didn't explain well,
    >
    > .....
    > ABC (x,y,one,a,b,c)
    > ABC (x,y,two,a,b,c)
    > ABC (x,y,three,a,b,c)
    > .....
    >
    > In the above text i need to get the output as
    >
    > one
    > two
    > three


    grep ^ABC test | cut -d, -f3

    --
    Ben.

  5. Re: grep for this text

    On Sep 15, 3:25*pm, Ben Bacarisse wrote:
    > sinbad writes:
    >
    >
    >
    > > i think i didn't explain well,

    >
    > > .....
    > > ABC (x,y,one,a,b,c)
    > > ABC (x,y,two,a,b,c)
    > > ABC (x,y,three,a,b,c)
    > > .....

    >
    > > In the above text i need to get the output as

    >
    > > one
    > > two
    > > three

    >
    > * grep ^ABC test | cut -d, -f3
    >
    > --
    > Ben.



    ABC (x,y,one,a,b,c)

    but what if the above text is not always in a single line,It can span
    multiple lines (which usually is the case for me)
    like

    ABC (x,y,
    one,a,
    b,
    c)

    ABC (x,y,
    two,a,
    b,c)

    ABC (x,y,
    three,
    a,b,c)

    ABC (x,
    y,four,a,b,c)

    Even in the above case, i need to get the output as

    one
    two
    three
    four

    I am wondering if it's atleast possible with grep.

    thanks

  6. Re: grep for this text

    On Mon, 15 Sep 2008 03:51:21 -0700, sinbad wrote:
    >> sinbad writes:


    >> > i need to get the output as
    >> > one


    > ABC (x,y,
    > one,a,
    > b,
    > c)



    sed -f foo.sed

    where foo.sed contains:

    /^ABC/bn
    d
    :n
    /,[[:space:]]*$/N
    /,[[:space:]]*\n/s//,/
    tn
    s/^[^,]*,[^,]*,\([^,]*\),.*$/\1/
    t
    d

  7. Re: grep for this text

    On Mon, 15 Sep 2008 11:29:06 +0000, viza wrote:

    > /^ABC/bn
    > d
    > :n
    > /,[[:space:]]*$/N
    > /,[[:space:]]*\n/s//,/
    > tn
    > s/^[^,]*,[^,]*,\([^,]*\),.*$/\1/
    > t
    > d


    NB: this requires that each broken line ends with a comma (and optional
    space).

    NB2: this silently discards ABC(...) without enough argument

  8. Re: grep for this text

    On Monday 15 September 2008 12:51, sinbad wrote:

    >> grep ^ABC test | cut -d, -f3
    >>
    >> --
    >> Ben.

    >
    >
    > ABC (x,y,one,a,b,c)
    >
    > but what if the above text is not always in a single line,It can span
    > multiple lines (which usually is the case for me)
    > like
    >
    > ABC (x,y,
    > one,a,
    > b,
    > c)
    >
    > ABC (x,y,
    > two,a,
    > b,c)
    >
    > ABC (x,y,
    > three,
    > a,b,c)
    >
    > ABC (x,
    > y,four,a,b,c)
    >
    > Even in the above case, i need to get the output as
    >
    > one
    > two
    > three
    > four
    >
    > I am wondering if it's atleast possible with grep.


    You can't with grep. Grep reads a single line at a time.
    Try with this sed program:

    /^$/d
    /^ABC/{
    :a
    /)$/!{N;ba;}
    }
    s/[[:space:]]*//g
    s/^[^,]*,[^,]*,\([^,]*\)\(,[^,]*\)\{3\}$/\1/'

    assuming all the records are well formed.


  9. Re: grep for this text

    On Sep 15, 3:51*am, sinbad wrote:

    > ABC (x,y,one,a,b,c)
    >
    > but what if the above text is not always in a single line,It can span
    > multiple lines (which usually is the case for me)
    > like
    >
    > ABC (x,y,
    > * * one,a,
    > * * b,
    > * * c)


    Though ugly, you can use 'tr' to convert newlines to spaces and ')'s
    to newlines.

    DS

  10. Re: grep for this text

    sinbad writes:

    > On Sep 15, 3:25*pm, Ben Bacarisse wrote:
    >> sinbad writes:
    >>
    >>
    >>
    >> > i think i didn't explain well,

    >>
    >> > .....
    >> > ABC (x,y,one,a,b,c)
    >> > ABC (x,y,two,a,b,c)
    >> > ABC (x,y,three,a,b,c)
    >> > .....

    >>
    >> > In the above text i need to get the output as

    >>
    >> > one
    >> > two
    >> > three

    >>
    >> * grep ^ABC test | cut -d, -f3
    >>
    >> --
    >> Ben.


    Best if you don't quote sigs.

    > ABC (x,y,one,a,b,c)
    >
    > but what if the above text is not always in a single line,It can span
    > multiple lines (which usually is the case for me)
    > like
    >
    > ABC (x,y,
    > one,a,
    > b,
    > c)


    OK. I'll wait until you confirm that this is the most complex format
    you will need. Please try to explain it in full generality. I don't
    fancy thinking it though and being ask for anther "what if"!

    --
    Ben.

+ Reply to Thread