Reusing a variable in a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reusing a variable in a script
# 1  
Old 10-11-2013
Lightbulb Reusing a variable in a script

I was breaking my head for a day still not successful


I have variable x for following is assigned

Code:
x=awk '/[0-9]_[0-9].*successful/{getline;getline;getline;print}' log filename | awk '{print $2}'

echo "$x"

2341

I have this piece of code inside a loop which runs n times.

I want to print or echo the value of "x" every time when it comes out of the loop (i.e if the loop condition is true). But unfortunately when
I echo the variable "x" 1st time it contains single value and when I echo variable "x" on the 2nd run of the loop I see values getting appended
to the 1st value i.e tab separated. If the loop runs for 5 times and if I echo the variable "x" at the last i get something like this
Code:
echo $x

2341 4665 5661 2342 1911

or

echo "$x"

2341
4665
5661
2342
1911

My goal is to get these values individually something like this

After the 1st loop:

Code:
echo $x
2341

After the 2nd loop:

Code:
echo $x
4665

Afetr the 3rd loop:

Code:
echo $x
5661

After the 4th loop:

Code:
echo $x
2342

After the 5th loop:

Code:
echo $x
1911

# 2  
Old 10-11-2013
Perhaps try putting your awk statement in a for loop:

Code:
for x in $(awk '/[0-9]_[0-9].*successful/{getline;getline;getline;print}' log filename | awk '{print $2}')
do
    echo $x
done

This User Gave Thanks to in2nix4life For This Post:
# 3  
Old 10-11-2013
If the filename contains each data as a new line its simple:
Code:
all="";C=0
while read line;do echo "Line:$C - $line";all+=" $line";let C++;done<filename
echo $all

You could also try:
Code:
for entry in $(cat filename);do echo "Working with: $entry...";done

EDIT
:doh: there are times i miss a delete button, completly overseen the awk statement.

Last edited by sea; 10-11-2013 at 10:34 AM..
# 4  
Old 10-11-2013
Quote:
Originally Posted by rpm120
. . .
Code:
x=awk '/[0-9]_[0-9].*successful/{getline;getline;getline;print}' log filename | awk '{print $2}'

I'm pretty sure that statement will NOT assign any number to x let alone a series of numbers. Would you make it a correct command substitution x=$(...), then still it should assign one number only in each loop. And, as the command obviously does not change in the loop, the result should always be the same identical number.

Please post relevant parts of your script plus input (partial) files, so we can reproduce what you describe above.
# 5  
Old 10-11-2013
Lightbulb

Quote:
Originally Posted by RudiC
I'm pretty sure that statement will NOT assign any number to x let alone a series of numbers. Would you make it a correct command substitution x=$(...), then still it should assign one number only in each loop. And, as the command obviously does not change in the loop, the result should always be the same identical number.

Please post relevant parts of your script plus input (partial) files, so we can reproduce what you describe above.
Following are the actual lines from the script:

Code:
#!/bin/ksh

fname=$1

for batchname in $(grep -i "Processing batch" $fname | cut -d "'" -f2)
do
Batch_state=`grep -c -i "Batch '$batchname' was successful" $fname`
if [[ "$Batch_state"  -ge 1 ]];then

S_Time=`awk '/[0-9]_[0-9].*successful/{getline;getline;getline;print}' $fname | awk '{print $2}'`
E_Time=`awk '/[0-9]_[0-9].*successful/{getline;getline;getline;getline;print}' $fname| awk '{print $2}'`
echo $batchname"\t"$S_Time"\t"$E_Time
}
else
{
echo $batchname encountered an error
}
fi
done

The Output that this is producing

Code:
1231324        14:29:04 15:29:11    14:32:19 15:33:11
3097935        14:29:04 15:29:11    14:32:19 15:33:11

Desired Output:

Code:
1231324        14:29:04     14:32:19 
3097935        15:29:11     15:33:11

Sample Input

Code:
 2013/06/11 13:26:57 <0999> (725102)
    Creating batch '12_2013162201.0'...
    2013/06/11 13:26:57 <0999> (725102)
    Batch '12_2013162201' was successful
    2013/06/11 13:26:57 <0999> (725102)
TMR:Child ZERO, 160 Docs 320 Pgs 3874 KByts Tot 0.42 WAL 0.10 WALIO 0.15 IO 0.03 secs
    2013/06/11 13:26:57 <0999> (725102)  Processing batch '12_2013162201'
    2013/06/11 13:26:57 <0999> (725102)
    Total in batch: 160 documents using 4 KBytes

Hope this one is helpful.

---------- Post updated at 10:29 AM ---------- Previous update was at 09:06 AM ----------

Quote:
Originally Posted by in2nix4life
Perhaps try putting your awk statement in a for loop:

Code:
for x in $(awk '/[0-9]_[0-9].*successful/{getline;getline;getline;print}' log filename | awk '{print $2}')
do
    echo $x
done

In my scenario I cannot use a for condition. I have pasted the actual lines from the code :

Code:
#!/bin/ksh

fname=$1

for batchname in $(grep -i "Processing batch" $fname | cut -d "'" -f2)
do
Batch_state=`grep -c -i "Batch '$batchname' was successful" $fname`
if [[ "$Batch_state"  -ge 1 ]];then

S_Time=`awk '/[0-9]_[0-9].*successful/{getline;getline;getline;print}' $fname | awk '{print $2}'`
E_Time=`awk '/[0-9]_[0-9].*successful/{getline;getline;getline;print}' hp.log | awk '{print $2}'`
echo $batchname"\t"$S_Time"\t"$E_Time
}
else
{
echo $batchname encountered an error
}
fi
done


Last edited by rpm120; 10-11-2013 at 11:53 AM..
# 6  
Old 10-12-2013
Applied to your sample input, above script yields
Code:
12_2013162201    13:26:57    13:26:57

I can't see any error - no reason to complain. Please convince me that sth is wrong!
This User Gave Thanks to RudiC For This Post:
# 7  
Old 10-12-2013
Lightbulb

Quote:
Originally Posted by RudiC
Applied to your sample input, above script yields
Code:
12_2013162201    13:26:57    13:26:57

I can't see any error - no reason to complain. Please convince me that sth is wrong!
Additional to the above info about the log, the given above log has 9 lines and these 9 lines are the unique lines, In real time my original log will have this 9 lines or this block repeated n times with batch numbers changed in each block or sequence .

Hence now coming to the point when this script is used for a log with more than one block or one sequence , the problem arises there I.e all the extracted are saved in a varible tab separated as shown above.

Hope I m clear.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

2. Shell Programming and Scripting

Shell script to pass the config file lines as variable on the respective called function on a script

I want to make a config file which contain all the paths. i want to read the config file line by line and pass as an argument on my below function. Replace all the path with reading config path line by line and pass in respective functions. how can i achieve that? Kindly guide. ... (6 Replies)
Discussion started by: sadique.manzar
6 Replies

3. Shell Programming and Scripting

Passing variable from called script to the caller script

Hi all, Warm regards! I am in a difficult situation here. I have been trying to create a shell script which calls another shell script inside. Here is a simplified version of the same. Calling Script. #!/bin/ksh # want to run as a different process... (6 Replies)
Discussion started by: LoneRanger
6 Replies

4. Shell Programming and Scripting

Script variable help, Varying number of arguments to excute script

Hi Guy's. Hopefully someone can help me with what I am trying to archieve. So situation currently is, I have a script already setup however I have another script that sits infront of it. The main script basically goes and searchs multiple platforms for a list of entered data. In... (10 Replies)
Discussion started by: mutley2202
10 Replies

5. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

6. UNIX for Dummies Questions & Answers

[Solved] Reusing a 'NOT' command

Hi Guys I'm trying to search for all lines that that do not contain the dash (-) in it. eg - I have the following list of numbers.I want to see those that donot contain the (-) in it.In this case '20151' 20151-60882 20151-60883 20151-60891 20151 20151-60893 Thanking you in advance. (3 Replies)
Discussion started by: Prega
3 Replies

7. Programming

FTP - reusing data connections

Hi there, This may be a stupid question, but... Is it possible to use one data connection channel initiated on a FTP server for sending multiple commands (LIST, NLST, MLSD)? Thanks in advance! (1 Reply)
Discussion started by: Yezu
1 Replies

8. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

9. Shell Programming and Scripting

How to return a value of a variable from shell script to perl script

HI , Is there any way to return a value of variable from shell to perl script. Code: === Perl file my $diff1=system("sh diff.sh"); my $diff2=system("sh diff1.sh"); I need exit status of below commands i.e 0 and 1 respectively. Since in both the cases diff is working so system... (3 Replies)
Discussion started by: srkelect
3 Replies

10. UNIX for Dummies Questions & Answers

extracting text and reusing the text to rename file

Hi, I have some ps files where I want to ectract/copy a certain number from and use that number to rename the ps file. eg: 'file.ps' contains following text: 14 (09 01 932688 0)t the text can be variable, the only fixed element is the '14 ('. The problem is that the fixed element can appear... (7 Replies)
Discussion started by: JohnDS
7 Replies
Login or Register to Ask a Question