嗨,我都想做这个
2022-11-14 18:49:59 Indicator is < 3
1 No
2022-11-14 18:49:59 Indicator is < 10
1 No
2022-11-14 18:49:59 Indicator is < 22
1 No
2022-11-14 18:49:59 Indicator is < 1
1 No转到
2022-11-14 18:49:59 Indicator is < 3 1 No
2022-11-14 18:49:59 Indicator is < 10 1 No
2022-11-14 18:49:59 Indicator is < 22 1 No
2022-11-14 18:49:59 Indicator is < 1 1 No我发现你可以在第一、第三、第五、第五、第一、第三、第五、……中使用sed 's/something/some//2'。就这样一个
发布于 2022-12-01 13:44:35
使用模块化的awk来尝试这一点。
$ awk -v val=2 'NR % val == 0{print prev, $0} {prev = $0}' file
2022-11-14 18:49:59 Indicator is < 3 1 No
2022-11-14 18:49:59 Indicator is < 10 1 No
2022-11-14 18:49:59 Indicator is < 22 1 No
2022-11-14 18:49:59 Indicator is < 1 1 No它查看记录号NR并计算其中的模2。因为第二行的输出为0,所以它将打印前一个prev和当前的$0行。
备注:
发布于 2022-12-01 14:48:50
$ awk '{printf "%s%s", $0, (NR%2 ? OFS : ORS)}' file
2022-11-14 18:49:59 Indicator is < 3 1 No
2022-11-14 18:49:59 Indicator is < 10 1 No
2022-11-14 18:49:59 Indicator is < 22 1 No
2022-11-14 18:49:59 Indicator is < 1 1 No这与@AndreWildberg's answer之间的细微差别是:
在内存中不包含行,而Andres持有1行(不是问题,只是一个difference).
。
$ seq 5 | awk '{printf "%s%s", $0, (NR%2 ? OFS : ORS)}'
1 2
3 4
5 $vs
$ seq 5 | awk 'NR % 2 == 0{print prev, $0} {prev = $0}'
1 2
3 4
$发布于 2022-12-01 21:04:44
另一种方法
$ paste - - <file
2022-11-14 18:49:59 Indicator is < 3 1 No
2022-11-14 18:49:59 Indicator is < 10 1 No
2022-11-14 18:49:59 Indicator is < 22 1 No
2022-11-14 18:49:59 Indicator is < 1 1 No或(更脆弱)
$ pr -w112 -2at file
2022-11-14 18:49:59 Indicator is < 3 1 No
2022-11-14 18:49:59 Indicator is < 10 1 No
2022-11-14 18:49:59 Indicator is < 22 1 No
2022-11-14 18:49:59 Indicator is < 1 1 Nohttps://stackoverflow.com/questions/74642704
复制相似问题