1

私はプログラミングの割り当ての紹介に取り組んでいる新しいバイオテクノロジーの学生です、私はレンガの壁にぶつかりました。質問は次のとおりです。

"(3)新しい「.bashrc1」ファイルで、各行のスペースで区切られた2番目のエントリの中に0 ... 9の数字が含まれているものはいくつありますか?新しいファイル「.bashrc1.counts」に回答をパイプしてください。 「number、count ...」(例:0、12 ...)の形式で。

私がこれまでにやったことは

more .bashrc1 |  awk ‘{print $2}’ | grep –c “0..9” > .bashrc1.counts

grepの部分がおそらく間違っていることは知っていますが、範囲を渡す方法はありますか?お気に入り

grep -c "0","1"... etc

または私はしなければなりませんか

|grep -c "0"|grep -c "1"|

また、ファイルに出力する方法はわかりますが、世界でどのように出力をそのような方法でフォーマットするのですか?グーグルと講義ノートを使い果たしましたが、問題に関連する情報が見つからないようです。

編集:私がgrepしているファイル; エイリアスが1つ追加されたデフォルトの.bashrcファイルのコピーです。

# Sample .bashrc for SuSE Linux
# Copyright (c) SuSE GmbH Nuernberg
# There are 3 different types of shells in bash: the login shell, normal shell
# and interactive shell. Login shells read ~/.profile and interactive shells
# read ~/.bashrc; in our setup, /etc/profile sources ~/.bashrc - thus all
# settings made here will also take effect in a login shell.
#
# NOTE: It is recommended to make language settings in ~/.profile rather than
# here, since multilingual X sessions would not work properly if LANG is over-
# ridden in every subshell.
# Some applications read the EDITOR variable to determine your favourite text
# editor. So uncomment the line below and enter the editor of your choice :-)
#export EDITOR=/usr/bin/vim
#export EDITOR=/usr/bin/mcedit
# For some news readers it makes sense to specify the NEWSSERVER variable here
#export NEWSSERVER=your.news.server
# If you want to use a Palm device with Linux, uncomment the two lines below.
# For some (older) Palm Pilots, you might need to set a lower baud rate
# e.g. 57600 or 38400; lowest is 9600 (very slow!)
#
#export PILOTPORT=/dev/pilot
#export PILOTRATE=115200
test -s ~/.alias && . ~/.alias || true
alias start = "ls ~"   
4

4 に答える 4

3

私が理解していることから、出力には から0までの各番号の個別のエントリが含まれている必要が9あるため、おそらくループなしで 1 つのコマンドで生成することはできません。

ループを使用すると、次のforようなことができます

for c in {0..9}; do
     cut -d ' ' -f 2 .bashrc1 | grep -c "$c" >> .bashrc1.counts
done
于 2012-09-26T16:58:36.963 に答える
1

これで試してください:

more .bashrc1 | awk '{print $2}' | grep –n "[0-9]" > .bashrc1.counts

于 2012-09-26T17:01:57.597 に答える
1

awkで:

awk '$2~/\d+/{print $2}' .bashsrc

パールで

perl -F" " -ane 'if($F[1]=~m/\d+/){print $F[1]}' .bashsrc
于 2012-09-26T17:01:15.063 に答える
0

これが私がそれに取り組む方法です:

awk -v c0=0 -v c1=0 -v c2=0 -v c3=0 -v c4=0 -v c5=0 -v c6=0 -v c7=0 -v c8=0 -v c9=0 \
'$2~/0/{ c0+=1 } \
$2~/1/{ c1+=1 } \
$2~/2/{ c2+=1 } \
$2~/3/{ c3+=1 } \
$2~/4/{ c4+=1 } \
$2~/5/{ c5+=1 } \
$2~/6/{ c6+=1 } \
$2~/7/{ c7+=1 } \
$2~/8/{ c8+=1 } \
$2~/9/{ c9+=1 } \
END {print  "0," c0 "\n1," c1 "\n2," c2 "\n3," c3 "\n4," c4 "\n5," c5 "\n6," c6 "\n7," c7 "\n8," c8 "\n9," c9}' file

c0基本的に、これは第 2 項 ( 、c1、 ... )で各桁が何回見つかったかをカウントする変数を定義しc9、カウントを 0 から開始します。

次に、各行で、第 2 項に 1 つ以上の 0 が含まれている場合は、c01 ずつ増加します (第 2 項に 0 がいくつあっても構いません)。「1」が含まれている場合は、増加しますc1。次に、指定した方法ですべてを出力します (#,c#)。

出力:

0,1
1,1
2,1
3,0
4,0
5,1
6,0
7,0
8,0
9,0

第 2 項に各数字が何回出現したかを正確に知る必要がある場合は、以下を使用します。

awk -v c0=0 -v c1=0 -v c2=0 -v c3=0 -v c4=0 -v c5=0 -v c6=0 -v c7=0 -v c8=0 -v c9=0 \
'$2~/0/{ split($2,a0,"0"); c0+=(length(a0)-1) } \
$2~/1/{ split($2,a1,"1"); c1+=(length(a1)-1) } \
$2~/2/{ split($2,a2,"2"); c2+=(length(a2)-1) } \
$2~/3/{ split($2,a3,"3"); c3+=(length(a3)-1) } \
$2~/4/{ split($2,a4,"4"); c4+=(length(a4)-1) } \
$2~/5/{ split($2,a5,"5"); c5+=(length(a5)-1) } \
$2~/6/{ split($2,a6,"6"); c6+=(length(a6)-1) } \
$2~/7/{ split($2,a7,"7"); c7+=(length(a7)-1) } \
$2~/8/{ split($2,a8,"8"); c8+=(length(a8)-1) } \
$2~/9/{ split($2,a9,"9"); c9+=(length(a9)-1) } \
END {print  "0," c0 "\n1," c1 "\n2," c2 "\n3," c3 "\n4," c4 "\n5," c5 "\n6," c6 "\n7," c7 "\n8," c8 "\n9," c9}' file

上記と同じですが、探している桁で第 2 項を配列に分割し、その配列の長さから 1 を引いた分だけカウンター変数をインクリメントします。これにより、数字が見つかった回数が効果的にカウントされます。

出力:

0,2
1,2
2,1
3,0
4,0
5,1
6,0
7,0
8,0
9,0
于 2012-09-27T16:35:07.423 に答える