Sign in to follow this  
Followers 0

unix wildcard grep

9 posts in this topic

Posted

i'm trying to type a command that starts with grep -e blah blah in putty (using windows) and here's the following msg i get:

grep: illegal option -- E

Usage: grep -hblcnsviw pattern file . . .

i've even tried using egrep, but no response.

anyone know how what command i can type instead?

Share this post


Link to post
Share on other sites

Posted

i'm trying to type a command that starts with grep -e blah blah in putty (using windows) and here's the following msg i get:

grep: illegal option -- E

Usage: grep -hblcnsviw pattern file . . .

i've even tried using egrep, but no response.

anyone know how what command i can type instead?

Well you can use grep without -e and it will allow basic regular expressions just fine.

Alternatively you could use Perl to do it if that is available.

Out of curiosity what OS are you running? Most POSIX operating systems have included egrep since the mid-1970s!

Share this post


Link to post
Share on other sites

Posted

i've tried this as an instance

egrep '\b[A-Z]+\b' nameofthefile.sh

and i get no result..

also, can you help me out with this command please?

i am supposed to pick up a file to print every line within them that contains both code and a comment

and heres the answer:

grep -E '^[^#]+#' nameofthefile.sh

i'm confused how ^[^#] comes from? doesn't [^#] literally mean comment, but not the code? and how come you put another carot sign before [^#]?

Share this post


Link to post
Share on other sites

Posted

Regular expressions are fun aren't they :p

I'll try and break down the regular expression for you:


^[^#]+#

??????? 3. Then match a hash symbol

?  ? 2. Match one or more characters that is NOT one of the specified characters (the caret inside square brackets indicates that a NEGATIVE match)

? 1. Match the start of the line.

In this case, the caret means two different things depending on where you place it. Inside square brackets, it acts like a NOT operator. When you say you get no response from egrep, are you saying you get no output at all? That would normally suggest that egrep is running, but not finding any matches. Try the following.
  1. Do your egrep statement again.
  2. As soon as you see the prompt return, type "echo $?", which will display the return code of the previous command. Zero is usually success, any other number will usually tell you what the problem was.
  3. Check the the number
    • If the number is 127, that means that egrep is not available.
    • If the number is 1, that means that egrep is installed, but no match was found in the file.
    • If the number is 2, that means that egrep encountered an error.
Example from one of my command prompts:

$ egrep '.*' .bashrc

<snipped output>

$ echo $?

0

$ egrep '^te[sx]t' .bashrc

$ echo $?

1

$ egrep '.*' file_that_doesnt_exist.sh

grep: file_that_doesnt_exist.sh: No such file or directory

$ echo $?

2

$ commandthatdoesntexist --help

bash: commandthatdoesntexist: command not found

$ echo $?

127

Share this post


Link to post
Share on other sites

Posted

thank u so much!!!!!!!!!!!!!

i think i was able to understand the syntax u were trying to explain!!

have to look at over and over again to make sure that i'm clear with it haha

for the egrep command, i did as you have told me, and i got a result 1. i'm assuming the reason why i have not received any output is because within the file then.

thanks once again!!

Share this post


Link to post
Share on other sites

Posted

Yes a return code of "1" from egrep suggests that it couldn't find any lines in the file that matched your regex, and no problem, happy to help :).

If you're looking at polishing up your regex knowledge, I'd recommend looking here: http://www.regular-expressions.info/reference.html. There's some decent information there that should be able to help you out.

Share this post


Link to post
Share on other sites

Posted

I have been searching this a lot, but i am still not clear. what does word boundary mean? what does it

do? so say with this command, could someone explain this to me please?

egrep'\b[A-Z]+\b' filename.sh

and when you use inverse in grep (egrep -v), this literally means the output is shown upside down (flipped over)?

i think i was confused more as i was seeing the following examples

$ egrep -v '\ ' blah.sh

$ egrep -v '(\ )|^$' blah.sh

i'm confused what '\ ' does, and what happens if you put -v. also what is the difference between '\ ' and '(\ )' command if we are excluding |^$ command? (i know it deals with finding lines that have empty spaces)

thank you

Share this post


Link to post
Share on other sites

Posted

A word boundary is similar to the caret (^) and ($), but matches the beginning and/or end of a word. What characters constitutes a "word" depends on the implementation. You can see an example of word boundaries in action here.


The -v flag does an "inverse grep", which means that it will print out lines that DON'T match the regular expression. Take the following example file:


foo.txt

--------------------------------------

1 |  This line is good.

2 |  This line is bad.

3 |  This line is odd numbered.

4 |  This line is even.

If we now do...

grep 'o' foo.txt

.. grep will find all lines containing an 'o' in them (lines 1 and 3) However, if we do...

grep -v 'o' foo.txt

... grep will find all lines that DON'T have an 'o' in them (lines 2 and 4)
(If you don't want to know what the backslash character does, skip to the bottom for an explanation about your regular expression!) The backslash character (\) is used to escape special characters, as well as identify special match patterns. So in normal usage, you'll see special match patterns like "\b" for a word boundary, or "\s" for whitespace, but consider the following file...

bar.txt

--------------------------------------

1 |  Jeff (or as he was sometimes called, Jeffrey), was a man of few words.

2 |  He would often go for days without speaking, and months without

3 |  having a conversation. But he was a man of honour, and rarely had a

4 |  bad word to say about anyone (except the Scots).

Let us say that we want to find all the lines in bar.txt that had something inside brackets (i.e. line 1 and line 4), so we create a regular expression like this:

egrep '(.*)'

What output does this produce?

$ egrep '(.*)'  test.txt

Jeff (or as he was sometimes called, Jeffrey), was a man of few words.

He would often go for days without speaking, and months without

having a conversation. But he was a man of honour, and rarely had a

bad word to say about anyone (except the Scots).

Why was every line printed? In regular expressions, brackets allow you to create a sub-expression that you can use in special ways, so the '(.*)' regular expression doesn't match brackets, it matches anything (because of '.*'). You can see how this regular expression gets matched by Ruby here. So how do we match brackets (or square brackets like [ or ], or dollars, or backslashes)? We use a backslash to escape the character, and tell grep to treat it as a normal character. Like this:

egrep '\(.*\)'  test.txt

Now the output is as we expected:

$ egrep '\(.*\)'  test.txt

Jeff (or as he was sometimes called, Jeffrey), was a man of few words.

bad word to say about anyone (except the Scots).

In the regex you provided however, the backslash doesn't actually do anything, since the space is being escaped, and it doesn't need to be. You are correct in saying that '(\ )' and ' ' are basically the same (brackets, as I mentioned, have a special meaning, but grep will ignore them in this regex), so I don't really know what the '\' is there for. The brackets and backslash are redundant.

Share this post


Link to post
Share on other sites

Posted

Just thought I willl add this....

You might need a escape char for the '+'. Grep searches for 'plus' in the pattern you mentioned and not the greedy pattern that you seem to be trying to get.

Try this....

egrep"\b[A-Z]\+\b" filename.sh

Cheers

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.