Linux学习—四剑客awk、sed、find、grep

1. find (找文件)

根据 文件名 和 路径 查找文件 并执行 一些操作

find  路径  -name "文件名"
find /home/python/Desktop/ -name "1.txt"
find /home/python/Desktop/ -name "*.txt"
find /home/python/Desktop/ -name "*.txt" -type f 
(f ==> file, 找到的是 文件 类型)
-------------------------------------------------------------------------------------------------
find /home/python/Desktop/ -name "*.txt" -type d
 (d ==> dir, 找到的是 文件夹 类型)
 -------------------------------------------------------------------------------------------------
find /home/python/Desktop/ -name "*.txt" -type d -ctime -1
(ctime ==> changetime,-1 ==> 1天以内,+30 ==> 超过30天)
-------------------------------------------------------------------------------------------------
find /home/python/Desktop/ -name "*.txt" -type f -ctime -1 |xargs rm -rf {} \;
(xargs ==> 把前面的输出当做输入传给后面花括号, \; ==> 与find固定搭配)
-------------------------------------------------------------------------------------------------
find /home/python/Desktop/ -name "*.txt" -type d -ctime -1 -exec cp -r {} ~/Desktop/test/ \;
find /home/python/Desktop/test -type d -exec chmod -R 755 {} \;
(-exec ==> 执行某些操作)
-------------------------------------------------------------------------------------------------
find /home/python/Desktop/ -name "*.txt" -type f -ctime +30 -size +1k
(-size ==> 文件大小, +1k ==> 大于1k且k为小写,+1M ==> 大于1M且M为大写)

2. grep (找文件内容,行操作)

根据 字符串 查找文件内容 并执行 一些操作

grep -n --color '^hello' ~/Desktop/2.txt
(-n ==> 显示行数, -color ==> 显示颜色,  '^hello' ⇒ 查找的字符串, ^ ==>字符串开头,
$ ==> 字符串结尾,  ~/Desktop/2.txt ==> 查找的文件路径)
-------------------------------------------------------------------------------------------------
grep -v --color '^hello' ~/Desktop/2.txt | grep -v "^$"
(-v ==> 取反,不要以 hello 开头的内容, -v ”^$” ==> 去掉空行)
-------------------------------------------------------------------------------------------------
egrep '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$' 2.txt 
egrep '([0-9]{1,3}\.){3}[0-9]{1,3}$' 2.txt 
('[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$' ==> 正则表达式匹配ip地址 xxx.xxx.xxx.xxx)

3. awk (找文件内容,列操作)

根据文件内容 做分割 添加字符串

awk '{print $2}' 2.txt 
('{print $2}' ==> 打印第2列,2.txt ==> 文件名)
awk '{print $NF}' 2.txt 
('{print $NF}' ==> 打印倒数第1列, NF指的是某行被分为NF列)
awk '{print $(NF-1)}' 2.txt
( '{print $(NF-1)}'  ==> 打印倒数第2列且(NF-1)不能为负数)
-------------------------------------------------------------------------------------------------
awk -F: '{print $1}' 2.txt  | head -5
(-F: ==> 以:分割         | head -5 ==> 取前5行)
-------------------------------------------------------------------------------------------------
awk -F: '{print $1":"$NF}' 2.txt
(":" ==> 添加冒号,双引号代表添加。 “”==>“”,双引号里面可以添加任何东西)
-------------------------------------------------------------------------------------------------
ifconfig | grep 'inet 地址' | grep -v '127.0.0.1' | awk '{print $2}' | awk -F: '{print $2}'| 
awk -F. '{print $1"-"$2"-"$3"-"$NF}'
(匹配ip地址,输出xxx-xxx-xxx-xxx)

4. sed (找文件内容,行操作)

替换字符串

sed 's/h/he/2' 2.txt 
(s ==> 替换, h ==> 原内容, he ==> 新内容,2  ==> 替换第2个 ,2.txt ==> 文件名)
sed 's/h/he/g' 2.txt
(g ==> 替换全部) 
-------------------------------------------------------------------------------------------------
sed 's/h/www.\/\/baidu.com/3' 2.txt 
(\/\/ ==> //不进行转义)
sed 's#h#www.//baidu.com#3' 2.txt
(# ==> 代替/,不用\标明不进行转义)
-------------------------------------------------------------------------------------------------
sed -i 's#h#he#3' 2.txt
(-i ==> 改变文件。没有的话,不改变文件)

猜你喜欢

转载自blog.csdn.net/b806071099/article/details/103957404