Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get a number from a String 1

Status
Not open for further replies.

zytalyb

Programmer
Oct 17, 2011
15
DE
Dear all, I need your help in the following problem:
When SQL script is executed, so I get the following data as an email message. I saved this message as a text and renamed it.

SQL*Plus: Release xx.x.x.x.x Production on Mon Jun 16 10:30:00 2011

Copyright (c) 1982, 2010, Oracle. All rights reserved.
SQL> Connected.
SQL> SQL> SQL> SQL> FILE_ONE_007
SQL> 2 9876
SQL> FILE_TWO_007
SQL> 2 12345.5
SQL> FILE_THREE_007
SQL> 2 10.5
SQL> SQL> FILE_4
SQL> 2 -999998

I need a script to get the following data with the format:
Mon Jun 16 10:30:00 2011, 007,98767, 12345.5, 109.5

I have tried the following:
gawk " / Production on / {print $6, $7, $8, $9, $10 }" test.txt, and got the first part:
Mon Jun 16 10:30:00 2011

but I think that there is other way to get it also instead of that i used, also I have no idea how to get the rest.
Any idea ? thanks a lot.
with regards
 
This may get you started. I assumed 10.7 in the file and 109.7 in the required output were supposed to be the same number
Code:
/Production on/ {a= $6 " " $7 " " $8 " " $9 " " $10 ","}
/FILE_ONE/ {a=a substr($NF,10)}
$2==2 {b[++i]= ", " $3}
END {print a b[1] b[2] b[3]}

CaKiwi
 
Thanks a lot CaKiwi for your fast answer, but I'm afraid it does not working well when I tried it. I do not no why?
I have also another question, assume we have an output as a result of my problem described above, (Mon Jun 16 10:30:00 2011, 007,98767, 12345.5, 109.5) is it possible to use awk to create any txt. file that save that result? Sorry but I'm new programmer and have no idea about awk at all.

kind regards
 
What errors or results did you get?

You can put the program into a file, test.awk say, and run it with
Code:
gawk -f test.awk test.txt
Or you can put include the program on the command line
Code:
gawk '/Production on/ {a= $6 " " $7 " " $8 " " $9 " " $10 ","}/FILE_ONE/ {a=a substr($NF,10)} $2==2 {b[++i]= ", " $3} END {print a b[1] b[2] b[3]}' test.txt
To save the output into a file, add
Code:
> test.out
to the end of the command line

CaKiwi
 
Hi CaKiwi,
I have tried it and became the error message "unexpected newline or end of string".
The good thing is the >test.out works well, I have tried it with my first code and become only the date in the output file.
The other gawk -f test.awk test.txt gives a syntax error message.

thanks in advance
 
I copied the line for the 2nd method I gave above and pasted it into a terminal. It worked without error. What error do you get with the 1st method using test.awk?

CaKiwi
 
Hi,
The second method you used gave me this error:
gawk: '/Production
gawk: ^ invalid char ''' in expression

I have tried with "/ Production.... instead of '/Production .... then I have:
gawk: cmd. line:1: /Production on/ {a= $6
gawk: cmd. line:1: ^ unexpected newline or end of string
errcount: 1
 
What operating system are you using? I'm guessing windows. If so, put this code into a file test.awk
Code:
/Production on/ {a= $6 " " $7 " " $8 " " $9 " " $10 ","}
/FILE_ONE/ {a=a substr($NF,10)}
$2==2 {b[++i]= ", " $3}
END {print a b[1] b[2] b[3]}
Then run by using
Code:
gawk -f test.awk test.txt

CaKiwi
 
Hi CaKiwi,
I use the win 7 OS.
I have tried your last code and it seems to be working except that the Number in the string FILE_ONE_ (007) is not displayed.
Here is the output:
Mon Jun 16 10:30:00 2011,, 9876, 12345.5, 10.5
The number 007 should be displayed between the ' '.
thanks a lot for your replay
 
Thank you very much CaKiwi for your help, it is really great :), I'm very happy now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top