使用bash shell脚本

时间:2015-04-25 23:21:13

标签: bash shell

我可以在python或其他一些高级语言中轻松完成。我感兴趣的是用bash做这件事。

以下是文件格式:

head-xyz
item1
item2
item3
head-abc
item8
item5
item6
item9

我想要做的是打印以下输出:

head-xyz: 3
head-abc: 4

标题将具有与我上面给出的示例类似的特定模式。项目也有特定的模式,如上例所示。我只对每个标题下的项目数感兴趣。

3 个答案:

答案 0 :(得分:4)

您可以使用awk

awk '/head/{h=$0}{c[h]++}END{for(i in c)print i, c[i]-1}' input.file

故障:

  • /head/{h=$0}

    对于匹配/head/的每一行,请设置变量h以记录标题。

  • {c[h]++}

    对于文件中的每一行,更新数组c,该数组存储从标题字符串到行计数的映射。

  • END{for(i in c)print i, c[i]-1}

    最后,遍历数组c中的键并打印键(标题),后跟值(count)。减去一个以避免计算标题本身。

答案 1 :(得分:3)

注意:仅限Bash版本4(使用关联数组)

#!/usr/bin/env bash

FILENAME="$1"
declare -A CNT

while read -r LINE || [[ -n $LINE ]]
do
    if [[ $LINE =~ ^head ]]; then HEADLINE="$LINE"; fi
    if [ ${CNT[$HEADLINE]+_} ];
    then
        CNT[$HEADLINE]=$(( ${CNT[$HEADLINE]} + 1 ))
    else
        CNT[$HEADLINE]=0
    fi
done < "$FILENAME"

for i in "${!CNT[@]}"; do echo "$i: ${CNT[$i]}"; done

输出:

$ bash countitems.sh input
head-abc: 4
head-xyz: 3

这是否回答了你的问题@powerrox?

答案 2 :(得分:0)

如果你不认为// format as array for json conversion $positive = array(); $positive['name'] = "Positive"; foreach ($positive_news as $key => $value){ $ts = $key * 1000 - $adjustment; // adjusting for certain timezone //$positive['data'][] = "[" . $ts . "," . $value['polarity']. "," . $value['count'] . "]"; // What I had previous $positive['data'][] = array($ts, $value['polarity'], $value['count']); // What I changed it to } $negative = array(); $negative['name'] = "Negative"; foreach ($negative_news as $key => $value){ $ts = $key * 1000 - $adjustment; //$negative['data'][] = "[" . $keyadj . "," . $value['polarity'] . "," . $value['count'] . "]"; // What I had previously $negative['data'][] = array($ts, $value['polarity'], $value['count']); // What I changed } // I also imploded the arrays to strings here (I removed that after reading Kacper's suggesting) $news = array(); array_push($news, $positive); array_push($news, $negative); echo json_encode($news, JSON_NUMERIC_CHECK); // this is returned to javascript via ajax and sent directly to the below function function renderPosNegChart(data){ var newschart = { chart: { type: 'spline' }, title: { text: "Test" }, xAxis: { type: "datetime" }, yAxis: { }, series: [data[0]] } $("#newschart").highcharts(newschart); 是一种高级语言,这是另一种方法:

sed

在英文中,sed脚本

  • 默认情况下不打印
  • 在匹配for file in head-*; do echo "$file: \c" sed -n '/^head-/,${ /^head-/d /^item[0-9]/!q p } ' <$file | wc -l done 到文件末尾的行中
    • 删除“头条”
    • 之后,如果找到非项目行,则退出
    • 否则,请打印

/^head-/计算行数。