四剑客和正则表达式常见故障及困惑集合

本文深入探讨了Linux环境下find与grep命令的高级用法,包括find命令的-maxdepth参数正确使用方法,避免无效参数输入,以及grep命令的正则表达式应用技巧,如特殊字符匹配和排除指定字符。同时,通过实例演示了如何处理相同文件名在不同目录下的搜索结果,以及如何避免文件覆盖。此外,还对比了grep的排除选项与正则表达式的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#一. find命令:
####warning警告

-maxdepth 这个参数要放在其他参数之前。

[root@oldboyedu59 ~]# find / -type d -maxdepth 1
find: warning: you have specified the -maxdepth option after a non-option argument -type, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it).  Please specify options before other arguments.

/
/boot
/dev
/proc
/run
/sys
/etc
/root
/var
/tmp
/usr
/home
/media
/mnt
/opt
/srv
/old
/oldboy
/lidao
/oldwang
/newwang
/data
/tmp01
[root@oldboyedu59 ~]# 
[root@oldboyedu59 ~]# find /  -maxdepth 1 -type d 
/
/boot
/dev
/proc
/run
/sys
/etc
/root
/var
/tmp
/usr
/home
/media
/mnt
/opt
/srv
/old
/oldboy
/lidao
/oldwang
/newwang
/data
/tmp01

#参数书写错误

Expected a positive decimal integer argument to -maxdepth, but got ‘-type’
发现了一个-maxdepth这个参数后面应该加上数字,但是却找到了-type

[root@oldboyedu59 ~]# find /etc/ -maxdepth   -type f  -iname "*.conf"
find: Expected a positive decimal integer argument to -maxdepth, but got ‘-type’

#参数书写错误

Arguments to -type should contain only one letter
-type的参数 应该只是一个字母

[root@oldboyedu59 ~]# find /etc/ -maxdepth 1  -type   -iname "*.conf"

find: Arguments to -type should contain only one letter

#Invalid argument 无效的参数

不支持小数,使用整数

[root@oldboy001 ~]# find /tmp/ -size -0.5k
find: Invalid argument `-0.5k' to -size

##will not overwrite just-created

不会覆盖刚刚创建的文件
不同目录下可能有相同的文件

模拟错误:
[root@oldboyedu59 ~]# touch   /oldboy/oldboy.txt  /oldboy/lidao/oldboy.txt 
[root@oldboyedu59 ~]# find  /oldboy/ -name 'oldboy.txt' 
/oldboy/lidao/oldboy.txt
/oldboy/oldboy.txt
[root@oldboyedu59 ~]# find  /oldboy/ -name 'oldboy.txt' |xargs cp -t /tmp 
cp: will not overwrite just-created ‘/tmp/oldboy.txt’ with ‘/oldboy/oldboy.txt’

[root@oldboyedu59 ~]# find /etc/ -type f -name "*.conf"  |xargs cp -t /tmp/
cp: will not overwrite just-created ‘/tmp/system.conf’ with ‘/etc/dbus-1/system.conf’
cp: will not overwrite just-created ‘/tmp/wpa_supplicant.conf’ with ‘/etc/wpa_supplicant/wpa_supplicant.conf’

二. 正则表达式部分

1.正则-中括号里面的 ,逗号

grep  '[a-zA-Z]' oldboy.txt
grep  '[a-z,A-Z]' oldboy.txt

2.正则表达式中括号 [ ] 里面写入什么(除了第1位的^)就匹配什么

[root@oldboyedu59 /oldboy]# cat  oldboy.txt
I am oldboy teacher!
I teach linux.

I like badminton ball ,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com 
our size is http://blog.oldboyedu.com 
my qq is 49000448

not 4900000448.
my god ,i am not oldbey,but OLDBOY! 
\\\\\...???$$$$^^&^
[root@oldboyedu59 /oldboy]# grep '[mn\.]' oldboy.txt
I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com 
our size is http://blog.oldboyedu.com 
my qq is 49000448
not 4900000448.
my god ,i am not oldbey,but OLDBOY! 
\\\\\...???$$$$^^&^
grep '^[mon][mo.]$' /tmp/oldboy

每一行只有2个字符
  1. [^m^n^o] 神马意思?

grep ‘[mn^o]’ oldboy.txt
[^]表示取反 但是^必须在中括号里面的第1个位置
‘[mn^o]’ 表示排除m或n或o或^(尖号)

[root@oldboyedu59 /oldboy]# cat oldboy.txt
I am oldboy teacher!
I teach linux.

I like badminton ball ,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com 
our size is http://blog.oldboyedu.com 
my qq is 49000448

not 4900000448.
my god ,i am not oldbey,but OLDBOY! 
\\\\\...???$$$$^^&^
[root@oldboyedu59 /oldboy]# grep '[^m^n^o]' oldboy.txt 
I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com 
our size is http://blog.oldboyedu.com 
my qq is 49000448
not 4900000448.
my god ,i am not oldbey,but OLDBOY! 
\\\\\...???$$$$^^&^

5.[^abc] vs grep -v

名称含义
[^abc]字符 不要a或b或c
grep -v行 排除某一行
[root@oldboyedu59 /oldboy]# grep -v 'oldboy' oldboy.txt
I teach linux.

I like badminton ball ,billiard ball and chinese chess!
my qq is 49000448

not 4900000448.
my god ,i am not oldbey,but OLDBOY! 
\\\\\...???$$$$^^&^
[root@oldboyedu59 /oldboy]# grep  '[^a-Z]' oldboy.txt
I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com 
our size is http://blog.oldboyedu.com 
my qq is 49000448
not 4900000448.
my god ,i am not oldbey,but OLDBOY! 
\\\\\...???$$$$^^&^

6.[abc] vs a|b|c

名称含义
[abc]表示字符的或者 匹配a或b或c
a丨b丨c表示字母也可以表示单词oldboy丨oldgirl

7.grep ‘[^$]’ oldboy.txt

grep ‘[^$]’ oldboy.txt

三.sed命令错误集合

invalid usage of line address 0

无效的行号

[root@oldboyedu59 /oldboy]# sed -n '0,5p' lidao.txt
sed: -e expression #1, char 4: invalid usage of line address 0

Unmatched

不成对的 ( (小括号)

[root@oldboyedu59 ~]# stat /etc/hosts |sed -n 4p |sed -r 's#^.*(0##g'
sed: -e expression #1, char 10: Unmatched ( or \(
stat /etc/hosts |sed -n 4p |sed -r 's#(^.*0)(.*)(/-.*$)#\2#g'   

四.三剑客awk错误集合

<think>好的,用户想了解Linux剑客,也就是sed、awk、grepcut这个工具。首先,我需要确认这个工具的正确性常用性,因为有时候可能会有不同的组合,比如有的资料里可能提到find而不是cut,但根据用户提供的引用,特别是引用34,明确提到了grep、awk、cut、sed,所以应该是这个。 接下来,我需要分别介绍每个工具的基本功能常见用法。用户提供的引用中有一些示例,比如引用1中的awk命令统计连接数,引用2中的grep排除特定行,引用34提到了grep的参数用途。需要确保每个工具的介绍都准确,并且结合引用中的例子来说明,这样更有说服力。 然后,要比较这个工具的差异,帮助用户理解它们各自适用的场景。比如,grep适合文本搜索,sed适合文本替换,awk适合处理结构化数据,cut适合按列提取数据。同时,用户可能想知道为什么这个被称为“剑客”,可能需要简要说明它们在文本处理中的高效强大。 另外,用户的问题可能隐含了想知道如何组合使用这些工具,比如管道操作,比如用grep过滤后用awk处理。引用1中的例子就使用了netstatawk结合,可能可以提到这种组合用法。 需要检查是否有遗漏的重要功能,比如sed的行编辑、awk的编程能力、cut的分隔符选项等。确保每个工具的介绍都覆盖主要用途,并引用提供的参考资料中的例子来支持说明。 最后,生成相关问题时要围绕用户可能进一步想了解的内容,比如每个工具的进阶用法、组合使用案例、常见错误处理等。确保问题能引导用户深入学习应用这些工具。</think>### Linux剑客详解 Linux剑客指**grep、sed、awk、cut**个命令行工具,它们在文本处理数据分析中具有高效、灵活的特点,常用于日志分析、数据提取、批量编辑等场景[^3][^4]。 --- #### 1. **grep**:文本搜索工具 - **功能**:基于正则表达式匹配文本行 - **常用参数**: - `-v` 反向匹配(排除包含关键词的行) 例如:`grep -v "c" a.txt` 显示不包含字母c的行[^2] - `-i` 忽略大小写 - `-n` 显示匹配行的行号 - **典型场景**:过滤日志中的错误信息 ```bash grep "ERROR" server.log ``` --- #### 2. **sed**:流编辑器 - **功能**:按行处理文本(替换、删除、插入) - **核心语法**:`sed 's/原内容/替换内容/[修饰符]'` - **示例**: - 替换文件中的字符串: ```bash sed 's/old/new/g' file.txt ``` - 删除包含特定模式的行: ```bash sed '/pattern/d' file.txt ``` --- #### 3. **awk**:文本分析工具 - **功能**:处理结构化文本(如按列提取数据、计算统计值) - **语法结构**:`awk '模式 {动作}' 文件` - **典型应用**: - 统计TCP连接状态: ```bash netstat -an | awk '/tcp/ {s[$NF]++} END {for(a in s) print a, s[a]}' [^1] ``` - 提取文件第3列: ```bash awk '{print $3}' data.csv ``` --- #### 4. **cut**:按列截取工具 - **功能**:按分隔符或字符位置提取列 - **常用参数**: - `-d` 指定分隔符(默认制表符) - `-f` 指定列号 - **示例**:提取以冒号分隔的第1列 ```bash cut -d':' -f1 /etc/passwd ``` --- ### 剑客对比与协作 | 工具 | 核心能力 | 典型协作场景 | |-------|--------------------|-----------------------------| | grep | 快速过滤行 | `grep "ERROR" log | awk '{...}'` | | sed | 行级编辑 | `sed 's/old/new/' file | cut -f2` | | awk | 复杂逻辑与列处理 | `awk '{sum+=$1} END{print sum}'` | | cut | 简单列提取 | `cut -d',' -f3 data.csv` | ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值