In PHP verwende ich grep
, um alle Anwendungsf?lle für bestimmte Klassen in fast allen Dateien zu suchen und zu z?hlen.
\exec("grep -orE '" . $classesBarred . "' ../../front/src/components | sort | uniq -c", $allClassesCount);
Stringartig von $classesBarred
包含類似于search-unfocused|bg-app|enlarged-window
(aber mehr).
Das aktuelle Ergebnis ist
' 2 ../../front/src/components/Actions/ActionOwner.vue:show', ' 1 ../../front/src/components/Actions/ActionOwner.vue:action', ' 1 ../../front/src/components/Actions/ActionOwner.vue:show', ' 5 ../../front/src/components/Actions/ActionOwner.vue:action', ' 1 ../../front/src/components/Actions/ActionOwner.vue:show', ....(還有幾百行類似的結(jié)果)
Ich muss die Ergebnisse in einem Array speichern, etwa so:
[ {show: 38}, {action: 123}, {search-unfocused: 90}, {....} ]
Herausgeber:
@Freeman bietet hier eine L?sung mit awk
grep -orE "btn-gray|btn-outline-white" ../../front/src/components | awk -F: '{打印 }' | awk -F/ '{print $NF}' |排序| uniq-c| awk '{print "::" }'
Habe folgende Ergebnisse erhalten:
btn-gray::1 btn-outline-white::13
是的,我可以看到,你的代碼使用 awk
將 grep
的輸出重新排列為兩列,一列是類名,另一列是計數(shù),
輸出結(jié)果如下:
search-unfocused 90 bg-app 5 enlarged-window 12
現(xiàn)在你可以通過 PHP 將這個輸出解析為一個數(shù)組,代碼如下:
$results = array(); foreach ($allClassesCount as $line) { $parts = explode(" ", $line); $className = $parts[0]; $count = (int)$parts[1]; if (!isset($results[$className])) { $results[$className] = $count; } else { $results[$className] += $count; } }
數(shù)組的結(jié)果如下:
[ "search-unfocused" => 90, "bg-app" => 5, "enlarged-window" => 12, ... ]
更新:
如果你堅持使用 awk 和 sed,你可以這樣做:
grep -orE "$classesBarred" ../../front/src/components | awk -F '/' '{print $NF}' | awk -F ':' '{print $2}' | sort | uniq -c | awk '{gsub(/^[ \t]+|[ \t]+$/, "", $2); print "{\""$2"\": "$1"},"}' | paste -sd '' | sed 's/,$//' | awk '{print "["$0"]"}'
結(jié)果如下:
[ {"show": 38}, {"action": 123}, {"search-unfocused": 90}, {....} ]
祝你好運!