运维开发网

【Linux各种坑】四剑客和正则表达式常见故障及困惑集合

运维开发网 https://www.qedev.com 2020-11-29 12:06 出处:51CTO 作者:老男孩培训
一.find命令:warning警告-maxdepth这个参数要放在其他参数之前。[[email protected]~]#find/-typed-maxdepth1find:warning:youhavespecifiedthe-maxdepthoptionafteranon-optionargument-type,butoptionsarenotpositional(-maxdepthaffec

一. find命令:

warning警告

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

[[email protected] ~]# 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
[[email protected] ~]# 
[[email protected] ~]# 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

[[email protected] ~]# 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的参数 应该只是一个字母

[[email protected] ~]# find /etc/ -maxdepth 1  -type   -iname "*.conf"

find: Arguments to -type should contain only one letter

Invalid argument 无效的参数

不支持小数,使用整数

[[email protected] ~]# find /tmp/ -size -0.5k
find: Invalid argument `-0.5k' to -size

will not overwrite just-created

不会覆盖刚刚创建的文件

不同目录下可能有相同的文件

模拟错误:

[[email protected] ~]# touch   /oldboy/oldboy.txt  /oldboy/lidao/oldboy.txt 
[[email protected] ~]# find  /oldboy/ -name 'oldboy.txt' 
/oldboy/lidao/oldboy.txt
/oldboy/oldboy.txt
[[email protected] ~]# find  /oldboy/ -name 'oldboy.txt' |xargs cp -t /tmp 
cp: will not overwrite just-created ‘/tmp/oldboy.txt’ with ‘/oldboy/oldboy.txt’
[[email protected] ~]# 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位的^)就匹配什么

[[email protected] /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! 
\\\\\...???$$$$^^&^
[[email protected] /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! 
\\\\\...???$$$$^^&^

3.

grep '^[mon][mo.]$' /tmp/oldboy

每一行只有2个字符

4. [^m^n^o] 神马意思?

grep '[^m^n^o]' oldboy.txt

[^]表示取反 但是^必须在中括号里面的第1个位置

'[mn^o]' 表示排除m或n或o或^(尖号)

[[email protected] /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! 
\\\\\...???$$$$^^&^
[[email protected] /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 行 排除某一行
[[email protected] /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! 
\\\\\...???$$$$^^&^
[[email protected] /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

无效的行号

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

Unmatched

不成对的 ( (小括号)

[[email protected] ~]# 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错误集合

awk

1. awk: cmd. line:1: fatal: division by zero attempted

[[email protected] ~]# x=10
[[email protected] ~]# y=3
[[email protected] ~]# awk 'BEGIN{print $x/$y}'
awk: cmd. line:1: fatal: division by zero attempted
[[email protected] ~]# #-v 创建或修改awk变量
[[email protected] ~]# awk -vn1=10 -vn2=3  'BEGIN{print n1/n2}'
3.33333
[[email protected] ~]# awk -vn1=$x -vn2=$y  'BEGIN{print n1/n2}'
3.33333
[[email protected] ~]# 

2. awk: cmd. line:1: (FILENAME=- FNR=1) fatal: attempt to access field -1

[[email protected] ~]# echo {1..10..5}|awk '{print $(NF-3)}'

awk: cmd. line:1: (FILENAME=- FNR=1) fatal: attempt to access field -1

扫码领视频副本.gif

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号