Jump to content

HowTo: Use grep Command In Linux / UNIX – Examples


securitybreach

Recommended Posts

securitybreach

The grep command tutorial on a Unix/Linux for new Linux and Unix users/sysadmins/devops:

 

grep intro http://www.cyberciti...-in-linux-unix/

 

grep regex http://www.cyberciti...rep-regular-expressions/ and more.

 

#unix #linux #osx #grepcommand #bsd

https://plus.google....sts/hNJXu2Yzi2p

 

HowTo: Use grep Command In Linux / UNIX – Examples

 

How do I use grep command on Linux, Apple OS X, and Unix-like operating systems? Can you give me a simple examples of the grep command?

 

The grep command is used to search text or searches the given file for lines containing a match to the given strings or words. By default, grep displays the matching lines. Use grep to search for lines of text that match one or many regular expressions, and outputs only the matching lines. grep is considered as one of the most useful commands on Unix and other Linux operating systems.

 

Did you know?

 

The name, "grep", derives from the command used to perform a similar operation, using the Unix/Linux text editor ed:

g/re/p

 

The grep command syntax

 

The syntax is as follows:

 

grep 'word' filename

 

grep 'word' file1 file2 file3

 

grep 'string1 string2' filename

 

cat other file | grep[/b] 'something'

 

command | grep' something'

 

command option | grep 'data'

 

grep --color 'data' filename

 

How do I use grep command to search a file?

 

Search /etc/passwd file for boo user, enter:

$ grep boo /etc/passwd

 

Sample outputs:

foo:x:1000:1000:foo,,,:/home/foo:/bin/ksh

 

You can force grep to ignore word case i.e match boo, Boo, BOO and all other combination with the -i option:

]$ grep -i "boo" /etc/passwd

http://www.cyberciti...-in-linux-unix/

  • Like 1
Link to comment
Share on other sites

I love grep, one of my favorite tools, use it every day. However, I think the writer might be wrong about where the name comes from, I was told it was General Regular Expression Parser. Whatever.

 

I first used it on IBM desktops back in the late 1980's, came in a package called Korn Shell Tools. Also had fgrep and egrep, and all the common command line utils. Nothing better for searching log files.

 

Grep also has a very useful -f switch, where you can give it the name of a text file which is a list of search strings, as many as you want. I used it extensively on a Sun Spark box in the runup to Y2K to find problem code in our company product.

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...