[Linux various pits] A collection of common faults and perplexities of the four swordsmen and regular expressions

One. find command:

warning warning

-maxdepth This parameter should be placed before other parameters.

[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

Parameter writing error

Expected a positive decimal integer argument to -maxdepth, but got'-type'
found a -maxdepth parameter should be followed by a number, but found -type

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

Parameter writing error

Arguments to -type should contain only one letter
parameter -type be just a letter

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

find: Arguments to -type should contain only one letter

Invalid argument

Do not support decimals, use integers

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

will not overwrite just-created

Will not overwrite the file just created.
There may be the same file in different directories

Simulation error:

[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’

2. Regular expression part

1.Regular-inside brackets, comma

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

2. What is written in the brackets [] in the regular expression (except the ^ in the first position) matches what

[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

Only 2 characters per line

4. [^m^n^o] What does the god horse mean?

grep '[^m^n^o]' oldboy.txt
[^] means inverted but ^ must be in the first position inside the brackets.
'[mn^o]' means to exclude m or n or o or ^ (caret)

[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

name meaning
[^abc] Do not use a or b or c
grep -v Exclude a row
[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

name meaning
[abc] Represents a character or matches a or b or c
a|b|c The letter can also represent the word oldboy|oldgirl

7.grep '[^$]' oldboy.txt

grep '[^$]' oldboy.txt

Three.sed command error collection

invalid usage of line address 0

Invalid line number

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

Unmatched

Unpaired ((parentheses)

[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'   
老师,这个第一个括号里的(^.*)为啥没有贪婪性

Four. Three Musketeers awk error collection

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

Guess you like

Origin blog.51cto.com/7681914/2555699