Jump to content

Grep Problem

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
dargueta

dargueta

    Writes binary right handed and hex left handed

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

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:

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
asafe

asafe

    Programmer

  • Members
  • PipPipPipPip
  • 107 posts

dargueta said:

Does anyone know how to do this in any way?

Well, you said any way:
 a=$(cat file|sed -n /##/p |cut -c  3-)

 echo $a

Output:
matching line 1
matching line 2
#matching line 3

#3
dargueta

dargueta

    Writes binary right handed and hex left handed

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

grep '##' file | cut -c 3-

sudo rm -rf /

#4
asafe

asafe

    Programmer

  • Members
  • PipPipPipPip
  • 107 posts

dargueta said:

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


grep '##' file | cut -c 3-


I was going to post a shorter version sed, but you already have the solution.

#5
DarkLordoftheMonkeys

DarkLordoftheMonkeys

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 255 posts
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.

#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,721 posts
That would be cut.
sudo rm -rf /