sumitc
1
Suppose I have a string " 1 A B C D F 3 F G 3 1 D L D ".
Please let me know the command which tells the above string having 3 Ds.
echo $str | grep -c "D"
It returns only 1 I know the reason but I am looking some command which should give 3.
Actually I have a file, which I need to read and store the total numbers of ":" in each record.
Any help is highly appreciated.
echo "1 A B C D F 3 F G 3 1 D L D" | grep -o "D" | wc -l
echo "1 A B C D F 3 F G 3 1 D L D" | tr ' ' '\n' | grep -c "D"
x="1 A B C D F 3 F G 3 1 D L D"
y=${x//[^D]}
echo ${#y}
CarloM
3
Try
echo " 1 A B C D F 3 F G 3 1 D L D " | tr -cd "D"| wc -c
sumitc
4
echo " 1 A B C D F 3 F G 3 1 D L D " | tr -cd "D"| wc -c
Worked perfectly...
grep -o won't work in HP Unix...Sorry I forgot to mention the Unix flavor...
Again thanks for all your help !
---------- Post updated at 07:12 AM ---------- Previous update was at 07:04 AM ----------
But could you please explain the meaning of the command...
I tried to understand (man tr) but didn't get it.
echo " 1 A B C D F 3 F G 3 1 D L D " | tr -cd "D"
It returns nothing....please explain, if possible.
CarloM
5
-d
deletes characters which match the specified set, -c
modifies it to use the complement of the set instead (anything which isn't D).
akshay@nio:~$ echo " 1 A B C D F 3 F G 3 1 D L D " | awk '{print gsub(search,"&")}' search='D'
3