Parsing a variable string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing a variable string
# 1  
Old 04-26-2004
Parsing a variable string

Hi all, I have a problem surfacing and I hope you all could help.

What I have to do is take a input file and fill out a fax template from that file. The biggest problem I found was I have to parse the string "//FAX(faxnumber=555-5555;style="style1";
and on and on. The string can be in any order (ie fax# could be first or last) and there could be any number of variables in the string.

I tried AWK and it sort of worked. I had to use some template files and such and it didn't seem to work to smoothly.

Any help would be appreciated,
Thanks,
Dave
# 2  
Old 04-26-2004
Any chance you can post a sample of the data? One complete listing that you need to parse.
# 3  
Old 04-26-2004
Bug

//FAX(fax=1-555-555-7249;class=Day ;style=Order Verification)

would be one version

//FAX(landscape=true;fax=1-555-555-3784; style=API)

could be another.

I'm linking this to a bunch of older unix scripts and they were all developed a bit differently...ugg....and it really isn't feasible to go and change all the legacy scripts so they work the same.

This is what I'm using right now.

Code:
imgOutput="faxOutput/$imgName"
imgBackend="faxWorking/$imgName"
tempFile="faxWorking/$tempFileName"
tempFile2="faxWorking/{$tempFileName}2"
tempFile3="faxWorking/{$tempFileName}3"

awk '/\/\/FAX/' $file | tee "$tempFile"
awk -F"(" '{ print $2 }' "$tempFile" | tee "$tempFile2"

awk -F";" '{ print $1 }' "$tempFile2" | tee "$tempFile3" 
faxNum=`awk -F"=" '{ print $2 }' "$tempFile3"`

awk -F";" '{ print $2 }' "$tempFile2" | tee "$tempFile3"
class=`awk -F"=" '{ print $2 }' "$tempFile3"` 

awk -F";" '{ print $3 }' "$tempFile2" | tee "$tempFile3"
style=`awk -F"=" '{ print $2 }' "$tempFile3"` 

#cleanup
rm "$tempFile"
rm "$tempFile2"
rm "$tempFile3"

The script finds the //FAX keyword and then parses it. It works if it's in the correct order and has a certain number of variables. Otherwise, it kind of dies.
I also need to be able to retrieve different instances of the //FAX keyword in the same file.....
Code:
//FAX(...)
stuff stuff
stuff stuff
//FAX(this is fax#2)
different stuff

Thanks a bunch for your reply. I really appreciate it! Smilie
# 4  
Old 04-26-2004
well provided you always have the dash between each group of digits. you could use a regex like


$parse_string=~/.*fax=(\d+-\d+-\d+-\d+).*/

now $1 will have the numeric string you are looking for. ie 1-555-555-5555

this is useing perl variables btw. i dont have a sys in front of me but i believe it should work you might have to do some minor tweeking.


in an instance like this use regex constructors to your advantage.

\d = a single digit
\D = a non digit
# 5  
Old 04-27-2004
You could use eval and awk...
Code:
#!/usr/bin/ksh

echo FIRST INSTANCE
instance=1
eval `awk -F'[(;=)]' '
  /\/\/FAX/ && ++c==i {
    for(x=2;x<NF;x+=2)
      print $x "=\047" $(x+1) "\047"
  }' i=$instance file1`
echo fax: $fax
echo style: $style
echo class: $class

...tested....

FIRST INSTANCE
fax: 1-555-555-7249
style: Order Verification
class: Day
# 6  
Old 04-28-2004
Thanks, worked like a charm. Wahoo!
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

Parsing a long string string problem for procmail

Hi everyone, I am working on fetchmail + procmail to filter mails and I am having problem with parsing a long line in the body of the email. Could anyone help me construct a reg exp for this string below. It needs to match exactly as this string. GetRyt... (4 Replies)
Discussion started by: cwiggler
4 Replies

3. UNIX for Dummies Questions & Answers

Parsing a variable

Can someone help me? I have been looking in the archives as I am sure this is very simple to do, but I do not know. I have a variable which sometimes contains a file name and sometimes contains a fully qualified file name. I want to be able to separate the directory from the file name into 2... (3 Replies)
Discussion started by: CAGIRL
3 Replies

4. UNIX for Dummies Questions & Answers

Parsing String

Hi, I am trying to do the following in Linux: I have some text as follows: /home/user/backup/scripts/SDW/sql/backup.sql which needs to be parsed: to pick up "backup" So, pick up text after the last '/' until '.' from a string. Can someone please tell me how can I do that? ... (2 Replies)
Discussion started by: thinksys
2 Replies

5. Shell Programming and Scripting

String-parsing!

I need the perl solution for the following : $string="I LOVE INDIA" now, in a new string i need the first character of each word... that is string2 should be "ILN". (10 Replies)
Discussion started by: vijay_0209
10 Replies

6. Shell Programming and Scripting

parsing a variable

Hi, I want to get an input from user and parse the input. The legal characters allowed in the input are alnum(a-zA-Z0-0), . , - Also the first and las characters must be alnum only. e.g if the input is abc.ghh-sok.com then the script should return correct, and if the input is like... (2 Replies)
Discussion started by: g_rohit7
2 Replies

7. Shell Programming and Scripting

Parsing of file for Report Generation (String parsing and splitting)

Hey guys, I have this file generated by me... i want to create some HTML output from it. The problem is that i am really confused about how do I go about reading the file. The file is in the following format: TID1 Name1 ATime=xx AResult=yyy AExpected=yyy BTime=xx BResult=yyy... (8 Replies)
Discussion started by: umar.shaikh
8 Replies

8. Programming

parsing string in c

how can i remove the special characters hi iam print the string variable . suppse: while(str!=NULL) printf("******* %s ********** %d ",str,strlen(str)); output as: ****srinu ******** 5 **** phani******** 63 ****srinu ******** 5 **** phani******** 63 so my problem is how can i... (3 Replies)
Discussion started by: phani_sree
3 Replies

9. Shell Programming and Scripting

Need help on parsing string

String example: /vmfs/volumes/46000471-71d7c414-8f74-0013210cddc3/gistst/gistst.vmx What I would like to do is create a variable and save gistst in it. I thought if I could create an array and split it by '/' then I could use the 4th array element or if they was a way to do a... (13 Replies)
Discussion started by: magnacrazy
13 Replies

10. Shell Programming and Scripting

parsing a string into variable

I know solution to this but I was wondering if its easier than what i think I have to pass 20 parameters to a script, which of course is not working so I parsed $3 to be a pipe deliminated string for instance below a.ksh One Two Compa|Compb|Compc|compd|............. Now i have to read... (5 Replies)
Discussion started by: Anubhav
5 Replies
Login or Register to Ask a Question