linux常用命令-grep、sed、awk

linux常用命令

awk、grep、sed是linux操作文本的三大常用命令,也是必须掌握的linux命令之一。其中,grep功能相对简单,更适合单纯的查找或匹配文本;sed更适合编辑匹配到的文本;awk更适合格式化文本,对文本进行较复杂格式处理。

grep

命令格式

grep [option] pattern file

常用参数

-A<行数 x>:除了显示符合范本样式的那一列之外,并显示该行之后的 x 行内容。
-B<行数 x>:除了显示符合样式的那一行之外,并显示该行之前的 x 行内容。
-C<行数 x>:除了显示符合样式的那一行之外,并显示该行之前后的 x 行内容。
-c:统计匹配的行数
-e :实现多个选项间的逻辑or 关系
-E:扩展的正则表达式
-f 文件名:从文件获取 PATTERN 匹配
-F :相当于fgrep,fgrep不会将搜索字符串解释为正则表达式。可以直接搜索包含特殊字符(如 $、*、[、|、(、) 和 \)的字符串,而无需对它们进行转义。这些字符在 grep 中通常具有特殊含义,但在 fgrep 中它们会被按字面意义解释。
-i --ignore-case #忽略字符大小写的差别。
-n:显示匹配的行号
-o:仅显示匹配到的字符串
-q: 静默模式,不输出任何信息
-s:不显示错误信息。
-v:显示不被 pattern 匹配到的行,相当于[^] 反向匹配
-w :匹配 整个单词

示例

[root@IC_EDA linux_cmds]# grep -A 4 100 testdata.txt 
1. 100  
2. 200  
3. 300  
4. 400  
  
[root@IC_EDA linux_cmds]# grep -B 4 100 testdata.txt 
This line contains a number: 12345.  
Yet another line.  
  
# 数字列表  
1. 100  
[root@IC_EDA linux_cmds]# grep -C 4 100 testdata.txt 
This line contains a number: 12345.  
Yet another line.  
  
# 数字列表  
1. 100  
2. 200  
3. 300  
4. 400  
  
[root@IC_EDA linux_cmds]# grep -c  00 testdata.txt 
7
[root@IC_EDA linux_cmds]# grep -e "07-19" -e "Line" testdata.txt 
Line 1  
Line 2  
Line 3  
[2023-07-19 10:00:00] INFO: This is an info message.  
[2023-07-19 10:05:00] ERROR: Something went wrong.  
[2023-07-19 10:10:00] WARNING: Watch out for this!  
[root@IC_EDA linux_cmds]# echo "100" > pattern.txt
[root@IC_EDA linux_cmds]# grep -f pattern.txt  testdata.txt 
1. 100  
[root@IC_EDA linux_cmds]# grep -i line testdata.txt 
This is a line of text.  
Another line of text.  
This line contains a number: 12345.  
Yet another line.  
Here is a multi-line text block:  
Line 1  
Line 2  
Line 3  
This line contains special characters: @#$%^&*()_+=-[]{};':"\\|,.<>/?  
[root@IC_EDA linux_cmds]# grep -ni line testdata.txt 
4:This is a line of text.  
5:Another line of text.  
6:This line contains a number: 12345.  
7:Yet another line.  
22:Here is a multi-line text block:  
23:Line 1  
24:Line 2  
25:Line 3  
33:This line contains special characters: @#$%^&*()_+=-[]{};':"\\|,.<>/?  
[root@IC_EDA linux_cmds]# grep -oi line testdata.txt 
line
line
line
line
line
Line
Line
Line
line
[root@IC_EDA linux_cmds]# grep -qi line testdata.txt 
[root@IC_EDA linux_cmds]# grep -vi line testdata.txt 
# 这是一个用于测试 grep, sed, awk 的文档  
  
# 示例文本  
  
# 数字列表  
1. 100  
2. 200  
3. 300  
4. 400  
  
# 表格数据  
Name, Age, City  
John, 25, New York  
Jane, 30, San Francisco  
Bob, 35, Los Angeles  
  
# 多行文本块  
  
# 日志样例  
[2023-07-19 10:00:00] INFO: This is an info message.  
[2023-07-19 10:05:00] ERROR: Something went wrong.  
[2023-07-19 10:10:00] WARNING: Watch out for this!  

sed

命令格式

sed [-hnV][-e<script>][-f<script文件>][文本文件]

常用参数

-e<script>或–expression=<script> 以选项中指定的 script 来处理输入的文本文件,这个-e可以省略,直接写表达式。
-f<script文件>或–file=<script文件>以选项中指定的 script 文件来处理输入的文本文件。
a:新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
c:取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
d:删除,因为是删除啊,所以 d 后面通常不接其他内容;
i:插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);同时更新文件内容
s:取代,通常这个 s 的动作可以搭配正规表示法,例如 1,$s/old/new/g .

示例

[root@IC_EDA linux_cmds]# cat sedtest.txt 
Name, Age, City  
John, 25, New York  
Jane, 30, San Francisco  
Bob, 35, Los Angeles
[root@IC_EDA linux_cmds]# sed -e 1a\nsedadd sedtest.txt 
Name, Age, City  
nsedadd
John, 25, New York  
Jane, 30, San Francisco  
Bob, 35, Los Angeles
[root@IC_EDA linux_cmds]# sed '/York/d' sedtest.txt 
Name, Age, City  
Jane, 30, San Francisco  
Bob, 35, Los Angeles
[root@IC_EDA linux_cmds]# sed '/York/c\york' sedtest.txt 
Name, Age, City  
york
Jane, 30, San Francisco  
Bob, 35, Los Angeles
[root@IC_EDA linux_cmds]# sed 's/York/york/g' sedtest.txt 
Name, Age, City  
John, 25, New york  
Jane, 30, San Francisco  
Bob, 35, Los Angeles
[root@IC_EDA linux_cmds]# sed 's/^/===/g' sedtest.txt 
===Name, Age, City  
===John, 25, New York  
===Jane, 30, San Francisco  
===Bob, 35, Los Angeles
[root@IC_EDA linux_cmds]# sed 's/$/===/g' sedtest.txt 
Name, Age, City  ===
John, 25, New York  ===
Jane, 30, San Francisco  ===
Bob, 35, Los Angeles===
[root@IC_EDA linux_cmds]# sed -i 's/$/===/g' sedtest.txt 
[root@IC_EDA linux_cmds]# cat sedtest.txt 
Name, Age, City  ===
John, 25, New York  ===
Jane, 30, San Francisco  ===
Bob, 35, Los Angeles===

awk

命令格式

awk [选项参数] ‘script’ var=value file(s)

awk [选项参数] -f scriptfile var=value file(s)

常用参数

-F fs or --field-separator fs 指定输入文件折分隔符,fs是一个字符串或者是一个正则表达式,如-F:。
-v var=value or --asign var=value 赋值一个用户定义变量。
-f scripfile or --file scriptfile 从脚本文件中读取awk命令。

内置功能

内置函数

toupper():字符转为大写。
tolower():字符转为小写。
length():返回字符串长度。
substr():返回子字符串。
sin():正弦。
cos():余弦。
sqrt():平方根。
rand():随机数。

内置变量

FILENAME:当前文件名
FS:字段分隔符,默认是空格和制表符。
RS:行分隔符,用于分割每一行,默认是换行符。
OFS:输出字段的分隔符,用于打印时分隔字段,默认为空格。
ORS:输出记录的分隔符,用于打印时分隔记录,默认为换行符。
OFMT:数字输出的格式,默认为%.6g。

示例

[root@IC_EDA linux_cmds]# cat awktest.txt 
Name, Age, City  ===
John, 25, New York  ===
Jane, 30, San Francisco  ===
Bob, 35, Los Angeles===
[root@IC_EDA linux_cmds]# awk '{print $3}' awktest.txt 
City
New
San
Los
[root@IC_EDA linux_cmds]# awk -F ',' '{print $3}' awktest.txt 
 City  ===
 New York  ===
 San Francisco  ===
 Los Angeles===
[root@IC_EDA linux_cmds]# awk -F ',' '{print $2}' awktest.txt 
 Age
 25
 30
 35
[root@IC_EDA linux_cmds]# awk -F ',' '{print $NF}' awktest.txt 
 City  ===
 New York  ===
 San Francisco  ===
 Los Angeles===
[root@IC_EDA linux_cmds]# awk -F ',' '{print toupper($NF)}' awktest.txt 
 CITY  ===
 NEW YORK  ===
 SAN FRANCISCO  ===
 LOS ANGELES===

注意点

sed的匹配模式注意点

在字段中包括 ’ 时,应使用sed -i “s/old/ne’w/g” sedtest.txt,而不能直接使用 sed -i ‘s/old/ne’w/g’ sedtest.txt

正则表达式

^ 表示一行的开头。如:/^#/ 以#开头的匹配。
$ 表示一行的结尾。如:/}$/ 以}结尾的匹配。
< 表示词首。 如:`\ 表示以 abc 为首的詞。
> 表示词尾。 如:abc> 表示以 abc 結尾的詞。
. 表示任何单个字符。

  • 表示某个字符出现了0次或多次。
    [ ] 字符集合。 如:[abc] 表示匹配a或b或c,还有 [a-zA-Z] 表示匹配所有的26个字符。如果其中有^表示反,如 [^a] 表示非a的字符

grep测试文件(testdata.txt)

# 这是一个用于测试 grep的文档  
  
# 示例文本  
This is a line of text.  
Another line of text.  
This line contains a number: 12345.  
Yet another line.  
  
# 数字列表  
1. 100  
2. 200  
3. 300  
4. 400  
  
# 表格数据  
Name, Age, City  
John, 25, New York  
Jane, 30, San Francisco  
Bob, 35, Los Angeles  
  
# 多行文本块  
Here is a multi-line text block:  
Line 1  
Line 2  
Line 3  
  
# 日志样例  
[2023-07-19 10:00:00] INFO: This is an info message.  
[2023-07-19 10:05:00] ERROR: Something went wrong.  
[2023-07-19 10:10:00] WARNING: Watch out for this!  

sed测试文件(seddata.txt)

Name, Age, City  
John, 25, New York  
Jane, 30, San Francisco  
Bob, 35, Los Angeles  

参考链接

https://zhuanlan.zhihu.com/p/110983126
https://www.linuxcool.com/

  • 17
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值