I have a file that I need to count the number of "Y"s in the file, but just the "Y"s that fall between certain columns. Like:
file1:
1234567890123456789012345
BBBBBBYBBBBYBBYBBYBBYBBB
I want to count the 'Y's between 17 and 21 = 2 "Y"s
Thanks
I have a file that I need to count the number of "Y"s in the file, but just the "Y"s that fall between certain columns. Like:
file1:
1234567890123456789012345
BBBBBBYBBBBYBBYBBYBBYBBB
I want to count the 'Y's between 17 and 21 = 2 "Y"s
Thanks
Try this:
awk '{
s=substr($0,17,5)
n+=gsub("Y","",s)
}
END{print n}
' file
$ cat input-file
1234567890123456789012345
BBBBBBYBBBBYBBYBBYBBYBBB
$ sed 's/^.\{17\}\(.\)\(.\)\(.\)\(.\).*$/\1\n\2\n\3\n\4/' input-file | grep -c 'Y'
2
thegeek,
grep -c gives the count of the lines that contain the pattern, not the numbers of the pattern in a line.
Regards
(thegeek is outputting characters 17-21 with a newline after each character then counting the number of lines containing a letter "Y" ). Ingenious.
>cat they.txt
1234567890123456789012345
BBBBBBYBBBBYBBYBBYBBYBBBB
BBBBBBYBBBBYBBYBBBBBYBBBB
BBBBBBYBBBBYBBYBYBBBBBBBB
>cut -c17-21 they.txt | sed "s/Y/Y\n/" | grep "Y" | wc -l
4
>cut -c17-21 they.txt | sed "s/Y/Y\n/" | grep "Y" -c
4
Just for the fun of it, another similar approach but using "fold" to break each character in each line into a separate line.
cut -c17-21 filename|fold -w 1| grep "Y"|wc -l
But it doesn't give the desired output:
$ cat file
1234567890123456789012345
BBBBBBYBBBBYBBYBBYBBYBBBB
BBBBBBYBBBBYBBYBBBBBYBBBB
BBBBBBYBBBBYBBYBYBBBBBBBB
$ sed 's/^.\{17\}\(.\)\(.\)\(.\)\(.\).*$/\1\n\2\n\3\n\4/' file|grep -c 'Y'
3
I wouldn't describe it as "ingenious" by any means, but it might work - on Linux at least - even if it does only takes characters 18 to 21
It is not because of grep. It is because of the characters which i have selected.
It should work now,
sed 's/^.\{16\}\(.\)\(.\)\(.\)\(.\)\(.\).*$/\1\n\2\n\3\n\4\n\5/' file | grep -c 'Y'
Selected from the character 17 to 21 as expected.
Right! Also with fold:
sed 's/^.\{16\}\(.\{5\}\).*$/\1/' a |fold -w 1 |grep -c 'Y'
methyl solution without the wc command is shorter:
cut -c17-21 filename|fold -w 1| grep -c 'Y'
Thanks everyone for your suggestions... all of them worked
People, people, people. Can we please stop the process forking insanity? There's only one sane way to approach this problem:
$ cat ydata
1234567890123456789012345
BBBBBBYBBBBYBBYBBYBBYBBBB
BBBBBBYBBBBYBBYBBBBBYBBBB
BBBBBBYBBBBYBBYBYBBBBBBBB
$ i=0; IFS=Y; while read x; do set ${x:16:5}.; ((i+=$#-1)); done < ydata; echo $i
4
Cheers,
Alister