• 0

unix wildcard grep


Question

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?

Link to comment
https://www.neowin.net/forum/topic/1145704-unix-wildcard-grep/
Share on other sites

8 answers to this question

Recommended Posts

  • 0
  On 06/04/2013 at 11:26, luc9 said:

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!

  • 0

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 [^#]?

  • 0

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 $?

$ 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

  • 0

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!!

  • 0

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.

  • 0

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

  • 0

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.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Brave... the crypto browser with Brave ad rewards and Leo AI everywhere is not an issue for you and Firefox is? At least in Firefox you can disable everything you don't want easily in about:config and everything you don't want is REALLY removed even from the UI. in Brave the only way to really disable all this stuff and them to completely removed from the UI is to use administrator policies. You can only have them turned off without admin policies.
    • The UK shouldn't copy Trump. If the UK wants its own AI Industry it needs to build one, it also need to sort out the issue of startups flying away to America.
    • Azure Linux 2.0 reaches end of life, requiring AKS Arc users to upgrade by Paul Hill Microsoft has warned that Azure Linux 2.0, used in Azure Kubernetes Service (AKS) enabled by Azure Arc, will reach its end of life on July 31, 2025, necessitating users to upgrade. After this date, Microsoft will no longer provide updates, security patches, or support for Azure Linux 2.0. The longer it is used after this date, the more vulnerable systems will become due to a lack of patches. Azure Linux 3.0 brings significant upgrades to core components that enhance performance, security, and the developer experience. The Linux kernel is upgraded from version 5.15 to 6.6, bringing performance and hardware compatibility improvements. The Containerd package has been updated from version 1.6.26 to 1.7.13, improving container management. The SystemD package has been upgraded from version 250 to 255, streamlining system and service management, and OpenSSL has jumped from version 1.1.1k to 3.3.0, providing enhanced encryption and security. Azure Linux 3.0 also brings more packages and better tooling. Major versions of Azure Linux are typically supported for three years, with Azure Linux 3.0’s EOL projected for Summer 2027. It became generally available in August 2024.Microsoft said that users must migrate to Azure Linux 3.0 by upgrading their Azure Local instances to the 2507 release when it becomes available. After the Azure Local instance has been upgraded, users can then upgrade their Kubernetes clusters. Microsoft gives you the option to remain on the same Kubernetes version during this upgrade by providing the same version number on the aksarc upgrade command. After upgrading, you can verify the kernel version on your Linux nodes by adjusting the file path in this command: kubectl --kubeconfig /path/to/aks-cluster-kubeconfig get nodes -o wide This upgrade is mandatory for continued support. If you want to learn more, check out Microsoft’s announcement which also includes how to reach out to the AKS Arc team if you need to.
    • PDF-XChange Editor 10.6.1.397 by Razvan Serea PDF-XChange Editor is a comprehensive PDF editor that allows you to create, view, edit, annotate, and digitally sign PDF documents with ease. With advanced features like OCR, document security, and PDF optimization, PDF-XChange Editor is a powerful tool for both personal and professional use. Whether you need to edit text, images, or links, or add comments, stamps, or watermarks, PDF-XChange Editor provides all the necessary tools to make your PDFs look perfect. Additionally, it supports a wide range of file formats, including PDF, XPS, and DOCX, making it easy to convert and share your documents. PDF-XChange Editor key features: Edit text and images in PDF documents Add and remove pages from PDF files Annotate and markup PDFs with comments, highlights, and stamps Use OCR to convert scanned documents into searchable text Create and fill out PDF forms Sign and certify PDF documents digitally Add and edit hyperlinks within PDFs Extract text and images from PDF files Batch process multiple PDF files at once Customize the interface to your preferences Work with multiple documents in tabs Convert PDFs to other formats such as Word, Excel, and HTML Use advanced redaction tools to permanently remove sensitive information Add customizable headers and footers to PDFs Merge multiple PDF documents into a single file Split PDF documents into multiple files Add watermarks to PDF documents Use the measurement tools to calculate distances and areas in PDFs ....and much more PDF-XChange Editor 10.6.1.397 changelog: New Features Added offline support for MIP-protected documents. Bug Fixes & Improvements Fixed some security and stability issues. Click here for further information. Fixed an issue with DocuSign when a document name is very long. Fixed a very rare issue when opening password-protected files. (46285) Fixed an issue with moving content items in some cases, where arrow keys became unexpectedly deselected under certain conditions. (T# 7476) (40279) Fixed an issue with disabling the Zoom In/Out tool after using the Snapshot tool. (46330) Fixed an issue with an unintentional button press when the Autoscroll command was executed under specific conditions. (46289) Fixed an issue with resetting object selections when panning with mouse middle button. (46358) Fixed an issue with adding a highlight when starting to drag directly above the comment that is itself above base content text. (46410) Fixed an issue with adding a guideline by dragging from the ruler while a text comment or base content is being edited. (45858) Fix the call to app.clearTimeOut after the timeout handler was executed and the associated timer was unregistered. Improved static XFA support. Fixed an issue when adding Insert/Replace text markup comments to the first word in a text line. (47120) For a complete list of changes and more detailed information, please refer to the official PDF-XChange Editor Version History. Download: PDF-XChange Editor (64-bit) | Portable ~300.0 MB (Shareware) Download: PDF-XChange Editor (32-bit) | Portable ~200.0 MB Download: PDF-XChange ARM64 | 264.0 MB Download: PDF-XChange Portable @PortableApps.com | 97.0 MB View: PDF-XChange Editor Website | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • Explorer
      DougQuaid went up a rank
      Explorer
    • Week One Done
      MIghty Haul earned a badge
      Week One Done
    • One Month Later
      MIghty Haul earned a badge
      One Month Later
    • Collaborator
      KD2004 earned a badge
      Collaborator
    • One Month Later
      ataho31016 earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      597
    2. 2
      Michael Scrip
      201
    3. 3
      ATLien_0
      192
    4. 4
      +FloatingFatMan
      140
    5. 5
      Xenon
      127
  • Tell a friend

    Love Neowin? Tell a friend!