Regular expressions in the shell
Vbird said that learning Linux, mastering the shell and the regular is equivalent to open the two-channel, after the ability of the growth will be rapid.
The Shell's basic learning has been summed up in a blog post: http://www.cnblogs.com/jyzhao/p/4485553.html
This article summarizes the regular expressions in the shell and the commonly used character processing commands, which lays the foundation ^_^ for opening the two veins of the governor.
- An example of a basic regular expression
Character Intercept command
- Cut command
- awk command
- SED command
Character processing commands
1. Examples of basic regular expressions
*,.,\,^word,word$,[list],[^list],[n1-n2],\{n\},\{n,m\}
Example 1: Easy to determine date format
For example: 2015-05-11, simply judge the format of the numbers.
^[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}$
Example 2: Easy to determine IP address format
For example: 192.168.1.100, just simple to judge the format of numbers.
[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}
Example 3: Find a line that starts with a letter in a 1.txt text file and does not start with a letter.
Lines that start with a letter:
grep --color=auto "^[a-z,A-Z]" 1.txt
Lines that do not start with a letter:
grep --color=auto "^[^a-z,A-Z]" 1.txt
2. Character intercept command cut command
Purpose: Specific interception of certain columns for the text of the delimiter specification is simple and easy to use.
Example 4: Cut intercepts the user name and corresponding uid,gid, filtering out rows containing '/sbin '.
grep -v "/sbin" /etc/passwd | cut -f 1,3,4 -d ":"
Examples of interception results:
root:0:0oracle:500:500grid:501:500
awk command
Purpose: For the truncation of irregular text, the feature is powerful, syntax is more complex than the cut command.
awk ‘条件类型1{动作1}条件类型2{动作2} ...‘ filename
awk built-in variables:
NF 每一行($0)拥有的字段总数NR 目前awk所处理的是“第几行”数据FS 目前的分隔字符,默认是空格键
The logical operator of awk:
>, <, >=, <=, ==, !=
Example 5: Querying the PID of the Pmon process.
ps -ef | grep pmon | grep -v grep | awk ‘{print $2}‘
Example 6: Print out the uid<3 users and their UID in/etc/passwd.
cat /etc/passwd | awk ‘BEGIN{FS=":"} $3 < 3 {print $1 "\t" $3}‘
NOTE: Notice that begin is used here, without the first line of the begin display will be incorrect.
Example 7: Capturing the disk space utilization of the system root directory
df -h | awk ‘{print $5}‘|cut -f 1 -d "%"
SED command
SED can replace, delete, add, and select specific rows of data.
-n 只有经过sed处理的才显示(默认显示全部)-e 直接在命令行模式上进行sed的动作编辑-f 将sed的动作写入一个文件,然后-f filename执行filename中的sed命令-r 支持扩展型正则表达式语法(默认是基础正则表达式语法)-i 直接修改读取的文件内容,而不是默认的屏幕输出结果
Example 8: Display line numbers and print out/etc/hosts, deleting the first two lines of a file.
nl /etc/hosts | sed ‘1,2d‘
Example 9: Replace all jy-db in the/etc/hosts file with alfred-db display
sed -e ‘s/JY-DB/Alfred-DB/g‘ /etc/hosts
Note: It is also possible not to write -e parameters here, if two or more SED commands require each of the previous write -e parameters.
Example: Modify the/etc/hosts file directly, insert a new line under line 2nd of the file "192.168.1.100 jy-db"
sed -i ‘2a 192.168.1.100 JY-DB‘ /etc/hosts
3. Character Processing Command Sort command
Purpose: The results are sorted and displayed.
du -sk * | sort -rn
sort -t ":" -k "3,3" /etc/passwd
sort -n -t ":" -k "3,3" /etc/passwd
WC command
Purpose: Count the number of rows, words, and characters of the result.
Statistics Linewc -l
Example: Count the number of user processes in Oracle
ps -ef | grep LOCAL=NO | grep -v grep | wc -l
Statistical wordswc -w
Example: Count the number of words in a/etc/issue file
wc -w /etc/issue
Statistical characterswc -m
Example: Count the number of characters in a/etc/issue file
wc -m /etc/issue
Regular expressions in the shell