Quantcast
Channel: Bashタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 2819

コマンドラインから特定の文字列の数を数える

$
0
0

以下のようにすれば良い

echo ${数える元となる文字列} | grep -o ${数えたい文字列} | wc -l

例えば以下の通り

(例)アンダーバー"_"の数を数える場合
$ echo 1_2_3_4_5_6_7_8_9_0 | grep -o '_' | wc -l
  9

スクリプト化するなら以下の通り

test.sh
#!/bin/bash -xSRC_STR="1_2_3_4_5_6_7_8_9_0"TARGET_STR="_"echo${SRC_STR} | grep-o${TARGET_STR} | wc-l
$ bash test.sh
9

Viewing all articles
Browse latest Browse all 2819