将搜索模式的组合grep结果分组到一个对象数组中

huangapple go评论55阅读模式
英文:

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

是的,我看到了,你的代码使用awkgrep的输出重新排列为两列,一列是类名,另一列是计数,
输出如下:

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 &quot;$classesBarred&quot; ../../front/src/components | awk -F &#39;/&#39; &#39;{print $NF}&#39; | awk -F &#39;:&#39; &#39;{print $2}&#39; | sort | uniq -c | awk &#39;{gsub(/^[ \t]+|[ \t]+$/, &quot;&quot;, $2); print &quot;{\&quot;&quot;$2&quot;\&quot;: &quot;$1&quot;},&quot;}&#39; | paste -sd &#39;&#39; | sed &#39;s/,$//&#39; | awk &#39;{print &quot;[&quot;$0&quot;]&quot;}&#39;

and the result is like this :

[
  {&quot;show&quot;: 38},
  {&quot;action&quot;: 123},
  {&quot;search-unfocused&quot;: 90},
  {....}
]

good luck!

huangapple
  • 本文由 发表于 2023年6月22日 19:35:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531464.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定