The Sword of the Three Musketeers of Linux.

Source: Internet
Author: User

The swordsman originated from the legendary Chinese martial arts novels of the Tang Dynasty, and the weapons they usedstatus is paramount, has always been the king of weapons, in line with thein the status of ancient Chinese society. Give you a detailed summary todaylinuxthe blade weapon under the system:awk,sed,grep

三剑客介绍(grep、sed、awk)

Old Three: grep

is a powerful text search tool that can use regular expressions to search for text and print matching lines

# grep Common Options-C #只输出匹配行的计数-i #不区分大小写-H #查询多文件时不显示文件名-L #查询多文件时只输出包含匹配字符的文件名-N #显示匹配行及行号-S #不显示不存在或无匹配文本的错误信息-v #显示不包含 All lines matching text-e #支持扩展的正则表达式-P #调用perl语法正则


Dick: SED

The processing of the text is very strong, to increase, delete, change, check everything can do

# sed Common options-r #在脚本中使用扩展正则表达式-N #不打印所有行到标准输出-i #直接修改文件
Boss: Awk (lead brother)

can be understood as a programming language, You can customize variables, conditional statements, loops, arrays, regular, function

# awk commonly used variable $n #当前记录的第n个字段, between fields separated by FS #完整的输入记录 FNR #同NR, but relative to the current file FS #字段分隔符 (default is any space) NF #当前记录中的字段数 NR #当前记录数 OFS #输出字段分隔符 ( The default value is a space) ORS #输出记录分隔符 (the default is a newline character) # awk common String function sub #匹配记录中最大, the most left-most substring of the regular expression, and replace the string with the replacement string Gsub #整个文档中进行匹配 index #返回子字符串 The first time the matched position is substr #返回从位置1开始的子字符串, if the specified length exceeds the actual length, the entire string split #可按给定的分隔符把字符串分割为一个数组 is returned, and the default is split by the current FS value of length #返回记录的字符数 ToUpper #可用于字符串大小间的转换, this feature is only valid in Gawk ToLower #可用于字符串大小间的转换, which is only valid in Gawk # awk common Functions Srand (x) #x是rand () function seed int (x) #取整 rand ( ) #产生一个大于等于0而小于1的随机数

Case use (there are many ways, today we only talk about the use of the Three Musketeers)

1. Quickly remove the native IP?

# grep ifconfig eth0|grep-po ' (? <=DR:) \s+ ' # sed ifconfig eth0|sed-rn ' s#.*r: (. *) B.*#\1#GP ' # awk ifconfig eth0|a Wk-f ' [:]+ ' Nr==2{print $4} '

2. Replace the original data format with the new data format (grep cannot implement this function by itself)

raw data:
17/apr/2015:09:29:24 +0800
17/apr/2015:09:30:26 +0800
17/apr/2015:09:31:56 +0800
18/apr/ 2015:09:34:12 +0800
18/apr/2015:09:35:23 +0800
19/apr/2015:09:23:34 +0800
19/apr/2015:09:22:21 +0800
20/apr/2015:09:45:22 +0800
Expected Result:
2015-04-17 09:29:24+0800
2015-04-17 09:30:26+0800
2015-04-17 09:31:56 +0800
2015-04-18 09:34:12+0800
2015-04-18 09:35:23+0800
2015-04-19 09:23:34+0800
2015-04-19 09:22:21+ 0800
2015-04-20 09:45:22+0800

# SED

sed-RN' s#apr#04#g;s#^ (. *)/(. *)/(...):(. *) (. *) #\3-\2-\1 \4\5#p; 'Test.log sed-RN' s#^ (. { 2})/(. { 3})/(. { 4}):(. {8}) (. { 4}) #\3-04-\1 \4\5#p; 'Test.Log

# awk

awk-F' [/:]+ ' ' {sub (/apr/, "" ");p rintf ("%s-%s-%s%s:%s:%s%s\n ", $3,$2,$1,$4,$5,$6,$7)} 'Test.Log awk' {sub (/apr/, ""), Split ($0,array, "[/:]");p rintf ("%s-%s-%s%s:%s:%s%s\n", Array[3],array[2],array[1],array[4] , array[5],array[6],array[7])} 'Test.Log

Words. The Sword of the Three Musketeers of Linux the sheath

Related Article

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.