Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ grep command Series: How to Use the grep command in Linux / UNIX     - The Definitive Guide to Linux NAT network connection (Linux)

- Linux Shell Scripting multithreading (Programming)

- APT-mirror using a four-step configuration Ubuntu local depot (Linux)

- Dockerfile use to build a mirror-based CentOS 7 (Linux)

- Linux / UNIX: Use the dd command to create a 1GB size binary (Linux)

- ORA-04031 error appears in the solution 11.2.0.4 run utlrp.sql (Database)

- Easy to install CentOS 6.6 desktop environment (Linux)

- Solve ORA-01012: not logged on (Database)

- Examples of testing and installation Mesos on CentOS (Linux)

- OpenGL shadow map (Programming)

- How to convert images, audio and video formats on Ubuntu (Linux)

- Cacti monitoring service Nginx (Linux)

- Java integrated development environment common set of operations (Linux)

- expdp reported ORA-39181 Export Processing Method (Database)

- Recover accidentally deleted Nginx logs (Server)

- To install PXE + Kickstart under CentOS 6.x operating system (Linux)

- Create a custom pixel format based on an existing image data BufferedImage (Programming)

- Linux centralized log server rsyslog (Server)

- PostgreSQL export data dictionary documents (Database)

- How to install the client sqlplus under linux (Database)

 
         
  grep command Series: How to Use the grep command in Linux / UNIX
     
  Add Date : 2018-11-21      
         
         
         
  How do I use in Linux, Apple OS X systems and other UNIX grep command, you give me to show some simple examples?

grep command to search for text or from a given file search within a file line contains the given string or word. Generally speaking, grep to display matching lines. Use grep to search for the one or more regular expressions to match lines of text, and to show only the matching rows. grep is regarded as one of the most useful in Linux / Unix system commands.

do you know

grep the name comes from a Unix / Linux command similar operation in the old line editor ed performed:

g / re / p
 

The syntax of the grep command

The syntax is as follows:

grep 'word' filename
grep 'word' File 1 File 2 File 3
grep 'string 1 string 2' file name
cat a file | grep 'something'
command | grep 'something'
command options 1 | grep 'data'
grep --color 'data' file name
 

How to use grep to search for a file

boo user searches for / etc / passwd file under, enter:

$ Grep boo / etc / passwd
Output:

foo: x: 1000: 1000: foo ,,,: / home / foo: / bin / ksh
You can use grep to ignore case mandatory. For example, use the -i option to match boo, Boo, BOO and other combinations:

$ Grep -i "boo" / etc / passwd
 

Recursively grep

You can use grep recursively search. For example, search for all files that contain the string "192.168.1.5" in the file directory

$ Grep -r "192.168.1.5" / etc /
or:

$ Grep -R "192.168.1.5" / etc /
Sample output:

/ Etc / ppp / options: # ms-wins 192.168.1.50
/ Etc / ppp / options: # ms-wins 192.168.1.51
/ Etc / NetworkManager / system-connections / Wired connection 1: addresses1 = 192.168.1.5; 24; 192.168.1.2;
You will see the search results to 192.168.1.5 Each line prefix to find the matching file names (for example: / etc / ppp / options). Output file name being included may add -h option to disable the output:

$ Grep -h -R "192.168.1.5" / etc /
or

$ Grep -hR "192.168.1.5" / etc /
Sample output:

# Ms-wins 192.168.1.50
# Ms-wins 192.168.1.51
addresses1 = 192.168.1.5; 24; 192.168.1.2;
 

Use grep to search for text

When you search for boo when, grep command will match fooboo, boo123, barfoo35 and all other string contains boo, you can use the -w option to force output only those lines (LCTT Universe contains only the whole word: that the character both sides of the string is the English word delimiters, such as spaces, punctuation, and so on end, so that there is no hyphenation Chinese sign language does not apply.).

$ Grep -w "boo" file
 

Use the grep command to search two different words

Use egrep command is as follows:

$ Egrep -w 'word1 | word2' / path / to / file
(LCTT Annotation: here to use regular expressions, so use the egrep command, that extended grep command.)

 

Statistics text to match the number of rows

grep command to display each file to match the number of parameters by adding -c:

$ Grep -c 'word' / path / to / file
It was added to the match line before the line can pass the -n option to output the line number:

$ Grep -n 'root' / etc / passwd
Sample output:

1: root: x: 0: 0: root: / root: / bin / bash
1042: rootdoor: x: 0: 0: rootdoor: / home / rootdoor: / bin / csh
3319: initrootapp: x: 0: 0: initrootapp: / home / initroot: / bin / ksh
 

Invert the match (mismatch)

You can use the -v option to output the contents do not contain a match, the output includes only those that contain a given word line, for example, all the output line does not contain the word bar:

$ Grep -v bar / path / to / file
 

UNIX / Linux Pipeline and grep command

grep is often used in conjunction with the pipe, in this example, displays the name of the hard disk device:

# Dmesg | egrep '(s | h) d [a-z]'
Shows the CPU type:

# Cat / proc / cpuinfo | grep -i 'Model'
However, the above command can also use the following methods, do not use the pipe:

# Grep -i 'Model' / proc / cpuinfo
Sample output:

model: 30
model name: Intel (R) Core (TM) i7 CPU Q 820 @ 1.73GHz
model: 30
model name: Intel (R) Core (TM) i7 CPU Q 820 @ 1.73GHz
 

Only shows how to match the contents of a file name?

Use the -l option to display the file name of the file that contains the contents of main ():

$ Grep -l 'main' * .c
Finally, you can force grep to output color:

$ Grep --color vivek / etc / passwd
     
         
         
         
  More:      
 
- How to import JNI resulting .so libraries in Android Studio (Programming)
- Apache POI Excel Document Processing (Linux)
- ElasticSearch basic usage and cluster structures (Server)
- How Vim playing a mature IDE (Linux)
- After Pydev installation, Eclipse does not display solutions (Linux)
- Linux / Windows setup is complete port range (Linux)
- MySQL and MariaDB traditional master-slave cluster configuration (Database)
- CentOS 6.5 configuration SSDB 1.8.0 (Server)
- Java Virtual Machine Basics (Programming)
- Ubuntu 12.04 installation OpenCV2.4.1 and compile test (Linux)
- How to Check notebook CPU temperature in Ubuntu (Linux)
- Implement Oracle dynamic registration of non-standard port 1521 (Database)
- Linux use logs to troubleshoot (Linux)
- Setup Qt development environment under Linux (Linux)
- MongoDB upgrade to 2.6 (Database)
- CentOS RedHat YUM Source Extensions Supplement (including 32-bit, 64-bit) (Linux)
- C ++ runtime environment built on CentOS 6.6 and Oracle database connection (Database)
- Grading defense against Linux server attacks (Linux)
- RHEL5 / 6 Installation Notes (Linux)
- Run Windows in FreeBSD bhyve (Linux)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.