一行shell代码搞定问题
一行shell代码搞定问题
一行shell代码搞定问题
1. 查找至少有一行包含字符串mysql的xml文件,并按照出现次数降序排列
C代码
find . -type f -iname "*.xMl"| xargs grep -c mysql | grep -v ":0$" | sort -t : -k 2 -nr
C代码
find . -type f -regex ".+xml" -exec grep -l mysql {} \; | xargs grep -c mysql | sort -t : -k 2 -nr
2. 统计第二列重复出现次数最多的5个字符串
C代码
awk -F " # " '{print $2}' source.txt | sort | uniq -c | sort -nf | tail -n 5 > ~/PwdTop5.txt
Java代码
awk '{print $3}' source.txt | sort | uniq -c | sort -nr | head -5 > ./PwdTop5.txt &
3. 统计邮箱类型出现频率最高的前5个
C代码
awk -F " # " '{print $3}' source.txt | cut -d @ -f 2 | sort | uniq -c | sort -nf | tail -n 5 > EmailTop5.txt
注:
source.txt 中的文本格式为
C代码
AAA # BBB # CCC@foxmail.com
AAB # BBC # CCD@qq.com
AAC # BBD # CCE@163.com
4. 将所有txt文件的第一行输出到first.txt文件中
C代码
find / -name "*.txt" -exec head -n 1 {} \; 1>first.txt 2>/dev/null &
5. 查找当前路径下最大的5个文件
C代码
find . -type f -exec ls -l {} \; | sort -nr -k 5 | head -n 5
6. 统计所有jpg文件的大小
C代码
find / -name "*.jpg" -exec wc -c {} \; | awk '{print $1}' | awk '{a+=$1} END{print a}'
7. 将PATH路径下所有非jpg和JPG文件内容中的aaa部分重命名为bbb部分
C代码
find $PATH -type f -print |grep -v ".*\.\(jpg\|JPG\)" | xargs sed -i "s/aaa/bbb/g"
评论暂时关闭