The powerful grep

Source: Internet
Author: User

Grammar

# grep Match_pattern filename//will output content that complies with the Match_pattern rules, match pattern is a wildcard character

# grep-e Match_pattern filename//The match_pattern here is a regular expression

# grep-o-e match_pattern filename//output only matching parts

# grep-v Match_pattern filename//contains rows outside the line that Math_pattern matches

# grep-c "text" FileName//Match string number of lines

# Egrep-o Match_pattern | Wc-l//Number of occurrences of a string

# grep "Text". -r-n//Recursive search file

# GREP-E MATCH_PATTERN-E match_pattern//Match multiple styles

# grep-f Pattern_file source_filename//Match multiple styles, styles appear as files

# grep match_pattern-a 3//3 lines after a result

# grep Match_pattern-b 3//3 lines before a result

# grep match_pattern-c 3//front and back 3 rows of a result

Apply the line that appears in the string "Linux" in the search file

# grep Linux-n sample.txt
1:GNU is not Linux
2:linux is fun

Search for multiple file strings "Linux" lines that appear

# grep Linux-n sample.txt sample2.txt
SAMPLE.TXT:1:GNU is not Linux
Sample.txt:2:linux is fun
Sample2.txt:4:planetlinux

Search for string "is" using regular expressions

# egrep "[i|s]+" Sample.txt
GNU is not Linux
Linux is fun
Bash is art

Calculates the position of the matching string not appearing throughout the file

# egrep "[i|s]+"-b-o sample.txt
4:is
23:is
35:is

Search multiple files and find out which file the matching text is in

# grep-l Linux sample.txt sample2.txt
Sample.txt
Sample2.txt

Search for files that do not contain matching text

# grep-l Linux sample.txt sample2.txt

Search files recursively

# grep "Linux". -r-n

./sample2.txt:4:planetlinux
./data.txt:4:4 Linux 1000
./NEWDIR/SAMPLE.TXT:1:GNU is not Linux
./newdir/sample.txt:2:linux is Fun
./SAMPLE.TXT:1:GNU is not Linux
./sample.txt:2:linux is Fun

Ignore case when searching for files

# grep-i "LINUX" sample.txt
GNU is not Linux
Linux is fun

Match multiple styles

# GREP-E "Gun"-E "Linux" Sample.txt
GNU is not Linux
Linux is fun

Include or exclude files in your search

# grep "Hello". -R--include *.txt
Out.txt:hello

# grep "Hello". -R--exclude "*.txt"
./test:hello

Whether the file contains the specified text
#!/bin/Bashif[$#-ne2 ]; ThenEcho "$ match_text filename"fiMatch_text=$1filename=$2grep-q $match _text $filenameif[$?-eq0 ]; Then Echo "The text exsit"ElseEcho "text doesn ' t exsit"fi

#./silent_grep.sh Student Stu_data.txt
The text exsit

The powerful grep

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.