Showing posts with label Pipe Command. Show all posts
Showing posts with label Pipe Command. Show all posts

Tuesday, 21 April 2020

An `egrep` example with multiple regular expressions

Linux Tutorial and Material, Linux Guides, Linux Certifications, Linux egrep, LPI Exam Prep

Summary: How to use the Linux egrep command with multiple regular expressions (regex patterns).

As a quick note here today, I just used the Linux egrep command to perform a case-insensitive search on multiple regular expressions (regex patterns). Really, what I did was a little more complicated:

locate -i calendar | grep Users | egrep -vi 'twiki|gif|shtml|drupal-7|java|PNG'

As you can see from that command, I did this:

◉ Used to locate command with the case-insensitive option to find all files with the string "calendar" in them.

◉ Used the grep command so the output would only display files and directories with the string "Users" in them.

◉ Used the egrep command with multiple regex patterns to reduce the output much more. I used the -v argument to perform the "opposite" meaning of a normal egrep command, so strings with these patterns were not shown; and also used the -i argument to perform a case insensitive egrep search here.

While my original locate -i calendar command shows nearly 3,000 files, the locate command combined with grep and egrep in this manner shows only 15 files.

An easier egrep command


Before I go away, here's an easier egrep command to look at:

egrep 'apple|banana|orange' *

That egrep command searches for those three strings (regular expressions, really) in all files in the current directory. This next command does the same thing, but in a case-insensitive manner:

egrep -i 'apple|banana|orange' *

Sunday, 19 April 2020

Linux pipe command examples (command mashups)

One of my favorite things about Unix, Linux, and Mac OS X systems is that you can create your own commands by merging other commands. There isn't any formal name for these command combinations, other than to say that you're "piping" commands together, so I recently started referring to these as "command mashups".

Linux Pipe Command, Linux Tutorials and Materials, LPI Study Materials

Here's a simple pipeline command I use all the time, creating a long list of files and piping the output into the Linux more command:

ls -al | more

Here's a pipeline command mashup I use to get a list of all sub-directories in the current directory:

ls -al | grep '^d'

That pipe command works because the grep command only shows the file records that begin with the letter d.

Here's how I list all the processes running on a Unix system that include the string "fred":

ps auxwww | grep fred | more

That helps me find processes that might be owned by a user named "fred".

Read More: LPI Linux Essentials

Here's an example of how I list all the files in my Apache log directory for the month of August in the current year:

ls -al | grep Aug | grep -v '200[456]' | more

I use that grep command to filter out files from the years 2004, 2005, and 2006, as all the log files for several years are currently in one directory.

This ps and wc command pipeline gives you an idea of how many processes are currently running on your system:

ps aux | wc -l

The number returned is actually high by one because the first line of output from the ps command is a header, and not a process.

This command that combines locate and grep helps me find Java files on my computer:

locate "*.java" | grep java

Of course there are many more mashups you can create, just about as many as your imagination allows. I just wanted to show these examples so you'd see how they work.

The basic concept is that you run one command, and then pipe the output from that command into another, and then another, etc. This is formally referred to as piping the "standard output" ("standard out", "STDOUT") from one command to the "standard input" ("standard in", "STDIN") of another command.