grep and count no of occurences in every line of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep and count no of occurences in every line of a file
# 1  
Old 04-20-2007
Question grep and count no of occurences in every line of a file

Hi Folks,

I have a input file of the below format.


~~~OLKIT~OLKIT~1~~TBD~BEST PAGER & WIRELESS~4899 COMMON MARKET PLACE~~~DUBLIN~KS~43016~I~Y~DIRECT~D~~0
BPGRWRLS~~~OLKIT~OLKIT~1~~TBD~BEST PAGER & WIRELESS~4899 COMMON MARKET PLACE~~~DUBLIN~KS~43016~I~Y~DIRECT~D~~0
BPI~~~OLKIT~OLKIT~1~~TBD~CITI CONNECTION~6413 18TH AVE~~~BROOKLYN~KS~11204~~Y~DIRECT~D~~0
BPNABOF~~~OLKIT~OLKIT~1~~TBD~BRIGHTPOINT NORTH AMERICA~501 AIRTECH PARKWAY~~~PLAINFIELD~KS~46168~~Y~DIRECT~DT~~0
BRADSINC~~~OLKIT~OLKIT~1~~TBD~BRADSHAW INC~4662 HORN LAKE RD~~~MEMPHIS~KS~38109~I~Y~DIRECT~D~~0
BWRES07TM~~~OLKIT~OLKIT~1~~TBD~BLUEFISH WIRELESS INC.~6100 WEST 96TH ST., SUITE 140~~~INDIANAPOLIS~KS~46278~~Y~DIRECT~J~~0
BYFGRPTM~~~OLKIT~OLKIT~1~~TBD~BYF GROUP, INC.~4810 BEAUREGARD ST.~~~ALEXANDRIA~KS~22312~~Y~DIRECT~F~~0



In the input file I have many no of lines which have "~" symbols in them.
In every line, I need to count the no. of times "~" occurs and compare it with a variable which is passed along with the shell script in the command line.

I have highlited each line in diffn color.

The command line format is
shell_script input_file.dat 20

In the above line 20 is the no to which the no of "~" are compared

Help is very much required as I am very new into shell scripting.


Regards
Srikanth GR

Last edited by srikanthgr1; 04-20-2007 at 06:30 AM.. Reason: Needed to give more details.
# 2  
Old 04-20-2007
Try this:
Code:
#!/usr/bin/ksh

numtilde=$1

while read line; do
        tildeinline=$(($(echo $line | tr '~' '\n' | wc -l)-1))
        echo "Comparing with $numtilde. Current line has $tildeinline ~s"
done < test

The file test contains your data. BTW, I think that all lines have the same number of ~s (at least in the example they do).
# 3  
Old 04-20-2007
Something like this?

Code:
awk 'NF!=n+1{print "Record",NR,"has",NF-1,"[~] :",$0}' FS="~" n=20 inputfile

Set n to your shell variable (n="var").
This User Gave Thanks to radoulov For This Post:
# 4  
Old 04-20-2007
Quote:
Originally Posted by blowtorch
Try this:
Code:
#!/usr/bin/ksh

numtilde=$1

while read line; do
        tildeinline=$(($(echo $line | tr '~' '\n' | wc -l)-1))
        echo "Comparing with $numtilde. Current line has $tildeinline ~s"
done < test

The file test contains your data. BTW, I think that all lines have the same number of ~s (at least in the example they do).
Code:
tildeinline=$(echo $line | tr -dc '~' | wc -c)

# 5  
Old 04-20-2007
Quote:
Originally Posted by anbu23
Code:
tildeinline=$(echo $line | tr -dc '~' | wc -c)


Or (with bash/ksh93?):

Code:
lno=1;while IFS= read -r;do 
	l=${REPLY//[^~]/} 
	printf "%s ~s in record number %s\n" ${#l} $lno 
	lno=$((lno+1))
done<inputfile

# 6  
Old 04-20-2007
Quote:
Originally Posted by radoulov
Something like this?

Code:
awk 'NF!=n+1{print "Record",NR,"has",NF-1,"[~] :",$0}' FS="~" n=20 inputfile

Set n to your shell variable (n="var").
Hi

I have a small doubt in the above code.
How can i capture the NF variable out of that line
and use this variable for validation ?

Regards
Srikanth GR
# 7  
Old 04-20-2007
Quote:
Originally Posted by srikanthgr1
Hi

I have a small doubt in the above code.
How can i capture the NF variable out of that line
and use this variable for validation ?

Regards
Srikanth GR
Can you give some example for what validation you do?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count occurences of a character in a file by sorting results

Hello, I try to sort results of occurences in an array by using awk but I can't find the right command. that's why I'm asking your help ! :) Please see below the command that I run: awk '{ for ( i=1; i<=length; i++ ) arr++ }END{ for ( i in arr ) { print i, arr } }' dictionnary.txt ... (3 Replies)
Discussion started by: destin45
3 Replies

2. Shell Programming and Scripting

Count the occurences of strings

I have some text files in a folder f1 with 10 columns. The first five columns of a file are shown below. aab abb 263-455 263 455 aab abb 263-455 263 455 aab abb 263-455 263 455 bbb abb 26-455 26 455 bbb abb 26-455 26 455 bbb aka 264-266 264 266 bga bga 230-232 230 ... (10 Replies)
Discussion started by: gomez
10 Replies

3. Shell Programming and Scripting

How to grep and count the occurences in while read script?

Hi, I have a file while is the output of lspath command and output of file is #cat lspath.txt Enabled hdisk0 vscsi0 Enabled hdisk0 vscsi1 Enabled hdisk1 vscsi0 Enabled hdisk2 vscsi0 Enabled hdisk2 vscsi1 Missing hdisk3 vscsi0 Enabled hdisk3 vscsi1 Have created script to check state... (7 Replies)
Discussion started by: ksgnathan
7 Replies

4. Shell Programming and Scripting

Count of matched pattern occurences by minute and date in a log file

Anyone knows how to use AWK to achieve the following Sun Feb 12 00:41:01-00:41:59 Success:2 Fail:2 Sun Feb 12 00:42:01-00:42:59 Success:1 Fail:2 Sun Feb 12 01:20:01-01:20:59 Success:1 Fail:2 Mon Feb 13 22:41:01-22:41:59 Success:1 Fail:1 log file: Success Success Fail Fail ... (9 Replies)
Discussion started by: timmywong
9 Replies

5. UNIX for Dummies Questions & Answers

Count pattern occurences

hi, I have a text..and i need to find a pattern in the text and count to the no of times the pattern occured. i have used grep command ..but the problem is , it shows the occurrences of the pattern but doesn't count no of times the pattern occuries. (5 Replies)
Discussion started by: nvnni
5 Replies

6. Shell Programming and Scripting

Count occurences of string

Hi, Please help me in finding the number of occurences of the string. Example: Apple, green, blue, Apple, Orange, green, blue are the strings can be even in the next line. The o/p should look as: Word Count ----- ----- Apple 2 green 2 Orange 1 blue 2 Thanks (2 Replies)
Discussion started by: acc888
2 Replies

7. Shell Programming and Scripting

Count of matched pattern occurences by time in a log file

We have a log file, the format is similar to this: 08/04/2011 05:03:08 Connection Success 08/04/2011 05:13:18 Connection Success 08/04/2011 05:23:28 Connection Fail 08/04/2011 05:33:38 Connection Success 08/04/2011 06:14:18 Connection Success 08/04/2011 06:24:28 Connection Fail 08/04/2011... (6 Replies)
Discussion started by: clu
6 Replies

8. Shell Programming and Scripting

Awk to count occurences

Hi, i am in need of an awk script to accomplish the following: Input table looks like: Student1 arts Student2 science Student3 arts Student4 science Student5 science Student6 science Student7 science Student8 science Student9 science Student10 science Student11 science... (8 Replies)
Discussion started by: saint2006
8 Replies

9. UNIX for Dummies Questions & Answers

How to count the occurences of a specific word in a file in bash shell

Hello, I want to count the occurences of a specific word in a .txt file in bash shell. Can somebody help me pleaze?? Thanks!!! (2 Replies)
Discussion started by: mskart
2 Replies

10. HP-UX

count occurences of specific character in the file

For counting the occurences of specific character in the file I am issuing the command grep -o 'character' filename | wc -w It works in other shells but not in HP-UX as there is no option -o for grep. What do I do now? (9 Replies)
Discussion started by: superprogrammer
9 Replies
Login or Register to Ask a Question