美文网首页
Linux 常用命令

Linux 常用命令

作者: 一颗北上广的心 | 来源:发表于2019-08-25 13:59 被阅读0次

# find 指定路径下按文件名查找 
MacBook-Pro:test root# find ~/test/ -name 'test.*'
/var/root/test//test.java
/var/root/test//test.txt

#-iname 不区分文件名大小写
MacBook-Pro:test root# find ~/test/ -iname 'test.*'
/var/root/test//Test.json
/var/root/test//test.java
/var/root/test//test.txt
#grep 查找文件内容
MacBook-Pro:test root# grep 'haha' test.*
test.java:haha
test.txt:haha 哈哈哈 

#-o 过滤指定字符
MacBook-Pro:test root# grep -o '哈哈'  test.*
test.txt:哈哈

#-v 过滤掉指定字符
MacBook-Pro:test root# grep -v '哈哈'  test.*
test.java:haha
test.java:你好:wq
test.java:
test.txt:hello 
test.txt:nihao
test.txt:大家好
#sed 替换

#^Str 替换Str开头的  
MacBook-Pro:test root# cat replace.txt
Str x = 1.
Str y = 2.
Str z = 2020.

MacBook-Pro:test root# sed 's/^Str/int/' replace.txt
int x = 1.
int y = 2.
int z = 2020.


# .$ 替换.结尾的
MacBook-Pro:test root# cat replace.txt
Str x = 1.
Str y = 2.
Str z = 2020.

MacBook-Pro:test root# sed 's/.$/;/' replace.txt
Str x = 1;
Str y = 2;
Str z = 2020;


# /g 全行所有匹配项都替换
MacBook-Pro:test root# cat replace.txt
Str x = 1.
Str y = 2.
Str z = 2020.
#默认值替换第一个匹配到的
MacBook-Pro:test root# sed 's/20/10/' replace.txt
Str x = 1.
Str y = 2.
Str z = 1020.

MacBook-Pro:test root# sed 's/20/10/g' replace.txt
Str x = 1.
Str y = 2.
Str z = 1010.

# /d 删除 
MacBook-Pro:test root# cat replace.txt
Str x = 1.
Str y = 2.
Str z = 2020.


结束行
MacBook-Pro:test root# sed '/^ *$/d' replace.txt
Str x = 1.
Str y = 2.
Str z = 2020.
结束行

#删除包含指定字符的行
MacBook-Pro:test root# cat replace.txt
Str x = 1.
Str y = 2.
Str z = 2020.
结束行
MacBook-Pro:test root# sed '/x/d' replace.txt
Str y = 2.
Str z = 2020.
结束行

# -i 将替换后的文件写入
MacBook-Pro:test root# sed -i '' '/x/d' replace.txt
MacBook-Pro:test root# cat replace.txt
Str y = 2.
Str z = 2020.


结束行
#awk  默认已空格分隔文件, 每一列是一个分片,$0表示所有,$1表示第一列 $2表示第二列,以此类推
MacBook-Pro:test root# cat user.txt 
name,age,gender
xiaoming,20,male
dahu,34,male
xiaohong,18,female
#-F 可以指定分隔符
MacBook-Pro:test root# awk -F ',' '{print $1,$2}' user.txt 
name age
xiaoming 20
dahu 34
xiaohong 18
#条件筛选
MacBook-Pro:test root# awk -F ',' '$2<30{print $0}' user.txt 
xiaoming,20,male
xiaohong,18,female
#输出带表头 NR==1
MacBook-Pro:test root# awk -F ',' '($2<30 || NR==1){print $0}' user.txt 
name,age,gender
xiaoming,20,male
xiaohong,18,female

相关文章

网友评论

      本文标题:Linux 常用命令

      本文链接:https://www.haomeiwen.com/subject/qjmhectx.html