码迷,mamicode.com
首页 > 系统相关 > 详细

shell 小技巧之修改后缀及grep

时间:2016-04-22 16:47:48      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:shell

批量修改文件后缀

第一种:

#!/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

本文出自 “运维菜鸟” 博客,请务必保留此出处http://ckl893.blog.51cto.com/8827818/1766655

shell 小技巧之修改后缀及grep

标签:shell

原文地址:http://ckl893.blog.51cto.com/8827818/1766655

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!