Scenario:
数张报表,某些报表包含“REPORT DATE : 01/15/2014”字样,某些报表包含“REPORT DATE : 01/09/2014”字样,需要删除掉01/05/2014的报表。
$ ls -lrt *txt
-rw-r--r-- 1 udwhapp1 udwhapp 1142 Jan 16 12:03 ORP643ICL1052D2014-01-16-11-58-23.txt
-rw-r--r-- 1 udwhapp1 udwhapp 921 Jan 16 12:04 ORP643ICL1054D2014-01-16-11-58-24.txt
-rw-r--r-- 1 udwhapp1 udwhapp 831 Jan 16 12:12 ORP643ICL1053D2014-01-16-11-58-24.txt
-rw-r--r-- 1 udwhapp1 udwhapp 921 Jan 16 12:12 ORP643ICL1057D2014-01-16-11-58-28.txt
-rw-r--r-- 1 udwhapp1 udwhapp 837 Jan 16 12:14 ORP643ICL1055D2014-01-16-11-58-25.txt
Process:
1. 找到报表
$ grep ' REPORT DATE : 01/15/2014' *txt
ORP643ICL1053D2014-01-16-11-58-24.txt: REPORT DATE : 01/15/2014
ORP643ICL1055D2014-01-16-11-58-25.txt: REPORT DATE : 01/15/2014
ORP643ICL1057D2014-01-16-11-58-28.txt: REPORT DATE : 01/15/2014
2. 以上结果为“文件名 + 搜索字样”的格式,需要提取出文件名
$ grep ' REPORT DATE : 01/15/2014' *txt | cut -c1-37
ORP643ICL1053D2014-01-16-11-58-24.txt
ORP643ICL1055D2014-01-16-11-58-25.txt
ORP643ICL1057D2014-01-16-11-58-28.txt
3. 删除文件
直接pipe给rm,报错
$ grep ' REPORT DATE : 01/15/2014' *txt | cut -c1-37 | rm
Usage: rm [-firRe] [--] File ...
需加xargs
$ grep ' REPORT DATE : 01/15/2014' *txt | cut -c1-37 | xargs rm
Result:
成功删除
$ ls -lrt *txt
-rw-r--r-- 1 udwhapp1 udwhapp 1142 Jan 16 12:03 ORP643ICL1052D2014-01-16-11-58-23.txt
-rw-r--r-- 1 udwhapp1 udwhapp 921 Jan 16 12:04 ORP643ICL1054D2014-01-16-11-58-24.txt
Experience:
* cut -c, 截取substring, 由-cx-y来指定substring的位置,如-c1-37则截取字符串的第1到第37个字符
* 一个List不能直接pipe到rm命令,需要在rm前加xargs.
xargs的作用:Constructs parameter lists and runs commands.
用xargs, cut来删除特定文件
原创
©著作权归作者所有:来自51CTO博客作者tamas_hun的原创作品,请联系作者获取转载授权,否则将追究法律责任
下一篇:lieshi
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
cut、tee、split、xargs、bc命令
cut -d "分隔字符" [-cf] fields
数据 bash 最大匹配