LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-24-2010, 09:49 AM   #31
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,438

Rep: Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879Reputation: 1879
Lightbulb


Hey catkin

I am sure you are probably getting sick of me
But I had another crack at this ( and made some quite different changes I thought might interest you.

Also, I stripped yours out and threw it in a script to (as wasn't sure how to run yours as is).
I have attached the output from both yours and my script (only a few discrepancies as yours seemed
to handle remarks as blank lines) and also your script as i was running it (as I said this could be wrong
way to do it) and mine is below in case you were interested

Cheers again for the challenge (although I would love to see how someone like ghostdog would do it,
probably a couple of one-liners <hehe>)

Code:
#!/usr/bin/awk -f

BEGIN {
	FS="[ \t]*=[ \t]*"
	f = 1
	g = 1
	cnt = 0
	value_arr_cnt = 0
}

$0 !~ /{|}/ && $0 !~ FS{ next }

{
	gsub(/^[ \t]+|#.*$/,"", $1)
	gsub(/[ \t]+$/, "", $1)
	key_org = "'"$1"'"
	gsub(/[ \t]+/,"", $1)
	key = tolower($1)
}

$2 ~ /#/ && index($2, "\"") > index($2, "#"){ f = 0 }

f && $2 ~ /".*"/{

	split($2, value, "\"")
	for (c in value)value_arr_cnt++

	for (x in value){
		gsub(/\\\\/, "SAVE", value[x])

		if (value[x] ~ /\\$/)
			gsub(/\\/, "\"", value[x])

		if (x % 2 == 1){
			gsub(/^[ \t]+|[ \t]+$/, "", value[x])

			if (value[x] ~ /^#.*/)
				for (y = x; y <= value_arr_cnt; y++)
					delete value[y]
		}
	
		if (value[x] ~ /"/ && value[x+1] != "")
			value[x+1] = value[x] value[x+1]

		gsub(/\\+/, "", value[x])
		gsub("SAVE", "\\", value[x])
	}

	g = 0
	value_arr_cnt = 0
}

g{
	gsub(/[ \t\"\\]+|#.*$/,"",$2)

	value[1] = $2
}

{
	print "keyword_org="key_org
	print "keyword="key
	for (x in value){
		if ( value[x] != "\"" && value[x] != "")
			print "conf_values["cnt++"]='"value[x]"'"
	}

	delete value
	f = 1
	g = 1
}
Attached Files
File Type: txt cat_parser.awk.txt (3.6 KB, 1 views)
File Type: txt catout.txt (2.2 KB, 1 views)
File Type: txt myout.txt (1.8 KB, 1 views)
 
Old 03-24-2010, 10:33 AM   #32
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,529
Blog Entries: 27

Original Poster
Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by grail View Post
Hey catkin

I am sure you are probably getting sick of me
But I had another crack at this ( and made some quite different changes I thought might interest you.

Also, I stripped yours out and threw it in a script to (as wasn't sure how to run yours as is).
I have attached the output from both yours and my script (only a few discrepancies as yours seemed
to handle remarks as blank lines) and also your script as i was running it (as I said this could be wrong
way to do it) and mine is below in case you were interested
You have been busy, grail!

And you identified a bug in my code -- leading tabs and spaces are not stripped from keyword_org

Regards your code, its output looks OK. Ideally I would have an expected output file and diff the actual output against it but my code (as the bash function name suggests) parses a single line so I do not have such an expected output file.

For performance reasons, if I was starting over, I would write the awk to parse the whole file as yours does, setting keyword[i], value_1[i], value_2[i], ... value_n[i] and not setting anything for functionally empty lines, again as yours does.

For maintainability, I'd modify your code especially changing "f" and "g" for names that show their meaning/purpose and adding a few comments. It is amazing how even one's own code can be hard to understand a few years later; if somebody else has to understand it ...

I'd also modify it so it didn't break if quoted values include the string "SAVE". No one would ever use such a string ... or might they!?

Best

Charles
 
  


Reply

Tags
awk, bacula


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script is enterpreting $1, $2 values in awk script ... praveen_218 Programming 4 09-14-2009 03:38 PM
awk in script Jurrian Linux - Newbie 13 10-30-2008 07:09 PM
what does this awk script do? sharathkv25 Programming 3 03-08-2007 03:10 PM
Passing variables from AWK script to my shell script BigLarry Programming 1 06-12-2004 04:32 AM
sed or awk question - replace caps with small letters computera Linux - General 1 12-30-2003 04:39 AM


All times are GMT -5. The time now is 04:21 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration