format text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting format text
# 1  
Old 01-31-2011
format text

input file test.csv has following structure.

Code:
SCHEM1.TAB1;COL1;DATATYPE;NOTNULL--
SCHEMA1.TAB1;COL2;DATATYPE;NOTNULL;WITH DEFAULT;--
....
SCHEMA1.TABn;COL1;DATATYPE;NOTNULL;WITH DEFAULT;


i started using awk but text is not tab seperated.its just ; seperated.
how to print

output should be:
Code:
ALTER TABLE SCHEMA1.TAB1 add COL1 DATATYPE NOTNULL;
ALTER TABLE SCHEMA1.TAB1 add COL2 DATATYPE NOTNULL WITHDEFAULT;
....

ALTER TABLE SCHEMA1.TABn add COL DATATYPE NOTNULL  WITHDEFAULT;

# 2  
Old 01-31-2011
That is no problem for awk since you can specify a different, even multiple and reg exp as delimeters. Try:
Code:
awk -F";" '......

# 3  
Old 01-31-2011
Code:
#!/bin/ksh
INPUTFILE=InputFile.csv
while read inputline
do
column1="$(echo $inputline | cut -d';' -f1)"
echo column1 = $column1 ;
 
column2="$(echo $inputline | cut -d';' -f2)"
echo column2 = $column2 ;
 
column3="$(echo $inputline | cut -d';' -f3)"
echo column3 = $column3 ;
 
column4="$(echo $inputline | cut -d';' -f4)"
echo column4 = $column4 ;
 
column5="$(echo $inputline | cut -d';' -f5)"
echo column5 = $column5 ;
 
db2 "ALTER TABLE ${column1} add ${column2} DATATYPE ${column3} ${column4} ${column5}"
 
done < $INPUTFILE


If You want to use 'awk' ::

Code:
column1=`echo "$inputline" | awk -F';' '{print $1}'`
column2=`echo "$inputline" | awk -F';' '{print $2}'`
column3=`echo "$inputline" | awk -F';' '{print $3}'`
column4=`echo "$inputline" | awk -F';' '{print $4}'`
column5=`echo "$inputline" | awk -F';' '{print $5}'`

Keep on incrementing the inputline number , so that the command is executed once for every input line.

Last edited by Franklin52; 01-31-2011 at 09:16 AM.. Reason: Please use code tags, thank you
# 4  
Old 07-21-2011
Can try with sed:

Code:
$ cat temp.txt
SCHEM1.TAB1;COL1;DATATYPE;NOTNULL--
SCHEMA1.TAB1;COL2;DATATYPE;NOTNULL;WITH DEFAULT;--
....
SCHEMA1.TABn;COL1;DATATYPE;NOTNULL;WITH DEFAULT;


Code:
$ sed -e 's/\(.*\)/\ALTER TABLE \1/' -e 's/;/ /g' -e 's/--/;/' temp.txt
ALTER TABLE SCHEM1.TAB1 COL1 DATATYPE NOTNULL;
ALTER TABLE SCHEMA1.TAB1 COL2 DATATYPE NOTNULL WITH DEFAULT ;
ALTER TABLE ....
ALTER TABLE SCHEMA1.TABn COL1 DATATYPE NOTNULL WITH DEFAULT


Last edited by radoulov; 07-21-2011 at 10:08 AM.. Reason: Code tags.
# 5  
Old 07-21-2011
Or try..
Code:
sed 's/\([^; ]*\) \([^; ]*\)/\1\2/g;s/[;-]/ /g;s/^\([^ ]*\) \(.*\)/ALTER TABLE \1 add \2;/' inputfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Format text output

Hello, i've got a script based on queries which generates this output into a file cat cron_S-PostGres-ncc_license_Simpel_ActSub_per_Product 201502-b31s31i10 | MVNO | 114751 | 2017-07-19 201502-b31s31i10R60-60 | MVNO | 62115 | 2017-07-19 201511-b31s31i15 ... (5 Replies)
Discussion started by: nms
5 Replies

2. Shell Programming and Scripting

Need script for transferring bulk files from one format to text format

"Help Me" Need script for transferring bulk files from one format to text format in a unix server. Please suggest (2 Replies)
Discussion started by: Kranthi Kumar
2 Replies

3. Shell Programming and Scripting

Text Format

I'd like to format a text in unix before mailing it to users. I want to change color and font of my plain text in unix. Thanks.. (1 Reply)
Discussion started by: Sara_84
1 Replies

4. Shell Programming and Scripting

text format

Hi all I need you help to create script get the below output Thanks in advance. Ashan name.txt Wymouth_MRI_Lun79 PACS DR TEMP R drive stat.txt NO Normal Normal Out need Wymouth_MRI_Lun79 NO Normal PACS DR TEMP R drive Normal (1 Reply)
Discussion started by: ashanabey
1 Replies

5. Shell Programming and Scripting

Converting text to certain format

Hi Forum. I'm trying to convert the following text using "sed" or "awk" or "tr" but with no luck. From: EDW_WHOLESALE_VORTEX To: __ Any help is greatly appreciated. (9 Replies)
Discussion started by: pchang
9 Replies

6. Shell Programming and Scripting

format text file

i have a text file in this format: name1^A1^B1^ name2^A2^B2^ ... namex^Ax^Bx^ name1^AA1^ name2^AA2^ ... namex^AAx^ name1^AAA1^BBB1^ name2^AAA1^BBB2^ ... namex^AAAx^BBBx^ name1^AAAA1^ name2^AAAA2^ ... namex^AAAAx^ i want to generate a file: (2 Replies)
Discussion started by: busystock
2 Replies

7. Shell Programming and Scripting

using awk to format text

I'm new to awk and would appreciate a jump start. I've got a text doc of people with first and last names, ages, home cities, and a phrase about the individual. I want to parse the text into fields and rows separated by tabs with the field names of - Firstname, Lastname, Age, City, Dollar... (5 Replies)
Discussion started by: jkandel
5 Replies

8. Shell Programming and Scripting

Format a text file

I have a file that gets created by pasting 3 files together to get the one file. The output of the 3 files follows. sft.rtf c3_critappdb_u5 start 04:22:20 end 08:03:41 c3_critappdb_u6 start 18:01:31 end 20:21:19 c3_critappdb_u7 start 00:02:50 end 08:30:17 c3_critappdb_u8 start 17:00:42 end... (2 Replies)
Discussion started by: jhardy
2 Replies

9. UNIX for Dummies Questions & Answers

Format a column of text

Hi I have a LONG ! column of text and I would like to format this into several columns. so from this abcd efgh ijkl mnop qrst uvwx yz12 to this abcd efgh ijkl mnop qrstu vwx yz12 (2 Replies)
Discussion started by: mlucas
2 Replies

10. Shell Programming and Scripting

Set format to text

Hello, Im using a terminal that can configure colors and backgrounds for determinate text with format, but.... how i can put format to some text? For example, i use: echo "Hello my friend" but i want as output Hello my friend the colors will be configured in the terminal but how i... (3 Replies)
Discussion started by: Lestat
3 Replies
Login or Register to Ask a Question