Closed Thread
Results 1 to 6 of 6

Thread: Grep Problem

  1. #1
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Grep Problem

    I'm trying to do two things at once with a grep command. Basically, I have a file like this:

    Code:
    something
    ##matching line 1
    ##matching line 2
    ###matching line 3
    stuff that won't match
    What I want to do is use grep or some similar utility so I can grab all lines that start with ##, but without the pound signs. My output would be:

    Code:
    matching line 1
    matching line 2
    #matching line 3
    I know I want something like the inverse of the -o option, i.e. printing out the part of each matching line that didn't match. Does anyone know how to do this in any way?
    sudo rm -rf /

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    asafe's Avatar
    asafe is offline Programmer
    Join Date
    Jul 2009
    Location
    Here
    Posts
    110
    Rep Power
    10

    Re: Grep Problem

    Quote Originally Posted by dargueta View Post
    Does anyone know how to do this in any way?
    Well, you said any way:
    Code:
     a=$(cat file|sed -n /##/p |cut -c  3-)
     echo $a
    Output:
    matching line 1
    matching line 2
    #matching line 3

  4. #3
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Grep Problem

    That works great! Thanks. I had no idea cut existed. I also found another way, slightly shorter:

    Code:
    grep '##' file | cut -c 3-
    sudo rm -rf /

  5. #4
    asafe's Avatar
    asafe is offline Programmer
    Join Date
    Jul 2009
    Location
    Here
    Posts
    110
    Rep Power
    10

    Re: Grep Problem

    Quote Originally Posted by dargueta View Post
    That works great! Thanks. I had no idea cut existed. I also found another way, slightly shorter:

    Code:
    grep '##' file | cut -c 3-
    I was going to post a shorter version sed, but you already have the solution.

  6. #5
    DarkLordoftheMonkeys's Avatar
    DarkLordoftheMonkeys is offline Programming Professional
    Join Date
    Oct 2009
    Location
    Massachussets
    Posts
    255
    Blog Entries
    56
    Rep Power
    11

    Re: Grep Problem

    I don't know a lot about Unix, but if there is a program that can take text as input and delete certain characters, you could pipe grep's output into that.

  7. #6
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Grep Problem

    That would be cut.
    sudo rm -rf /

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. If problem or cout problem?
    By chaoticape in forum C and C++
    Replies: 4
    Last Post: 06-10-2011, 10:29 AM
  2. C: Problem with solving problem
    By rakche in forum C and C++
    Replies: 15
    Last Post: 03-28-2010, 01:24 PM
  3. Problem - Grep cammand in shell script
    By prasannasupp in forum Linux Programming and Scripting
    Replies: 2
    Last Post: 09-08-2009, 09:36 AM
  4. Using Grep
    By mop in forum Linux/Unix General
    Replies: 3
    Last Post: 05-31-2008, 08:52 AM
  5. Replies: 0
    Last Post: 04-26-2007, 05:33 PM

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