我有一個文字文件,每行單字都用逗號分隔,如下所示:
7022122465,0,\N,,0,2015-09-29 10:48:33
7022597642,0,\N,,0,2015-09-29 10:48:33
7022848906,0,\N,,0,2015-09-29 10:48:33
7022848906,5,\N,,0,2015-09-29 10:48:33
7022848906,55,\N,,0,2015-09-29 10:48:33
.....................................etc
我想在 Linux/UNIX 中僅使用sed
or命令來計算第二列的非零數。grep
筆記
不使用其他指令:
cut -d',' -f2 < KAR_UBONA_UBONACT15_20150929_20150930_FEEDBACK.txt | grep -vcw 0
但我不僅僅想要cut
,我需要使用grep
.
答案1
您可以使用-c
grep 選項。您可以使用以下命令刪除第一個逗號之前的所有字元以及第二個逗號之後的所有內容sed
:
sed 's/^[^,]*,//;s/,.*//' < the_file | grep -c -E '[^0]'
編輯:此sed
命令的作用與您的命令相同,cut
因此您也應該能夠使用原始grep
命令。
EDIT2:如果您只想使用一個命令,您可以使用 @cuonglm grp 答案。如果您只想使用一次調用為了總結最後的行數,需要sed
對標籤進行大量工作。
sed -E -n '
s/^[^,]*,[^0,]+,.*/+1/ # replace the lines we are interested in with "+1"
T delete_line # if we did not do a substitution right now we jump to "delete_line"
H # we did not jump (so we did the substitution and append the "+1" to the hold space
: delete_line # the label, here we do nothing (silently drop the current line)
$ { # on the last line we ...
s/.*/0/ # replace the whole line with "0"
G # append the hold space (all the "+1" from before")
s/\n//g # remove all newlines
p # print the line
}' < the_file
現在可以將其通過管道傳輸bc
,或者您可以p
用一些複雜的sed
魔法替換該命令,以將這些數字匯總到sed
.我相信我聽說這sed
已經完成,所以它應該是可能的。
如果你只想使用一個程式( sed
) 但不介意多次呼叫它,這樣會容易得多:
sed '/^[^,]*,0,.*/d' < the_file | sed -n '$='
答案2
答案3
grep -c '^[^,]*,[-+0-9.]*[1-9]'
這應該涵蓋表示為12
, -1
, 0e+12
, 01
, 的數字0.0001
。但不是 for0xFF
或Inf
orNaN
例如,所以這仍然與更規範的不同:
POSIXLY_CORRECT=1 awk -v n=0 -F , '$2 != 0 {n++}; END{print n}'
如果您的輸入有以這種格式表示的數字。
對於sed
唯一的解決方案,您可以這樣做:
sed '/^[^,]*,[-+0-9]*[1-9]/!d' | sed -n '$='
但對於只有一次調用的解決方案sed
,我們需要手動進行算術。
sed -n '
1{x;s/$/0,:0123456789,0/;x;}
/^[^,]*,[-+0-9]*[1-9]/ {
x;:1
s/^,/1/;s/\(.\),\(.*:.*\1\(,*.\)\)/\3\2/;t1
s/:/,:/
x
}
${x;s/,.*//p;}'