Reputation: 1365
I'm a new biotechnology student working on my introduction to programming assignment, I've hit a brick wall. The question is as follows.
"(3) In your new “.bashrc1” file, how many of the space-delimited 2nd entries on each line have the numbers 0...9 in them? Pipe your answer out to a new file “.bashrc1.counts” in the form “number, count ... ” (eg 0, 12 ...) ."
What I've done so far is
more .bashrc1 | awk ‘{print $2}’ | grep –c “0..9” > .bashrc1.counts
I know the grep part is probably wrong, is there any way to pass it a range? like
grep -c "0","1"... etc
or do I have to do
|grep -c "0"|grep -c "1"|
Also, I get how to output to the file, but how in the world do I format the output in such a manner? I've exhausted google and my lecture notes, and can't seem to find any relevant info with my problem.
EDIT: file I'm grepping to; just a copy of the default .bashrc file with one added alias.
# 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 ~"
Upvotes: 1
Views: 2252
Reputation: 4117
here's how i'd tackle it:
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
basically, this defines a variable to count how many times each digit is found in a 2nd term (c0
, c1
, ... c9
) and starts the count at 0.
then, for each line, if the 2nd term contains one or more 0s, it increments c0
by 1 (no matter how many 0s there may be in the 2nd term). if it contains "1", it increments c1
, and so forth. then it prints it all out in the manner you specified (#,c#).
output:
0,1
1,1
2,1
3,0
4,0
5,1
6,0
7,0
8,0
9,0
if you need to know exactly how many times each number appeared in the 2nd term, use the below:
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
same as the above, but it splits the 2nd term into an array on the digit we're looking for, and increments the counter variable by the length of that array minus one. this effectively gives a count of the number of times the digit was found.
output:
0,2
1,2
2,1
3,0
4,0
5,1
6,0
7,0
8,0
9,0
Upvotes: 0
Reputation: 4924
Try with this:
more .bashrc1 | awk '{print $2}' | grep –n "[0-9]" > .bashrc1.counts
Upvotes: 1
Reputation: 67211
in awk:
awk '$2~/\d+/{print $2}' .bashsrc
in perl
perl -F" " -ane 'if($F[1]=~m/\d+/){print $F[1]}' .bashsrc
Upvotes: 1
Reputation: 65781
From what I understand, your output should contain separate entries for each number from 0
to 9
, so you probably can't produce it with 1 command without a loop.
With a for
loop, you can do something like
for c in {0..9}; do
cut -d ' ' -f 2 .bashrc1 | grep -c "$c" >> .bashrc1.counts
done
Upvotes: 3