英文:
Group grep results by the searched patterns into an array of objects
问题
\exec("grep -orE '" . $classesBarred . "' ../../front/src/components | sort | uniq -c", $allClassesCount);
其中 $classesBarred
包含一个类的字符串,如 search-unfocused|bg-app|enlarged-window
(但更多)。
当前结果是:
' 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',
....(还有几百行类似的行)
我需要将结果放在一个数组中,类似于:
[
{show: 38}, {action: 123}, {search-unfocused: 90}, {....}
]
编辑:
@Freeman 在我的问题这里提供了一个解决方案,使用了 awk
grep -orE "btn-gray|btn-outline-white" ../../front/src/components | awk -F: '{print $2}' | awk -F/ '{print $NF}' | sort | uniq -c | awk '{print $2 "::" $1}''
这给出了以下结果:
btn-gray::1
btn-outline-white::13
英文:
In PHP I am searching and counting all the use cases of some classes in almost all files, with grep
.
\exec("grep -orE '" . $classesBarred . "' ../../front/src/components | sort | uniq -c", $allClassesCount);
Where $classesBarred
contains a string of classes like search-unfocused|bg-app|enlarged-window
(but much more).
The current result is
' 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',
.... (plus a few hundred more lines like these)
I would need the result in an array, something like:
[
{show: 38}, {action: 123}, {search-unfocused: 90}, {....}
]
EDIT:
@Freeman provided a solution to my question here, using awk
grep -orE "btn-gray|btn-outline-white" ../../front/src/components | awk -F: '{print $2}' | awk -F/ '{print $NF}' | sort | uniq -c | awk '{print $2 "::" $1}'
which gave this result:
btn-gray::1
btn-outline-white::13
答案1
得分: 4
是的,我看到了,你的代码使用awk
将grep
的输出重新排列为两列,一列是类名,另一列是计数,
输出如下:
search-unfocused 90
bg-app 5
enlarged-window 12
现在你可以轻松地通过PHP将这个输出解析为一个数组,方法如下:
$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;
}
}
数组的结果如下:
[
"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"]"}';
结果如下:
[
{"show": 38},
{"action": 123},
{"search-unfocused": 90},
{....}
]
祝你好运!
英文:
yes as I can see, your code uses awk
to rearrange output from grep
into two columns, one is class name, and two contains the count,
the the output is like this :
search-unfocused 90
bg-app 5
enlarged-window 12
now you can easily, parse this output into an array by PHP like this :
$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;
}
}
and the array result is like below :
[
"search-unfocused" => 90,
"bg-app" => 5,
"enlarged-window" => 12,
...
]
Update :<br>
If you insist on using awk and sed, you can do it like this:
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"]"}'
and the result is like this :
[
{"show": 38},
{"action": 123},
{"search-unfocused": 90},
{....}
]
good luck!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论