我有一个文本文件
DATA SENT TO : 1
DATA SENT TO : 2
DATA SENT TO : 3
DATA SENT TO : 4
DATA SENT TO : 5
Failed to sent data
DATA SENT TO : 6
Failed to sent data
DATA SENT TO : 7
Failed to sent data
DATA SENT TO : 8
DATA SENT TO : 9
% SENT % FAILED AVG Time Time Time Current
Dload Upload Total Spent Left
0 0 0 0 0 0 0
DATA SENT TO : 10
% SENT % FAILED AVG Time Time Time Current
Dload Upload Total Spent Left
0 0 0 0 0 0 0
DATA SENT TO : 11
% SENT % FAILED AVG Time Time Time Current
Dload Upload Total Spent Left
0 0 0 0 0 0 0
DATA SENT TO : 12
% SENT % FAILED AVG Time Time Time Current
Dload Upload Total Spent Left
0 0 0 0 0 0 0
DATA SENT TO : 13
Failed to sent
DATA SENT TO : 14
DATA SENT TO : 15
从文本文件中找出需要发送的数据总数。 如果我尝试使用:wc -l file.txt,那么它将给出总行数。但这不是准确的计数,因为我需要输出为
DATA SENT TO : 1
DATA SENT TO : 2
DATA SENT TO : 3
DATA SENT TO : 4
DATA SENT TO : 5
DATA SENT TO : 9
DATA SENT TO : 10
DATA SENT TO : 11
DATA SENT TO : 12
DATA SENT TO : 13
DATA SENT TO : 15
如果使用了命令sed -e / Failed / {N; d;}->它将删除匹配的和下一行,但是iam无法删除以下3行
% SENT % FAILED AVG Time Time Time Current
Dload Upload Total Spent Left
0 0 0 0 0 0 0
答案 0 :(得分:1)
尚不清楚是要输出行还是要计数行数,或者两者兼而有之:
$ awk '/DATA SENT TO/ && !f{print} {f=/Failed to sent/}' file
DATA SENT TO : 1
DATA SENT TO : 2
DATA SENT TO : 3
DATA SENT TO : 4
DATA SENT TO : 5
DATA SENT TO : 9
DATA SENT TO : 10
DATA SENT TO : 11
DATA SENT TO : 12
DATA SENT TO : 13
DATA SENT TO : 15
$ awk '/DATA SENT TO/ && !f{cnt++} {f=/Failed to sent/} END{print cnt+0}' file
11
$ awk '/DATA SENT TO/ && !f{print;cnt++} {f=/Failed to sent/} END{print "Total=" cnt+0}' file
DATA SENT TO : 1
DATA SENT TO : 2
DATA SENT TO : 3
DATA SENT TO : 4
DATA SENT TO : 5
DATA SENT TO : 9
DATA SENT TO : 10
DATA SENT TO : 11
DATA SENT TO : 12
DATA SENT TO : 13
DATA SENT TO : 15
Total=11
答案 1 :(得分:0)
使用awk对值求和并减去失败的行:
awk '/DATA SENT TO/{sum++} /Failed to sent/{sum--} END{print sum}' test.txt
答案 2 :(得分:0)
这可能对您有用(GNU sed):
var initialArray = [{"key": "key1", "value": "value1"}, {"key": "key2", "value": "value2"}, ...];
var newArray = initialArray .map(function (el) {
var obj=new Object;
obj[el["key"]]=el["value"];
return(obj);
});
禁止隐式打印。删除sed -n '/^Failed to sent/N;/^DATA/p' file
开头的行和下一行。仅打印以Failed to sent
开头的行。
要获取此类行的数量,请使用:
DATA
或者:
sed -n '/^Failed to sent/N;/^DATA/p' file | wc -l