1

我是一名新的生物技术学生,正在研究编程作业的介绍,但我遇到了障碍。问题如下。

“(3) 在你的新“.bashrc1”文件中,每行中有多少以空格分隔的第二个条目中包含数字 0...9?将你的答案输出到一个新文件“.bashrc1.counts”以“数字,计数...”的形式(例如 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"|

另外,我知道如何输出到文件,但是我究竟如何以这种方式格式化输出?我已经用尽了谷歌和我的讲义,似乎找不到任何与我的问题相关的信息。

编辑:我正在寻找的文件;只是带有一个添加别名的默认 .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

据我了解,您的输出应该包含从0to的每个数字的单独条目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

在 awk 中:

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

在 perl 中

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

试试这个:

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

于 2012-09-26T17:01:57.597 回答
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, c1, ... c9) 中出现的次数,并从 0 开始计数。

然后,对于每一行,如果第 2 项包含一个或多个 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

如果您需要确切知道每个数字在第二项中出现了多少次,请使用以下命令:

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

与上面相同,但它将第二项拆分为我们要查找的数字上的数组,并将计数器变量增加该数组的长度减一。这有效地计算了找到数字的次数。

输出:

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 回答