我是一名新的生物技术专业的学生,正在研究我的编程任务,我已经碰壁了。问题如下。
“(3)在你的新”.bashrc1“文件中,每行中有多少空格分隔的第二个条目的数字为0 ... 9?将你的答案输出到一个新文件”.bashrc1 .counts“in the form”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"|
另外,我得到了如何输出到文件,但是世界上如何以这种方式格式化输出? 我已经筋疲力尽了谷歌和我的讲义,似乎无法找到与我的问题相关的任何信息。
编辑:文件我正在贪图;只是一个默认的.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 ~"
答案 0 :(得分:3)
根据我的理解,您的输出应包含从0
到9
的每个数字的单独条目,因此您可能无法使用没有循环的1命令生成它。
使用for
循环,您可以执行类似
for c in {0..9}; do
cut -d ' ' -f 2 .bashrc1 | grep -c "$c" >> .bashrc1.counts
done
答案 1 :(得分:1)
:
awk '$2~/\d+/{print $2}' .bashsrc
in perl
perl -F" " -ane 'if($F[1]=~m/\d+/){print $F[1]}' .bashsrc
答案 2 :(得分:1)
试试这个:
more .bashrc1 | awk '{print $2}' | grep –n "[0-9]" > .bashrc1.counts
答案 3 :(得分: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开始计数
然后,对于每一行,如果第二项包含一个或多个0,则它将c0
递增1(无论第二项中可能有多少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
与上面相同,但它将第二项分成我们正在寻找的数字上的数组,并将计数器变量增加该数组的长度减去1。这有效地计算了找到数字的次数。
输出:
0,2
1,2
2,1
3,0
4,0
5,1
6,0
7,0
8,0
9,0