Using awk and grep for sql generation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using awk and grep for sql generation
# 8  
Old 10-18-2016
If it makes any difference, here is a commented version of my code:
Code:
awk -F "[ |]+" '	# Start awk program with input field separator set to
			# any sequence of one or more adjacent <space> or
			# <vertical-line> characters.
BEGIN {	# Before processeing any input file lines, set last output field format
	# strings...
	fmt[0] = "%s%s.%s=%s.%s"	 # Format for non-string field values.
	fmt[1] = "%sTRIM(%s.%s)=TRIM(%s.%s)" # Format for string field values.
}
FNR == NR {
	# Process lines from 1st input file (file record number is the same as
	# the all record number).
	if($3 == "string")	# If the field type is string...
		trim[$1, $2]	# ... add the table and field names as indices
				# in the trim[] array.
	next	# Skip the remaining steps in this awk script for this record.
}
FNR > 1 {
	# Process lines in any remaining input file, skipping the header line.
	# Print output table field.
	printf("%s|", $1)
	# Split the list of fields to be processed in this table into the F[]
	# array.
	flds = split($2, F, ",")
	# For each field found...
	for(i = 1; i <= flds; i++)
		# ... print the data for this field using the proper format
		# (fmt[0] if this field in this table is not a string; fmt[1] if
		# this field in this table is a string).
		printf(fmt[(($1, F[i]) in trim)], (i > 1) ? " AND " : "", S,
		    F[i], D, F[i])
	# Add an output line terminating <newline> character.
	print ""
}' S=t1 D=t2 desc.txt pk.txt	# End awk program, set souce database name (S)
				# and destination database name (D), and name
				# the two input files.

Note that this is very similar to Chubler_XL's code (which was closely based on your code), with the following differences:
  • Instead of using the T[] array to store that field type for all of the table and field values, this code uses the trim[] array to just keep a list of table and field values that are of type string. (This slightly reduces the amount of storage needed and makes the test easier when we are checking the field type to determine which format string to use.)
  • This code uses an array of format strings instead of reassigning the format string each time through the field conversion printing loop.
  • Your code used the table and field names and concatenated <vertical-line> characters as the printf format string. If there is ever any chance that any of the strings you will be printing could contain a <percent-sign> or <backslash> character, you won't get the output you want. Although that is unlikely in this case, I find it safer to make an explicit format string that will correctly print user supplied data even if it contains characters that have special meaning in printf format strings.
  • Your code looped the printing the last output field subfields with an index starting at zero and continuing through the number of elements in the F[] array minus one and references F[i + 1]. This code loops with an index starting at one and continuing through the number of elements in the F[] array and references F[i]. Both do the same thing, I just find the way I did it easier to read.
If you want the output to have three fields instead of just the two the above code creates, change the code in red above:
Code:
	# Print output table field.
	printf("%s|", $1)

to:
Code:
	# Print output table and fields field.
	printf("%s|%s|", $1, $2)

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Finding The Complete SQL statement Using PDFGREP Or Grep

Linux Gods, I am simply attempting to parse SQL statements from a PDF doc in creating a base SQL script at a later time but for the life of me, am having a tough time extracting this data.This exact string worked perfectly a couple of months ago and now it doesnt. Below is an example of the data... (4 Replies)
Discussion started by: metallica1973
4 Replies

2. Shell Programming and Scripting

awk concatenation issue - SQL generation

Greetings Experts, I have an excel file and I am unable to read it directly into awk (contains , " etc); So, I cleansed and copied the data into notepad. I need to generate a script that generates the SQL. Requirement: 1. Filter and select only the data that has the "mapping" as "direct"... (4 Replies)
Discussion started by: chill3chee
4 Replies

3. UNIX for Dummies Questions & Answers

Grep SQL output file for greater than number.

Hi, This is my first post. I have a korn shell script which outputs a select statment to a file. There is only one column and one row which contains a record count of the select statement. The select statement looks something like this: SELECT COUNT(some_field) AS "count_value" ... (2 Replies)
Discussion started by: MurdocUK
2 Replies

4. Programming

[ask]SQL command act like sort and grep

for example, I have a text file in random content inside, maybe something like this. 234234 54654 123134 467456 24234234 7867867 23424 568567if I run this command cat "filename.txt" | sort -n | grep "^467456$" -A 1 -B 1the result is 234234 467456 568567is it possible to do this command... (2 Replies)
Discussion started by: 14th
2 Replies

5. Shell Programming and Scripting

How to grep the where clause of a SQL?

Hi UNIX Gurus, I want to use extract the where clause of a SQL present in a file. Please suggest me how can I do it. Select * from emp where emp_id>10; cat <file_name> | grep -i "where" returns whole SQL. how can I extract only "where emp_id>10;" Thanks in advance (4 Replies)
Discussion started by: ustechie
4 Replies

6. Shell Programming and Scripting

Random word generation with AWK

Hi - I have a word GTTCAGAGTTCTACAGTCCGACGAT I need to extract all the possible "chunks" of 7 or above letter "words" from this. SO, my out put should be GTTCAGA TTCAGAG TCAGAGT CAGAGTTCT TCCGACGAT CAGTCCGACG etc. How can I do that with awk or any other language? I have no... (2 Replies)
Discussion started by: polsum
2 Replies

7. Shell Programming and Scripting

Report Generation with Grep

All, I am pretty new to Unix Environment. I am not sure if my requirement can be accomplished in Unix. I did try searching this forum and others but could not get an answer. Requirement is explained below: I have a set of files in a folder. file1_unload file2_unload file3_unload... (7 Replies)
Discussion started by: bharath.gct
7 Replies

8. Shell Programming and Scripting

Dynamic command line generation with awk

Hi, I'm not an expert in awk but i need a simple script to do this: I'd like to AutoCrop PDF files. I found 2 simple script that combined together could help me to automatize :) The first utiliti is "pdfinfo" that it gives the MediaBox and TrimBox values from the pdf. The pdfinfo output... (8 Replies)
Discussion started by: gbagagli
8 Replies

9. Shell Programming and Scripting

awk- report generation from input file

I have input file with below content: Person: Name: Firstname1 lastname1 Address: 111, Straat City : Hilversum Person: Name : Fistname2 lastname2 Address: 222, street Cit: Bussum Person: Name : Firstname2 lastname3 Address: 333, station straat City: Amsterdam I need... (6 Replies)
Discussion started by: McLan
6 Replies

10. UNIX for Dummies Questions & Answers

SQL Loader Auto Number Generation

Hi all, I have a doubt in SQL Loader. We have SEQUENCE function in SQL Loader or can create Sequence in Oracle database for generating a number sequence for a column while loading data using SQL Loader into table or multiple tables. My requirment is this. For the first run in SQL... (2 Replies)
Discussion started by: vinoth_kumar
2 Replies
Login or Register to Ask a Question