我在日志目录中有大量文件,其名称带有时间戳(在本例中带有date +%b[...]
)。
我想写一个符合 POSIX 标准的单行这将:
任何一个
ls -ltc
跳过包含 2 个或更少字段(如 所示awk
)且不包含正则表达式常量的行输出/Jul/
或者
- 跳过输出的第一行
ls -ltc
以及任何不匹配的行/Jul/
。
结果与我的目的相同,因为只有第一行由两个字段组成(如图所示awk
)。
我试过:
> ll -tc | rm $(awk --posix 'NF > 2 && !/Jul/ {print $NF;}')
> ll -tc | rm $(awk --posix 'NF > 2 !/Jul/ {print $NF;}')
两者都有不好的语法,因为 NF 上的条件显然不能与正则表达式匹配条件很好地共存ls | awk '/foo/ && /bar/'
。
有人能指点我如何跳过任意记录和/或同时应用 NF 上的算术条件和正则表达式模式匹配吗?我确实四处寻找,但找不到我正在寻找的记录语法...
答案1
任何一个
awk 'NF<=2 || /Jul/ {next} {print}'
或者
awk 'NR==1 || /Jul/ {next} {print}'
或者,反转逻辑来缩短命令,
awk 'NF>2 && !/Jul/ {print}'
或者
awk 'NR!=1 && !/Jul/ {print}'
~$ cat input
first line
Gen second third
Feb second third
Mar second third
Apr second third
May second third
Jun second third
Jul second third
Aug second third
Sep second third
Oct second third
Nov second third
Dec second third
~$ awk 'NF<=2 || /Jul/ {next} {print}' input
Gen second third
Feb second third
Mar second third
Apr second third
May second third
Jun second third
Aug second third
Sep second third
Oct second third
Nov second third
Dec second third
~$ awk 'NR==1 || /Jul/ {next} {print}' input
Gen second third
Feb second third
Mar second third
Apr second third
May second third
Jun second third
Aug second third
Sep second third
Oct second third
Nov second third
Dec second third
答案2
@科斯对你的问题给出了绝对正确的答案。
但我认为你试图解决的不是问题。要删除符合特定条件的文件,你应该使用find
参数-delete
。
例如:
find . -type f -newermt 2015-07-07 ! -newermt 2015-07-08 -delete
find . -type f -newerat 2015-07-07 ! -newerat 2015-07-08 -delete