【find】
- 查找当前目录下,所有包含.attn的.py文件
find . -type f -iname “*.py” -exec grep -Hi “.attn” ‘{}’ +
-exec 后面接命令,以+结尾,‘{}’表示前面的每个文件
grep的参数 -H列出文件名,-i忽略大小写,-h表示不输出文件名
- 和cp结合查找相应文件
/home/data 下面有a b c d e五个文件夹,拷贝除了e之外的其他文件夹,拷贝到/bak下面
cp -Rfind /home/data -path /home/data/e -prune -o -print | sed 1d
/bak
find命令其中 -o 表示或者
例如:查找 /home/ 下所有以.txt或.pdf结尾的文件,命令为find /home/ -name “.txt" -o -name ".pdf”
find命令中-prune 和find命令中-print 结合 -o用时,-print匹配的东西打印,-prune匹配的东西不打印,默认是-print
这里有一个讲prune的,资料很少:https://blog.csdn.net/chj_1224365967/article/details/108347093
sed 1d 去掉第一行,因为d是删除的意思
- find 和 exec结合使用
https://www.cnblogs.com/huchong/p/9961625.html
find . -type f -name “*text” -exec ls -l {} ;
find source/ -name “*.txt”| xargs -i mv {} target/
总结:-exec后面接命令,{}表示前面的匹配项,以;结尾
- 参数解释
-name : 即名字
-iname:忽略大小写的名字
- 统计测试结果举例
find 20220208_conformer_bidecoder_exp/test_-1-1_false/ -iname "cer." -exec grep -Hi “Overall” {} ;
- 替换测试参数
find conf/ -name “finetune_u2++_squeezeformer_v*.yaml” -exec sed -i ‘s#max_epoch: 1#max_epoch: 26#’ {} ;
注解:sed -i参数直接对原始文件进行修改,后面的s表示替换,用#进行隔离替换前后的内容
7.find . -type f -size +1024M 查找大于1g的文件
【grep】
实例:find exp/conformer_bidecoder/test_* -name cer.* -exec grep -H “Overall” {} + | sed ‘s#exp/conformer_bidecoder/test_##g’ | sed ‘s#_avg_last__attention_rescoring_18_1_true/cer.0.5.0.3:Overall -># #g’| sed ‘s#=# #g’> temp2
cat temp | xargs -i grep {} temp2
1 grep参数全集
http://www.136.la/shida/show-28986.html
2. 重点参数
-H 在显示符合范本样式的那一列之前,标示该列的文件名称。
-i 忽略字符大小写的差别。
3. 将temp文件按照temp2文件顺序进行打印
cat temp2 | xargs -Ihello grep hello temp
cat temp2 | xargs -i grep {} temp 等价
第一行在I后定义了一个hello变量,用来表示管道前的输出
4. awk 结合字典的用法
https://jingyan.baidu.com/article/656db918820a77a280249cd1.html
5.筛选出非空行
grep -a -v -e ‘^\s*$’
其中\s表示空白字符
\S表示非空白字符
*表示多个
$表示结尾
grep -e表示使用正则
-a 表示 输入文件为二进制
-v表示不符合
- 匹配方括号内的字符,任意连续两个
grep “[一二三四五六七八]{2}”
【awk】
7. awk中获取字符串长度
cat text | awk ‘{if(length($2)==1)print $2}’| head -n 3
8. NF表示切分为了多少列
9. $NF表示最后一列内容
10. awk ‘{if ($2 == “合适”) print $2}’ | wc -l
【sed】
- sed中使用变量
sed ‘s#https://888/#’“$wav_dir”‘/#g’ -i $wavscp_dir/wavscp