Help with grep & count in ksh


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help with grep & count in ksh
# 8  
Old 10-05-2011
Thanks all. That helps a lot.

Can I use the above code like this:
Code:
count=`nawk '{n+=gsub("esprd","")}END{print n}' myFile`
if [ $count == 0 ] ; then
  echo success
else
 exit

I donno why I keep getting
Code:
pmugstart[8]: test: argument expected
for statement if [ $count == 0 ] ; then


Last edited by radoulov; 10-07-2011 at 04:52 PM.. Reason: Code tags!
# 9  
Old 10-05-2011
Your shell might not have ==, try =.

Try echoing the value of $count to see what you actually get.

You can put that part inside awk too, actually, since you can return an exit status of 1 if n>0, which if reads directly, avoiding needing to use backticks:

Code:
if ! nawk '{n+=gsub("esprd","")}END{exit n>0}' myFile
then
        echo "failure"
        exit 1
fi

echo "success"

# 10  
Old 10-06-2011
Help with grep & count

Thanks Corona688


Code:
if ! nawk '{n+=gsub("esprd","")}END{exit n>0}' myFile

Can gsub be used along with ignore case. Like how to make this work for esPRD/ESprd/ etc.I tried looking but didn't find one.

Thanks a lot in advance.
# 11  
Old 10-06-2011
Quote:
Originally Posted by Green_Star
Thanks Corona688


Code:
if ! nawk '{n+=gsub("esprd","")}END{exit n>0}' myFile

Can gsub be used along with ignore case. Like how to make this work for esPRD/ESprd/ etc.I tried looking but didn't find one.

Thanks a lot in advance.
Code:
nawk '{n+=gsub("esprd","", tolower($0))}END{exit n>0}' myFile

# 12  
Old 10-06-2011
This seems not to work as required.
My file is as follows:
Quote:
No.esPRD.Queue|No.Receive.Queue|||n|120|No_User||No_User|
No.Send.Queue|No.Receive.Queue|||n|120|No_User||No_User|
If I run this code
Code:
nawk '{n+=gsub("esprd","", tolower($0))}END{exit n>0}' myFile

The return value is 5. I thought the value would be 1.

Thank you.
# 13  
Old 10-06-2011
Alternatively, perhaps try grep?
Code:
if grep -qi esprd infile
then 
  echo failure
  exit 1
else
  echo success
fi

# 14  
Old 10-06-2011
Quote:
Originally Posted by Green_Star
The return value is 5. I thought the value would be 1.
I don't understand how you're getting any count from that code at all. It outputs no count! It just returns success if the count is zero, and error for anything else.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Inconsistent `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l`

i have this line of code that looks for the same file if it is currently running and returns the count. `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l` basically it is assigned to a variable ISRUNNING=`ps -eaf -o args | grep -i sfs_pcard_load_file.ksh |... (6 Replies)
Discussion started by: wtolentino
6 Replies

2. Shell Programming and Scripting

Count & Result

Hi All. I'm trying to count with command cat Count.log |sort -t 1 | uniq -c Input File. Back-end Invalid Id Password Back-end Invalid Id Password Success Success Success Back-end Invalid Id Password Back-end User state is invalid Back-end User state is invalid Success... (9 Replies)
Discussion started by: ooilinlove
9 Replies

3. UNIX for Dummies Questions & Answers

How do I count how many times a specific word appear in a file (ksh)?

Hi Please can you help how do I count the number of specific characters or words that appear in a file? (8 Replies)
Discussion started by: fretagi
8 Replies

4. Shell Programming and Scripting

KSH: Split String into smaller substrings based on count

KSH HP-SOL-Lin Cannot use xAWK I have several strings that are quite long and i want to break them down into smaller substrings. What I have String = "word1 word2 word3 word4 .....wordx" What I want String1="word1 word2" String2="word 3 word4" String3="word4 word5" Stringx="wordx... (5 Replies)
Discussion started by: nitrobass24
5 Replies

5. Shell Programming and Scripting

Count lines and use if then ksh

I try to count number of lines of a data.txt file and then if number of lines is greater than 1 then email me the file. I could not find what is wrong with my code, hope you can point out the mistake i made #! /bin/ksh count =`cat /from/file/data.txt | wc -l` if ]; then mailx -s... (4 Replies)
Discussion started by: sabercats
4 Replies

6. Shell Programming and Scripting

Using Grep & find & while read line in a script

Hello people! I would like to create one script following this stage I have one directory with 100 files File001 File002 ... File100 (This is the format of content of the 100 files) 2012/03/10 12:56:50:221875936 1292800448912 12345 0x00 0x04 0 then I have one... (0 Replies)
Discussion started by: Abv_mx81
0 Replies

7. UNIX for Dummies Questions & Answers

Grep char count & pipe to sed command

Hi I am having a 'grep' headache Here is the contents of my file: (PBZ,CP,(((ME,PBZ,BtM),ON),((ME,((PBZ,DG),(CW9,PG11))),CW9,TS2,RT1))) I would like to count out how many times 'PBZ' occurs and then place that number in the line above 3... (8 Replies)
Discussion started by: cavanac2
8 Replies

8. Shell Programming and Scripting

combination between || and && in IF condition with ksh

Dear All, Please advice about this issue. when i run this line in a script if && || && || && if i enter $x = test3 and $y = test1 the If condition apply while it should not Best Regards (2 Replies)
Discussion started by: islam.said
2 Replies

9. UNIX for Dummies Questions & Answers

Difference between grep, egrep & grep -i

Hi All, Please i need to know the difference between grep, egrep & grep -i when used to serach through a file. My platform is SunOS 5.9 & i'm using the korn shell. Regards, - divroro12 - (2 Replies)
Discussion started by: divroro12
2 Replies

10. UNIX for Dummies Questions & Answers

Sorting using count, grep and count

Hi, I trying to sort information in a file by making count for every object. For example: A B A D A B C i would like to sort out count for each object (A's, B's and etc) but in actual case i dont know the object but i know the position ofthe object. do i need to use array as well? Any... (2 Replies)
Discussion started by: sukhpal_78
2 Replies
Login or Register to Ask a Question