How to use a string in the for loop?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use a string in the for loop?
# 1  
Old 12-16-2014
How to use a string in the for loop?

Hi Guys,

I have the below 2 exception which I want to monitor and subsequently many more. My problem is since the For loop takes the syntax for variable in word1 word2 as separate entities and my error strings are consisting of many words , how to treat the whole error string as a single entity.

Code:
 
Exception 1: org.com.Exception JMS MQ Error
Exception 2: org.db.Exception Db2 SQL Error

What can I do to modify the below code so that I can include individual exceptions in a for loop? The below code will treat each word as a different variable

Code:
for exception in org.com.Exception JMS MQ Error org.db.Exception Db2 SQL Error

---------- Post updated at 01:55 AM ---------- Previous update was at 01:43 AM ----------

Ok. I got it .. It shud be in the following format.

Code:
for exception in "org.com.Exception JMS MQ Error" "org.db.Exception Db2 SQL Error"

---------- Post updated at 03:59 AM ---------- Previous update was at 01:55 AM ----------

Now I am facing another issue where the Awk script is not recognizing the variable passed in for loop i.e. in the below script, it is probably searching for $error string instead of the value of error variable.

How to make awk script recognize the value of the error variable?

Code:
 
rm /tmp/jvm1error.txt
echo "Error" >/tmp/jvm1error.txt
for error in "org.com.Exception JMS MQ Error" "org.db.Exception Db2 SQL Error"
do
a=$(awk -vDT=$(date +"%y%m%d%H%M" -d"- 3 hour") '
                     {sub(/^\[/,"")
                      split ($1, D, "/")
                      split ($2, T, ":")
                      AT=sprintf ("%02d%02d%02d%02d%02d", D[3], D[1], D[2], T[1], T[2])}
             AT > DT && /$error/
        ' /logs/websphere/*jvm1*/SystemOut.log)
 
echo "$a" >> /tmp/jvm1error.txt
done

# 2  
Old 12-16-2014
Why don't you use the same mechanism as you do for DT? And, why passing the whole through the "a" variable?
# 3  
Old 12-17-2014
Yeah, a the "a" variable is redundant. I dint get the using the same mechanism part. Are you saying I should store the error value in a user defined awk variable and then use it?
# 4  
Old 12-17-2014
Yes, you can pass the $error variable to awk as you did for DT

Code:
awk -vDT=$(date +"%y%m%d%H%M" -d"- 3 hour") -vERROR=$error

This User Gave Thanks to pravin27 For This Post:
# 5  
Old 12-17-2014
And, don't use /ERROR/ (which is a regex constant) but $0 ~ ERROR in lieu.
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to call a string by string from a file to use in for loop?

Hi, I have a below sql query. select * from table where name in ('${name}); I have a requirement to automate this as here i need to pass the string value name from txt file. The text file contains the differnet special characters and i have some 200 names in the text file as follows. 3"... (3 Replies)
Discussion started by: Samah
3 Replies

2. UNIX for Dummies Questions & Answers

Loop with Perl (string search)

I am using a perl script to reverse and complement sequences if a string is found. The script works as expected as standalone but I would like to use it in my bash file. However, I am not getting my expected result. My test.txt file >Sample_72... (8 Replies)
Discussion started by: Xterra
8 Replies

3. Shell Programming and Scripting

Searching for a string using loop.

Hi i am very new to shell scripting. I have got stuck on a portion on my script. Problem : I have 30 logfiles inside /home/test directory. I need to find the string "@ended today" in all the 30 logfiles and print the name of the files which did not have this string. i need to this by... (3 Replies)
Discussion started by: Soma Das
3 Replies

4. Shell Programming and Scripting

for loop ( string having spaces )

Dear All, i facing problem to use string having spaces in for loop.. file used for FOR LOOP command.txt rpm -t -v ttm -D -r RJLL -h YELP rpm -t -v ttm -D -r RJLL -h ERRT rpm -t -v ttm -D -r RJLL -h TYYE rpm -t -v ttm -D -r RJLL -h POOL CODE using for execute above command... (3 Replies)
Discussion started by: arvindng
3 Replies

5. Shell Programming and Scripting

String concatenation not working in a loop

Hi, First post, so I hope someone can help me with this weirdness :) I have a number files with some rows of information I want to extract, at the same time I want to add to a string some details from the file. I have found two different ways of looping over rows in a file, but one method... (5 Replies)
Discussion started by: LostInTheWoods
5 Replies

6. Shell Programming and Scripting

find a string in a loop

Hi, Can anyone help with the problem below? I need to read all text files (file1, file2, file3) in a loop and find file1.txt: file2.txt: File3.txt: (7 Replies)
Discussion started by: Lenora2009
7 Replies

7. Shell Programming and Scripting

How to loop through every char in a string

for example this string: gLZMQp8i Loop become easy if we add space between each char, How to do it? or other solutions are welcome. (9 Replies)
Discussion started by: honglus
9 Replies

8. Shell Programming and Scripting

Sed appending string using for loop?

Hi All, I have been trying to format a file using sed. I can't seem to get the syntax right. I want to append the string from file1.txt to file1.csv with the final output file being file2.csv, but before the string is appended to the end of each line, first insert a comma. Here is the sed... (2 Replies)
Discussion started by: cstovall
2 Replies

9. Shell Programming and Scripting

Help with if loop (string comparison)

Hi Can someone please tell me what is wrong with this (ksh).. if + ]] then echo ${COMP_TEMP} fi What i need here is, say if the variable is a 1 or 2 digit number, then execute the if loop. Basically the variable can either be 1-30 or some other character sequence say '?', '&&'... (4 Replies)
Discussion started by: psynaps3
4 Replies

10. UNIX for Dummies Questions & Answers

if statement in for loop of a string

I am attempting to pass a string into awk and loop through it, and then for every occurrance of a certain character perform an action. In this case, for example, echo 1 for each time character r is found in the string. Except I can't get it to work. Could someone please tell me why? echo... (7 Replies)
Discussion started by: Sniper Pixie
7 Replies
Login or Register to Ask a Question