To count a string with in a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To count a string with in a variable
# 1  
Old 09-01-2010
To count a string with in a variable

I am writing a ksh to check for duplicate records in two different set of tables on oracle database, to get this i am running two plsql qurries at a time through the ksh, so the output of the qurries will be stored in variable say "SQL_STRING". So now to say if duplicate records exists in table or not i have to write a script which checks the count of the word "no rows selected" in the SQL_STRING variable.

One of our friend on UNIX.COM helped me with the code below which counts from a file instead of from a variable
Code:
#!/bin/ksh
str='no rows selected'
num=$(awk '{n+=gsub(str,"")}END{print n}' str="${str}" mySampleFile.txt)
echo "Total number of [${str}] = ${num}"

For example the variable SQL_STRING has data as below:

no rows selected no rows selected
ora-1234 sql querry failed
no rows selected

Now we should look for the string "no rows selected" (note: it has spaces between) in the variable SQL_STRING and then store the count of the string in a variable.
for the above data the output should be 3 since the string "no rows selected" occurred three times.
Any help is appreciated.
# 2  
Old 09-01-2010
Question Is this homework? Looks like a post I read earlier today.

Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.
# 3  
Old 09-01-2010
This is not an home work, i am new to forums concept and to the unix too.
I have a situation at work where i need to compare two oracle tables and see wheatear we have duplicates or not. if duplicates exists then i should ftp those records to mainframe system and if no duplicates exists then should go to next step.
i have posted this question previously and got an answer to it, but once i started working on it i found some changes and then posted a new thread.

Last edited by vpv0002; 09-22-2010 at 06:09 PM..
# 4  
Old 09-01-2010
If the command you referenced is working, modulo the variable issue, then this will probably do the trick:

Code:
str="no rows selected"
num=$(echo "$SQL_STRING" | awk -v str="$str" '{ n += gsub (str, "" ); } END{ print n+0; }')
echo $num

Depending on your needs, you could also just pipe the output of the database query command directly into awk and not worry about any size limits that the shell might put on the contents of a variable.

The 'n+0' in the print ensures that you get a number out. If the query returns no output, n will not have been set and the print will print a blank rather than 0; the n+0 forces it to print a zero.

---------- Post updated at 20:07 ---------- Previous update was at 19:39 ----------

As was pointed out in another post just a few minutes ago, if the SQL_STRING is empty, echo will still print a single newline so n should always be set. Caution never hurts though.

Last edited by agama; 09-01-2010 at 08:40 PM.. Reason: corrected typo
# 5  
Old 09-01-2010
Thank you very muchSmilie

Thanks for the explanation too
# 6  
Old 09-01-2010
Quote:
Originally Posted by vpv0002
Thank you very muchSmilie

Thanks for the explanation too
You are most welcome. I believe that it is most important to give the 'why' with the 'how,' and not just the 'how.' I also believe in commenting my code Smilie
# 7  
Old 09-01-2010
you are right, it helps people like us who are learners.
I appreciate it buddy. Once again thanks a lotSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check if string variable is a subset of another string variable

Below is my ksh shell script where I need to check if variable fileprops is a subset of $1 argument. echo "FILE PROPERTY: $fileprops" echo "PARAMETER3: $1" if ; then echo "We are Good. $line FILE is found to be INTACT !! " else echo... (2 Replies)
Discussion started by: mohtashims
2 Replies

2. Shell Programming and Scripting

awk variable search and line count between variable-search pattern

Input: |Running the Rsync|Sun Oct 16 22:48:01 BST 2016 |End of the Rsync|Sun Oct 16 22:49:54 BST 2016 |Running the Rsync|Sun Oct 16 22:54:01 BST 2016 |End of the Rsync|Sun Oct 16 22:55:45 BST 2016 |Running the Rsync|Sun Oct 16 23:00:02 BST 2016 |End of the Rsync|Sun Oct 16 23:01:44 BST 2016... (4 Replies)
Discussion started by: busyboy
4 Replies

3. Shell Programming and Scripting

Grep a string and count following lines starting with another string

I have a large dataset with following structure; C 0001 Carbon D SAR001 methane D SAR002 ethane D SAR003 propane D SAR004 butane D SAR005 pentane C 0002 Hydrogen C 0003 Nitrogen C 0004 Oxygen D SAR011 ozone D SAR012 super oxide C 0005 Sulphur D SAR013... (3 Replies)
Discussion started by: Syeda Sumayya
3 Replies

4. UNIX for Dummies Questions & Answers

Comparing a String variable with a string literal in a Debian shell script

Hi All, I am trying to to compare a string variable with a string literal inside a loop but keep getting the ./testifstructure.sh: line 6: #!/bin/sh BOOK_LIST="BOOK1 BOOK2" for BOOK in ${BOOK_LIST} do if then echo '1' else echo '2' fi done Please use next... (1 Reply)
Discussion started by: daveu7
1 Replies

5. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

6. UNIX for Dummies Questions & Answers

How to set a variable with a count variable i.e. VARIABLE$COUNT

Hi All I've very nearly finished this script I'm working on but have hit another idiots problem, after googling I can't see a solution for this one. I have a while count loop that checks checks two consecutive values then increments the count by two. What the script has to do is then check... (5 Replies)
Discussion started by: Bashingaway
5 Replies

7. Shell Programming and Scripting

count of a string within a variable

I am writing ksh to check for duplicate records in two different set of tables on oracle database, to get this i am running two plsql qurries at a time through the ksh, so the output of the qurries will be stored in variable say "SQL_STRING". So now to say if duplicate records exists in table or... (3 Replies)
Discussion started by: vpv0002
3 Replies

8. Shell Programming and Scripting

Appending string, variable to file at the start and string at end

Hi , I have below file with 13 columns. I need 2-13 columns seperated by comma and I want to append each row with a string "INSERT INTO xxx" in the begining as 1st column and then a variable "$node" and then $2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13 and at the end another string " ; COMMIT;" ... (4 Replies)
Discussion started by: Vaddadi
4 Replies

9. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies

10. Shell Programming and Scripting

select count(*) into a variable

Hi, Could anybody help me how can I assign the value of "select count(*) from table1" to a variable in an unix shell script. Thanks. (4 Replies)
Discussion started by: nguda
4 Replies
Login or Register to Ask a Question