Die folgenden beiden Befehle werden verwendet, um die Anzahl der Ganzzahlen ungleich Null in der zweiten Spalte der Textdatei zu zählen. Kann mir bitte jemand den regulären Ausdruck im Detail erklären?
grep -c '^[^,]*,[^0][^,]*,' < myfile.txt
sed '/^[^,]*,0,.*/d' < myfile.txt | sed -n '$='
Antwort1
Der erste reguläre Ausdruck sucht nach allen Zeilen, die Folgendes enthalten:
'^ - start of line, followed by
[^,]* - 0 or more non-comma characters, followed by
, - a comma, followed by
[^0] - any single character other than a zero, followed by
[^,]* - 0 or more non-comma characters, followed by
,' - a comma
grep -c zählt die Anzahl der übereinstimmenden Zeilen
Der zweite reguläre Ausdruck stimmt überein
'/ (the start of the regex)
^ - start of line, followed by
[^,]* - 0 or more non-comma characters, followed by
,0, - a comma then a zero then a comma, followed by
.* - 0 or more other characters
/d' (the end of the regex -- delete the lines matching the preceding expression)