In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to use grep, sed and awk in Shell regular expressions. It has certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let Xiaobian take you to understand it together.
First, copy an example:
The code is as follows:
# vi regular_express.txt
-------------------------------
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
this dress doesn't fit me.
However, this dress is about $ 3183 dollars.
GNU is free air not free beer.
Her hair is very beauty.
I can't finish the test.
Oh! The soup taste good.
motorcycle is cheap than car.
This window is clear.
the symbol '*' is represented as start.
Oh! My god!
The gd software is a library for drafting programs.
You are the best is mean you are the no. 1.
The world is the same with "glad".
I like dog.
google is the best tools for search keyword.
goooooogle yes!
go! go! Let's go.
# I am VBird
--------------------------------
Set the language family to C
The code is as follows:
#export LANG=C
grep
1. Search for specific string "the"
Note: n is the display line number
The code is as follows:
# grep -n 'the' regular_express.txt
2. Reverse search for specific string "the"
The code is as follows:
# grep -vn 'the' regular_express.txt
3. Get this string with any case of "the"
The code is as follows:
# grep -in 'the' regular_express.txt
4. Use brackets [] to search for set characters
When searching for the words test or taste, you find that they have a common't? st', so you can search like this
The code is as follows:
# grep -n 't[ae]st' regular_express.txt
So what we're looking for is t[a]st and t[e]st, two separate characters.
If you are searching for characters with oo, you can use:
The code is as follows:
# grep -n 'oo' regular_express.txt
If we don't want to find a g before oo when searching oo, we can use reverse selection [^] to achieve:
The code is as follows:
# grep -n '[^g]oo' regular_express.txt
If you don't want lowercase characters before the search oo, then:
The code is as follows:
# grep -n '[^a-z]oo' regular_express.txt
Note: Upper/Lower case English/Numbers can be written in [a-z]/[A-Z]/[0-9], etc., or can be written together
[a-zA-Z0-9] indicates that the required string is numeric and English
If we want to get the row with numbers, then:
The code is as follows:
# grep -n '[0-9]' regular_express.txt
Note: However, considering the influence of language families on coding order, in addition to using minus [-] for continuous coding,[:lower:] can also be used instead of a-z and [:digit:] can be used instead of 0-9.
The code is as follows:
# grep -n '[^[:lower:]]oo' regular_express.txt
# grep -n '[[:digit:]]' regular_express.txt
5. Display strings starting with 'the'
The code is as follows:
# grep -n '^the' regular_express.txt
Display lines begin with lowercase characters
The code is as follows:
# grep -n '^[a-z]' regular_express.txt
6. Display line ends with dots. the line that
The code is as follows:
# grep -n '\.$ ' regular_express.txt
7. Display 5-9 rows of data
The code is as follows:
# cat -An regular_express.txt |head -n 10 |tail -n 6
8. Display blank lines
The code is as follows:
# grep -n '^$' regular_express.txt
9. Find out g d string, four strings starting with g and ending with d
The code is as follows:
# grep -n 'g.. d' regular_express.txt
10. o* stands for empty characters (that is, with or without characters) or one to N o characters, so grep -n 'o*' regular_express.txt will print out all lines,
11.oo* stands for o+ null characters or one to N o characters, so grep -n 'oo*' regular_express.txt will print out all the lines o,oo,ooo, etc.
12. "goo*g" stands for gog,goog,gooog... such
The code is as follows:
# grep -n 'goo*g' regular_express.txt
13. Find out what contains g... g line of string
Note: Represents any character, .* then it represents a null character or one to N arbitrary characters.
The code is as follows:
# grep -n 'g.* g' regular_express.txt
14. Find the rows that contain numbers
The code is as follows:
# grep -n '[0-9][0-9]*' regular_express.txt
or # grep -n '[0-9]' regular_express.txt
15. Find a string with two o's
Note:{} Because it has a special meaning in the shell, it needs to add the escape character\to make it meaningless.
The code is as follows:
# grep -n 'o\{2\}' regular_express.txt
Find a string with 2 to 5 o's after g and ending with g
The code is as follows:
# grep -n 'go\{2,5\}g' regular_express.txt
Find a string with an o after g and then ending in g
The code is as follows:
# grep -n 'go\{2,\}g' regular_express.txt
Summary:
^word indicates string with search (word) at the beginning of the line
word$indicates the string with search (word) at the end of the line
. Represents 1 arbitrary character
\means escape character, adding\before special character will remove the original special character meaning
* Represents repeating 0 to an infinite number of previous RE(regular expression) characters
[list] means to search for strings containing lists
[n1-n2] means to search the specified string range, for example,[0-9] [a-z] [A-Z], etc.
[^list] indicates a range of inverted strings, e.g.[0-9] for non-numeric characters,[A-Z] for non-upper case character ranges
\{n,m\} means find n to m previous RE characters
\{n,\} indicates more than n previous RE characters
egrep Summary:
+ Indicates repetition of one or more previous RE characters
Example:egrep 'go+d' regular_express.txt
indicates search for (god)(good)(goood)... string, o+ stands for [more than one o]
? Indicates a repeat of zero or one previous RE character
Example:egrep 'go? d' regular_express.txt
The search for (gd)(god) string, o? stands for [blank or 1 o]
Note: 'go+d' and 'go' under egrep? the result set of d'is equal to'go*d'under grep
| Indicates finding several strings by or (or)
Example:egrep 'gd| good| dog' regular_express.txt
indicates search for (gd) or (god) or (god) strings,| representative or
() means find group string
Example:egrep 'g(la| oo)d' regular_express.txt
indicates a search (glad) or (good) string
() + indicates identification of multiple duplicate groups
Example: echo 'AxyzC'| egrep 'A(xyz)+C'
Indicates that the search starts with A and ends with C, with more than one 'xyz' string in between.
sed:
Insert:
1. List the contents of/etc/passwd and print the line numbers. At the same time, delete lines 2-5 and display them.
The code is as follows:
# nl /etc/passwd | sed '2,5d'
Note: sed is short for sed -e, followed by single quotation marks
Ibid. Delete line 2
The code is as follows:
# nl /etc/passwd | sed '2d'
Same as above Delete third line to last line
The code is as follows:
# nl /etc/passwd | sed '3,$d'
2. Add a test line after the second line
The code is as follows:
# nl /etc/passwd | sed '2a test'
Add a test line before the second line
The code is as follows:
# nl /etc/passwd | sed '2i test'
Add two lines of test after the second line
The code is as follows:
# nl /etc/passwd | sed '2a test \
> test'
Replace line:
3. Replace lines 2-5 with No 2-5 number
The code is as follows:
# nl /etc/passwd | sed '2,5c No 2-5 number'
4 List lines 5-7 in/etc/passwd
The code is as follows:
# nl /etc/passwd |sed -n '5,7p'
Replacement string:
sed 's/substituted string/new string/g'
1. Get the line of native IP
The code is as follows:
# /sbin/ifconfig eth0 |grep 'inet addr'
Delete the first part of IP
The code is as follows:
# /sbin/ifconfig eth0 |grep 'inet addr'| sed 's/^.* addr://g'
Delete the part after IP
The code is as follows:
# /sbin/ifconfig eth0 |grep 'inet addr'| sed 's/^.* addr://g'| sed 's/Bcast:.*$// g'
-------------------
192.168.100.74
-------------------
2. Use grep to extract the keyword MAN from the row
The code is as follows:
# cat /etc/man.config |grep 'MAN'
Delete comment line
The code is as follows:
# cat /etc/man.config |grep 'MAN'| sed 's/^#.*$// g'
Delete blank rows
The code is as follows:
# cat /etc/man.config |grep 'MAN'| sed 's/^#.*$// g'| sed '/^$/d'
3. Use sed to convert each line in regular_express.txt to. Change it!
Note: The-i parameter modifies text directly, not output directly
The code is as follows:
# sed -i 's/.*\.$/\!/ g' regular_express.txt
4.#This is a test
Note: $stands for the last line and a stands for adding after the line
The code is as follows:
# sed -i '$a #This is a test' regular_express.txt
Change the selinux profile enforcing to disabled
The code is as follows:
# sed -i '6,6c SELINUX=disabled' /etc/selinux/config
Extended normal notation:
The code is as follows:
# grep -v '^$' regular_express.txt |grep -v '^#'
Extended Writing:
The code is as follows:
# egrep -v '^$'|'^#' regular_express.txt
1. + indicates repetition of one or more previous RE characters
egrep -n 'go+d' regular_express.txt
Common spelling: grep -n 'goo*d' regular_express.txt
2. ? Indicates repetition of zero or one previous RE character
For example: egrep -n 'go? d' regular_express.txt
3. |Indicates to find several strings using or
For example: egrep -n 'gd| good' regular_express.txt
4. () means find group string
egrep -n 'g(la)| oo)d' regular_express.txt
which is to search for the two strings glad or good.
5. ()+ Multiple repeat group discrimination
For example: echo 'AxyzC'| egrep 'A(xyz)+C'
That is to say, it is necessary to find the meaning of the string that starts with A and ends with C and has more than one 'xyz' character in the middle.
awk:
1. Use last to extract the first five lines of landing data
The code is as follows:
# last -n 5
Take out the account and login IP, and the account and IP are separated by TAB.
The code is as follows:
# last -n 5 |awk '{print $1 "\t" $3}'
Note:$1 represents the first field separated by a space or TAB, and so on.
$0 represents all fields in the row
The code is as follows:
# last -n 5 |awk '{print $1 "\t lines:" NR "\t columes:" NF}'
Note: NF stands for the total number of fields per line for $0
NR represents the line number of data awk is currently in.
FS stands for target delimiter, default is space
2. In/etc/passwd with: as a segmentation character, we have to look up the third column less than 10 data, and only list the account number and the third column
The code is as follows:
# cat /etc/passwd | awk '{FS=":"} $3
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.