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

一. 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! 
\\\\\...???$$$$^^&^

3.

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

每一行只有2个字符

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

grep '[^m^n^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错误集合

awk

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

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

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

[root@web01 ~]# echo {1..10..5}|awk '{print $(NF-3)}'
awk: cmd. line:1: (FILENAME=- FNR=1) fatal: attempt to access field -1

猜你喜欢

转载自blog.51cto.com/7681914/2555699