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
# 8  
Old 04-20-2007
Quote:
Originally Posted by anbu23
Can you give some example for what validation you do?
I need to Validate the No Of "~"s occuring in each line to a variable which is passes in the command line to the shell script.

Basically the command line will be

count_Tilde.sh input_file.dat $columns

here columns is the numerical variable.
# 9  
Old 04-20-2007
Quote:
Originally Posted by srikanthgr1
I need to Validate the No Of "~"s occuring in each line to a variable which is passes in the command line to the shell script.

Basically the command line will be

count_Tilde.sh input_file.dat $columns

here columns is the numerical variable.
Code:
awk ' NF!=n+1 {print "Record",NR,"has",NF-1,"[~] :",$0}' FS="~" n=$2 $1

Here n is assigned with the value of $columns
Replace NF!=n+1 with whatever condition you need
# 10  
Old 04-20-2007
Java

awk:
Code:
#!/bin/sh
awk -v num=20 'BEGIN { FS = "" }
{
       for ( i=0;i<=NF;i++) { 
          if ($i == "~")  c++
       }
       if ( c == num ) { print "Record: " NR , $0 ;} 
       else { print "NO" }
       c = 0
} ' "file"

If you have Python, here's an alternative:
Code:
#!/usr/bin/python
for line in open("file"):
	if line.count("~") == 20:
		print line

# 11  
Old 04-20-2007
Quote:
Originally Posted by anbu23
Code:
awk ' NF!=n+1 {print "Record",NR,"has",NF-1,"[~] :",$0}' FS="~" n=$2 $1

Here n is assigned with the value of $columns
Replace NF!=n+1 with whatever condition you need
Hi
Thanks for the helpfull code..

What needs to be done
If I need to exit from the script
It will be like If the Validation fails Exit with a return code 1
Else usual exit with return code 0

Code:
awk 'NF!=n{print "Record",NR,"has",NF-1,"[~] :",$0} END { print " Files Not Valid " ,exit 1}' FS="~" n=$2 $1

Will this work considering the exit code is captured in another file .
Which is used for processing.

Like in another file just after this script is processed

Code:
 count_Tilde.sh input_file.dat $columns
err_code = $?

Will the return code be capture in the variable err_code
# 12  
Old 04-20-2007
Since you seem only to want a true/false, this would be easier and potentially much quicker if a fault record exists at the start of a large file.

Code:
awk -F"~" ' NF!=n+1 {exit 1}' n=$2 $1

# 13  
Old 04-20-2007
Hi Pals ..

Thanks for all the help and support...

Regards
Srikanth GR ...
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