to grep and print their counts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting to grep and print their counts
# 1  
Old 10-13-2007
to grep and print their counts

suppose u have a file
ACFCFACCACARCSHFARCVJVASTVAJFTVAJVGHBAJ

another file
A
C
F
R

then output shud be
A= 9
C=7
F=3
R=2

Thanks
# 2  
Old 10-13-2007
with zsh:

Code:
zsh 4.3.4% cat file
ACFCFACCACARCSHFARCVJVASTVAJFTVAJVGHBAJ
zsh 4.3.4% cat file1
A
C
F
R
zsh 4.3.4% <file1 while read;do printf "%s=%d\n" "$REPLY" "${#$(<file)//[^$REPLY]}";done
A=9
C=7
F=4
R=2

P.S. F is 4, not 3 Smilie
# 3  
Old 10-13-2007
one more,

Code:
awk 'BEGIN{ while( getline < "firstfile" ) { len = length($0); for ( i=1; i<=len; i++ ) { arr[substr($0, i, 1)]++ } }} { printf "%s=%d\n",  $0, arr[$0] }' secondfile

# 4  
Old 10-13-2007
sorry

Quote:
Originally Posted by radoulov
with zsh:

Code:
zsh 4.3.4% cat file
ACFCFACCACARCSHFARCVJVASTVAJFTVAJVGHBAJ
zsh 4.3.4% cat file1
A
C
F
R
zsh 4.3.4% <file1 while read;do printf "%s=%d\n" "$REPLY" "${#$(<file)//[^$REPLY]}";done
A=9
C=7
F=4
R=2

P.S. F is 4, not 3 Smilie
sorry not getting with it
if u use shell script or with awk
Thanks
thanks
# 5  
Old 10-13-2007
Did you try with what I had posted ?
# 6  
Old 10-13-2007
Hi.

Using standard utilities:
Code:
#!/usr/bin/env sh

# @(#) s1       Demonstrate split of line into characters, filter, sort, count.

set -o nounset
echo

debug=":"
debug="echo"

## Use local command version for the commands in this demonstration.

echo "(Versions used in this script displayed with local utility "version")"
version bash sed grep sort uniq

echo

FILE=${1-data1}

sed -e 's/\(.\)/\1\n/g' $FILE |
grep -f data2 |
sort |
uniq -c

exit 0

To produce:
Code:
% ./s1

(Versions used in this script displayed with local utility version)
GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
GNU sed version 4.1.2
grep (GNU grep) 2.5.1
sort (coreutils) 5.2.1
uniq (coreutils) 5.2.1

      9 A
      7 C
      4 F
      2 R

Noting that "F" is indeed 4: using a local utility:
Code:
% describe -c data1
    1 lines read
    1 words read
   40 chars read

   39 length of longest line
    1 occurrences
    1 line at which first seen
    1 line at which last seen

   39 length of shortest (non-zero length) line
    1 occurrences
    1 line at which first seen
    1 line at which last seen

  1:1 columns:lines.
       1 newline
       9 A
       1 B
       7 C
       4 F
       1 G
       2 H
       4 J
       2 R
       2 S
       2 T
       5 V

Best wishes ... cheers, drl
# 7  
Old 10-13-2007
Code:
awk 'BEGIN{FS=""}
 FNR==NR{
     for(i=1;i<=NF;i++){
          a[$i]++
     }
     next;
 }
 { print $1,a[$1]}
' "file" "file2"

Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep patterns and group counts

Hi, I have a continuous log file which has the following format:- 02/Sep/2015: IP 11.151.108.166 error occurred etc 03/Sep/2015: IP 11.151.108.188 error occurred etc 03/Sep/2015: IP 11.152.178.250 error occurred etc 03/Sep/2015: IP 11.188.108.176 error occurred etc 03/Sep/2015: IP... (4 Replies)
Discussion started by: finn
4 Replies

2. UNIX for Dummies Questions & Answers

Grep and Print

Hi , I have data in below pattern 05:00:13,184 WARN ContentTransferService - CTS:createContent - File Name:470208627 Total time taken(ms):5137 05:00:13,184 WARN ContentTransferService - CTS:createContentWithFolderPath(uploadDocument) - File Name:295918481 User Id:xyz Total time... (5 Replies)
Discussion started by: Abhayman
5 Replies

3. Shell Programming and Scripting

Print lines before and after..not grep -A

Hi I have this in my file 2011-04-18 15:32:11 system-alert-00012: UDP flood! From xxxxxx to yyyyyyyyyy, int ethernet0/2). Occurred 1 times. 2011-04-18 15:32:11 system-alert-00012: UDP flood! From xxxxxx to yyyyyyyyyy, int ethernet0/2). Occurred 1 times. 2011-04-18 15:32:11... (9 Replies)
Discussion started by: zorrox
9 Replies

4. Shell Programming and Scripting

Counts a number of unique word contained in the file and print them in alphabetical order

What should be the Shell script that counts a number of unique word contained in a file and print them in alphabetical order line by line? (7 Replies)
Discussion started by: proactiveaditya
7 Replies

5. UNIX for Dummies Questions & Answers

Grep & print

I just need to print value 12 digit number after the key *MI*. My big concern is the below lines are not fixed format or length so cant cut based on the position. XSA*00**00**XZ*DA-Paper*30*942411167****MI*010001990802~AEE XSA*00**00**ZZ*EA-aper*30*94169****MI*010001960802~SDRE*ER... (7 Replies)
Discussion started by: gunaah
7 Replies

6. Shell Programming and Scripting

MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else

Hi Guys, I need to set the value of $7 to zero in case $7 is NULL. I've tried the below command but doesn't work. Any ideas. thanks guys. MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else { print $7}}' ` Harby. (4 Replies)
Discussion started by: hariza
4 Replies

7. Shell Programming and Scripting

Print lines after grep

Hi all, I need help in following scenario. I have a file with about 10,000 lines. There are several lines which have word "START" (all upper case) in them. I want to grep line with word "START" and then do the following 1. Print the line number having word "START" 2. Print the next 11 lines. ... (4 Replies)
Discussion started by: jakSun8
4 Replies

8. Shell Programming and Scripting

using grep and print filename

Hi, I have a question on bash. Basically I would like to print a file name using bash. I am actually trying to grep a particular character in sequential files. I have alot files such that a.txt, b.txt,c.txt...etc. If I found a certain character, I would print that particular filename. I... (5 Replies)
Discussion started by: ahjiefreak
5 Replies
Login or Register to Ask a Question