to grep and print their counts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting to grep and print their counts
# 8  
Old 10-13-2007
... and another one Smilie

Code:
awk 'NR==FNR{p=$0;next}
{r=FS=$0;$0=p;printf "%s=%d\n",r,NF-1}' file1 file2

Use nawk or /usr/xpg4/bin/awk on Solaris.

Example:
Code:
% cat file1              
ACFCFACCACARCSHFARCVJVASTVAJFTVAJVGHBAJ
% cat file2              
A
C
F
R
% awk 'NR==FNR{p=$0;next}
{r=FS=$0;$0=p;printf "%s=%d\n",r,NF-1}' file1 file2
A=9
C=7
F=4
R=2


Last edited by radoulov; 10-13-2007 at 04:44 PM..
# 9  
Old 10-13-2007
Quote:
Originally Posted by cdfd123
sorry not getting with it
if u use shell script or with awk
[...]
Z-Shell is a shell ...
# 10  
Old 10-13-2007
Quote:
Originally Posted by matrixmadhan
Did you try with what I had posted ?
thanx matrix
it really works....
thanx
# 11  
Old 10-13-2007
Hi.

A brief improvisation on radoulov's inventive zsh script -- move the read outside the loop -- probably a negligible difference for short files, however, could make a difference for longer files (a manifestation of my habit of moving invariant code outside Fortran loops Smilie ):
Code:
#!/bin/zsh

# @(#) s2       Demonstrate small optimization, avoid repeated reads.

FILE=${1-data1}

echo
echo " Input $FILE:"
cat $FILE

# <data2 while read;do printf "%s=%d\n" "$REPLY" "${#$(<data1)//[^$REPLY]}";done

echo
echo " Results:"
t1=$(<$FILE)
while read
do
  printf "%s=%d\n" "$REPLY" "${#${t1//[^$REPLY]}}"
done <data2

exit 0

Producing:
Code:
% ./s2

 Input data1:
ACFCFACCACARCSHFARCVJVASTVAJFTVAJVGHBAJ

 Results:
A=9
C=7
F=4
R=2

cheers, drl
# 12  
Old 10-13-2007
Quote:
Originally Posted by drl
Hi.

A brief improvisation on radoulov's inventive zsh script -- move the read outside the loop -- probably a negligible difference for short files, however, could make a difference for longer files (a manifestation of my habit of moving invariant code outside Fortran loops Smilie ):
[...]
Definitely,
just wanted to change my post
(sorry, wrote it quickly Smilie).
# 13  
Old 10-15-2007
awk

Hi,
Hope this one can help you.

Code:
sed '/^$/d' b > tb
sed '/^$/d' a > ta
awk '{
if (length($1)>3)
for (i=1;i<=length($1);i++)
arr[substr($1,i,1)]++
else
print $1" = "arr[$1]
}
' tb ta
rm tb ta

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