Help with formatting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with formatting
# 1  
Old 09-26-2014
Help with formatting

Hi, I am new to UNIX and need your help in formatting the below input command to the desire output

Input:
Code:
CREATE UNIQUE INDEX XPKTABLE1
 (
                       COL1,
                       COL2
 ) ON TABLE_NM;

Output:
Code:
COMMENT ON TABLE DB_NM.TABLE_NM AS 'PK=,COL1,COL2; ';


In the input above, there are two tab spaces before COL1 and COL2.
Appreciate your inputs.

Thanks,
V

Last edited by Don Cragun; 09-26-2014 at 11:48 PM.. Reason: Fix CODE tags.
# 2  
Old 09-27-2014
Is this a homework assignment?

It is awfully hard to tell from your sample data what the expected input file format is.
  1. Are there always exactly 5 lines in your input file?
  2. Are there always two elements to be placed in the "PK=" list in the output?
  3. Why is there a "," after the "PK=" in the output before "COL1"?
  4. You say: "there are two tab spaces before COL1 and COL2". What is a "tab space"? There aren't any tabs in your sample data. Is a "tab space" 11.5 space characters???
  5. Could "COL1" appear on the same line as the "("?
  6. Could "COL1,COL2" appear on the same line?
  7. Could there be a comma or a semicolon after "COL2" in your input file?
  8. What OS and shell are you using?
# 3  
Old 09-27-2014
Don,
Please find my answers here, I could have been more specific earlier. I am working on automating few scripts and as part of that, I need to have this.

1. No there could be less or more depending on columns between brackets. In this example, I have 2 columns and so 5 lines. If there are 5 columns, then there will be 8 lines. Line 1, Line 2 and last line will remain same except the table name in last line
2. No, the number of elements changes depends on the number of columns(COL1, COL2, COL3.....)
3. ',' is expected in the output
4. It is tab key. In the sample data, it didn't allow me to put tab key, so I used spaces. There are two tabs before COL1, COL2
5. No, COL1 will always appear in line 3
6. No, COL1, COL2(and even COL3 if there is one) will always be in separate lines
7. Yes there will be a comma if COL3 exists and so on. The last column(COLx) will not have anything
8. I am using ksh in linux

Thanks,
V
# 4  
Old 09-27-2014
Hello, you can try:
Code:
awk '/CREATE UNIQUE INDEX/,/)/ {
 if(/CREATE UNIQUE INDEX|\(/) {
  next
 }
 if (!/\)/){
  A=A $NF
 }
 else{
  X=$NF
  gsub(/TABLE_|;/,"",X)
  print "COMMENT ON TABLE DB_" X ".TABLE_" X " AS \047PK=," A "; \047;"
 }
}' file.txt

It work fine with your input example...

Regards.
# 5  
Old 09-27-2014
Thank you disedorgue. It worked the way I wanted.

But one last question. How can I pass a variable for DB_NM instead of hard coding every time?

I tried to set something like this before awk and below is the output.

Input:

Code:
export TBL_DB="DATABASE";

awk '/CREATE UNIQUE INDEX/,/)/ {
if(/CREATE UNIQUE INDEX|\(/) {
  next
}
if (!/\)/){
  A=A $NF
}
else{
  X=$NF
  gsub(/TABLE_|;/,"",X)
  print "COMMENT ON TABLE ${TBL_DB}." X " AS \047PK=," A "; \047;"
}
}' input_index.txt > cmnt1.txt

and the output I am getting is

Code:
COMMENT ON TABLE ${TBL_DB}.TABLE_NM AS 'PK=,COL1,COL2; ';


But I want the output to be something like this

Code:
COMMENT ON TABLE DATABASE.TABLE_NM AS 'PK=,COL1,COL2; ';

Appreciate your help.

Thanks,
V
# 6  
Old 09-27-2014
As (I simplified many part code):
Code:
TBL_DB="DATABASE"
awk -v VAL_DB="${TBL_DB}" '/CREATE UNIQUE INDEX/,/)/ {
if(/CREATE UNIQUE INDEX|\(/) {
  next
}
if (!/\)/){
  A=A $NF
}
else{
  X=$NF
  sub(/;/,"",X)
  print "COMMENT ON TABLE "VAL_DB"." X " AS \047PK=," A "; \047;"
}
}' input_index.txt > cmnt1.txt

Regards.
This User Gave Thanks to disedorgue For This Post:
# 7  
Old 09-27-2014
Thank you so much. You rock!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Formatting

Good day All, I have requirement where my input data looks like below ] Message5 Expecting Output as 04/MAR/2104 ||| 23:15:45 ||| servername ||| NOTIFICATION |||message1||||||userId|||||| Message5 I could not use space delimiter as in the messages I would be having them as... (2 Replies)
Discussion started by: Tomlight
2 Replies

2. Shell Programming and Scripting

Formatting Help

Hi Guys, i have report that runs every 10 min and send the report of failed jobs to my mail. Currently i am using a command like this to send mail. mailx -t -s "FAILURE JOBS IN HYDRA $temp_date" addressee@domain.com < temp_file5 But i am getting mail in this format ....... (4 Replies)
Discussion started by: gkrish
4 Replies

3. Shell Programming and Scripting

help formatting

I need to format a txt file and convert it in CSV. Any "future" column is separated by a newline. FROM: XS1 1.43294 0.0 XS2 1.21824 0.0 TO: XS1,XS2 (2 Replies)
Discussion started by: alfreale
2 Replies

4. Shell Programming and Scripting

formatting of df -k

Hello, I am developing a platform Independant tool that should work for all major unix flavors outlined in this forum(Solaris,Linux, AIX, HPUX, SCO,BSD) Therefore, in order to cover all types of user community, I have deliberately posted the same message on every forum. Please do not think... (9 Replies)
Discussion started by: darsh123
9 Replies

5. Shell Programming and Scripting

Formatting

Is there a way to make a 2 column output out of the following : 1 2 3 4 5 6 Output : 1 2 3 4 5 6 Thanks, Prasanna (3 Replies)
Discussion started by: prasanna1157
3 Replies

6. Shell Programming and Scripting

formatting

I have file with different columns for ex. contents of file "txt" NAME AGE MARKS HARRY 23 89 TOM 12 67 BOB 23 11 and you see its not formatted.Now, I need the file "txt" to be formatted like COLUMN1 COLUMN2 COLUMN3 NAME AGE ... (3 Replies)
Discussion started by: vijay_0209
3 Replies

7. Shell Programming and Scripting

Formatting

I have next part of script: for i in $LIST do echo "`date +"%H:%M:%S"` Converting $i ..."; mysql -uroot some -sABe "ALTER TABLE $i ENGINE=$ENGINE"; done I want to get following output formatting: "OK" must be one under another :) ... (3 Replies)
Discussion started by: mirusnet
3 Replies

8. UNIX for Dummies Questions & Answers

formatting

Hi Again Guys , Please i installed linux RH 6.1 on Toshiba , 10G , RAM=128 , 600 MHZ . After i installed linux i got many error messages , seems it was not installed correctly , also when i finished installation it did not ask me for the 2nd installation CD , and when i logged as root , i... (5 Replies)
Discussion started by: tamemi
5 Replies

9. UNIX for Dummies Questions & Answers

formatting

is it possible to format a powerbook g4 mac? like totally erase the HD then pop in the Mac OS cd and it will boot up an install like windows or any linux? (5 Replies)
Discussion started by: xeron
5 Replies

10. UNIX for Dummies Questions & Answers

formatting

I've been asking on IRC channels but no one answers me, I need to format my hard drive, normally it's just format c: but c doesn't exist, how do I format when I have linux mandrake installed. Please reply to this quickly, I'm kinda in a rush :( (1 Reply)
Discussion started by: darryll777
1 Replies
Login or Register to Ask a Question