banbanbai 发表于 2018-8-28 08:30:20

shell 小技巧之修改后缀及grep

  批量修改文件后缀
  第一种:
#!/bin/bash  
for fn in *.$1
  
do
  
mv $fn ${fn%$1}$2
  
done
  第二种:
find . -name "*.txt" | awk -F'[./]+' '{print $2}' | xargs -i -t mv {}.txt {}.sh  GREP 用户小技巧
  文件如下:
# cat test.txt  
This is a test file
  
a good day and no coludy
  
Sit here and sing a song
  
miss agao isting
  
but money is money !
  
this is : hang
  
only one line .
  -w 匹配整个单词,避免单词字符匹配
# grep -w is test.txt  
This is a test file
  
but money is money !
  
this is : hang
  没有匹配到isting
  -A 匹配之后的N行
# grep -A 3 Sit test.txt  
Sit here and sing a song
  
miss agao isting
  
but money is money !
  
this is : hang
  -B 匹配之前的N行
# grep -B 2 Sit test.txt  
This is a test file
  
a good day and no coludy
  
Sit here and sing a song
  -C 匹配前后N行
# grep -C 2 -i sit test.txt  
This is a test file
  
a good day and no coludy
  
Sit here and sing a song
  
miss agao isting
  
but money is money !
  -c 统计匹配行数:
# grep -c is test.txt  
4
  -o 只显示匹配的字符串:
# grep -o money test.txt  
money
  
money
  -o -b 显示匹配的位置:
# grep -o -b Sit test.txt  
45:Sit
  交集与差集:
# cat 1.txt  
1
  
2
  
3
  
4
  
# cat 2.txt
  
1
  
2
  
3
  
4
  
5
  
6
  交集:
# grep -f 1.txt 2.txt  
1
  
2
  
3
  
4
  差集
# grep -vf 1.txt 2.txt  
5
  
6


页: [1]
查看完整版本: shell 小技巧之修改后缀及grep