Results 1 to 6 of 6

Thread: [bash] Grep strange behaviour

  1. #1
    Join Date
    Oct 2011
    Beans
    37

    [bash] Grep strange behaviour

    I was 'grep'ping a text file:
    Code:
    vvss@vvss-laptop:~/tmp $ cat abc.txt 
    abc pqr
    abcpqr
    pqr abc
    a pqr
    ab cpqr
    grep behaves strangely if I add the --color=always option parsed to grep -v. Following are the outputs of the three different option of --color and also without grep -v parsing:

    Code:
     vvss@vvss-laptop:~/tmp $ grep -r abc abc.txt | grep -v abcpqr
    abc pqr
    pqr abc
    Code:
     vvss@vvss-laptop:~/tmp $ grep -r --color=auto abc abc.txt | grep -v abcpqr
    abc pqr
    pqr abc
    Code:
    vvss@vvss-laptop:~/tmp $ grep -r --color=never abc abc.txt | grep -v abcpqr
    abc pqr
    pqr abc
    Code:
    vvss@vvss-laptop:~/tmp $ grep -r --color=always abc abc.txt | grep -v abcpqr
    abc pqr
    abcpqr
    pqr abc
    Code:
     vvss@vvss-laptop:~/tmp $ grep -r abc abc.txt | grep -v abcpqr
    abc pqr
    pqr abc
    What happens if you give --color=always option? Why does it ignore the grep -v parsing?

    Thanks in advance

    VInay
    Last edited by vinay_wagh; June 18th, 2013 at 07:45 AM.

  2. #2
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,153
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: [bash] Grep strange behaviour

    it puts formatting sequences in the output. When you print to terminal, it interprets these special sequences to change color to highlight matches, but when you pipe the colored output to another command they are polluting your data.
    use --color=always only in the last step, when you print out to terminal.

    If you want to see these sequences redirect grep output with --color=always to a file and open it in gedit. That's what your grep -v step gets
    Last edited by Vaphell; June 17th, 2013 at 06:14 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  3. #3
    Join Date
    Oct 2011
    Beans
    37

    Re: [bash] Grep strange behaviour

    Thanks Vaphell for the explaination.

    I want to use --color=always in the last step and wish to print the output to terminal. However while printing I dont want string 'abcpqr'. So logically I would parse it to grep -v. The reason I gave --color=always was I wanted coloured output at the end, but after parsing the colour effect vanishes.

    Now I agree with you for regarding formatting sequence. I could see that by parsing the output to less (without -R option).

    So to get the filtered output, redirecting is the only way out?

    VInay

  4. #4
    Join Date
    Oct 2011
    Beans
    37

    Re: [bash] Grep strange behaviour

    Ah silly me

    Here is a simplest solution:

    Code:
    grep -v abcpqr abc.txt | grep abc
    This gives me a coloured output without lines containing 'abcpqr'...

    VInay

  5. #5
    Join Date
    Jun 2006
    Location
    Brisbane Australia
    Beans
    713

    Re: [bash] Grep strange behaviour

    Code:
    grep -w abc abc.txt

  6. #6
    Join Date
    May 2009
    Location
    Courtenay, BC, Canada
    Beans
    1,583

    Re: [bash] Grep strange behaviour

    Quote Originally Posted by markbl View Post
    Code:
    grep -w abc abc.txt
    +1 fewest steps

    and as per usual, Vaphell knows what's wrong! I may like to contradict him sometimes, but the man knows his stuff

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
  •