awk error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk error
# 1  
Old 01-25-2011
awk error

Hi Team,

I have .csv file in the following format
.csv file
Code:
TAB1;COL1;DATATYPE;NOTNULL;WITH DEFAULT
TAB2;COL1;DATATYPE;NOTNULL;WITH DEFAULT
....
....
....

output:
Code:
ALTER TABLE TAB1. add COL1 DATATYPE NOTNULL WITH DEFAULT;
ALTER TABLE TAB2  add COL1 DATATYPE NOTNULL WITH DEFAULT;

I started writing like this but could not able get desired out put.
Code:
#Read input
cat test.csv|while read line
do
  TABLENAME=`cut -d';' -f1`
  COLNAME=`cut -d';' -f2`
  DATATYPE=`cut -d';' -f3`
  COMMAND="ALTER TABLE LS77DB.TLPR${TABLENAME} ${COLNAME} ${DATATYPE}"
done

can some body suggest where to adjust the code.
?

Last edited by Franklin52; 01-25-2011 at 08:43 AM.. Reason: Please use code tags
# 2  
Old 01-25-2011
This should be sufficient:
Code:
awk -F; '{$1="ALTER TABLE " $1; $2="add " $2; $NF=$NF ";"}1' test.csv


Last edited by Franklin52; 01-25-2011 at 08:56 AM.. Reason: Adding "add " to column $2
# 3  
Old 01-25-2011
What error/output do you get and where do you even call awk in that script?
# 4  
Old 01-25-2011
Hello Pludi,

I tried echo $COMMAND where i already defined the command.
I didn't get any error but output is not 100%.The output is coming like

ALTER TAB1.COL1 TAB2 TAB3 TAB4 ..etc.

That means its just considering only first field ie $TABLENAME

Regards

---------- Post updated at 08:04 AM ---------- Previous update was at 07:57 AM ----------

Hello Franklin,
Thanks for your inputs.But that command is not working for me.Its giving error when i execute from my end.
Code:
$ awk -F ; ' { $1="ALTER TABLE " $1; $2="add " $2; $NF=$NF ";" } ' test.csv
awk: A flag requires a parameter: F
Usage: awk [-F Character][-v Variable=Value][-f File|Commands][Variable=Value|File ...]

Regards

Last edited by pludi; 01-25-2011 at 09:12 AM..
# 5  
Old 01-25-2011
Try to escape the fieldseparator like this and don't forget the last "1":
Code:
awk -F\; '{$1="ALTER TABLE " $1; $2="add " $2; $NF=$NF ";"}1' test.csv

# 6  
Old 01-25-2011
Yes franklin, Its working for me .Out put is coming like this
Code:
ALTER TABLE TAB1 add COL1 CHAR 2 No YES;
ALTER TABLE TAB1 add COL1 DOUBLE 7 2 No YES;

one more requirement to extend the above output to

- if DATATYPE is CHAR OR DOUBLE out put should be
Code:
ALTER TABLE TAB1 add COL1 CHAR(2) No YES;
ALTER TABLE TAB1 add COL1 DOUBLE(7 2) No YES;

- In place NO NOTNULL should come in place of YES WITHDEFAULT should come.

Please don't have feel this is like home work.Help me out .


Moderator's Comments:
Mod Comment Please use code tags when posting data and code samples!

Last edited by rocking77; 01-25-2011 at 09:29 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Error in awk...

Hi friends... (Apoloigies for any typos.) (Don, thanks for your input.) Consider these two code snippets:- awkerror1.awk #!/usr/bin/awk -f BEGIN \ { SAWTOOTHPLUS64 = " !#$&\'\)*,-/0235689:<>?ABDEGHJKMNPQSTVWYZ\\^_abdeghjkmnpqstvwyz|}~" for(LOOP = 1; LOOP <= 13; ++LOOP) {... (3 Replies)
Discussion started by: wisecracker
3 Replies

2. Shell Programming and Scripting

awk output yields error: awk:can't open job_name (Autosys)

Good evening, Im newbie at unix specially with awk From an scheduler program called Autosys i want to extract some data reading an inputfile that comprises jobs names, then formating the output to columns for example 1. This is the inputfile: $ more MapaRep.txt ds_extra_nikira_usuarios... (18 Replies)
Discussion started by: alexcol
18 Replies

3. Shell Programming and Scripting

Error with awk

i have been trying to run the following bash script, but get error: awk: cmd. line:1: - awk: cmd. line:1: ^ unexpected newline or end of string I have been trying to figure out what the problem is but to no avail. I need to run the script urgently for a project, so any help will be highly... (5 Replies)
Discussion started by: anti_antaeus
5 Replies

4. Shell Programming and Scripting

awk Error

Hi I am trying to create a file with count of lines and string from shell variable, i am getting the error incorrect syntax. Below is the code : wc -l $filename | awk '{ print $1"|"$2"|"${SOURCETYPE} }'>>$AUDITFILENAME Could someone help me on this? (7 Replies)
Discussion started by: cnrj
7 Replies

5. Shell Programming and Scripting

awk command in script gives error while same awk command at prompt runs fine: Why?

Hello all, Here is what my bash script does: sums number columns, saves the tot in new column, outputs if tot >= threshold val: > cat getnon0file.sh #!/bin/bash this="getnon0file.sh" USAGE=$this" InFile="xyz.38" Min="0.05" # awk '{sum=0; for(n=2; n<=NF; n++){sum+=$n};... (4 Replies)
Discussion started by: catalys
4 Replies

6. Shell Programming and Scripting

AWK error

This is my disk space on solaris box and i wrote a simple script which identifies mount points which crosses 90% or above. But the AWK part is throwing some error and im unable to debug.. mzaheer @ UAT 144 % => df -k Filesystem kbytes used avail capacity Mounted on... (5 Replies)
Discussion started by: user__user3110
5 Replies

7. Shell Programming and Scripting

Awk error -- awk: 0602-562 Field $() is not correct.

typeset -i i=1 while read -r filename; do Splitfile=`$Targetfile_$i.txt` awk 'substr($0,1,5) == substr($filename,1,5) && substr($0,526,2) == substr($filename,6,2) && substr($0,750,12) == substr($filename,8,12)' $SourceFilename >> $Splitfile i=i+1 done < /tmp/list.out I am using this logic... (1 Reply)
Discussion started by: pukars4u
1 Replies

8. UNIX for Dummies Questions & Answers

awk error

Hi all, i have the files in the below sequence: fancy_LANG_STD_AU_2008-03-05.dat fancy_LANG_STD_HK_2008-03-06.dat fancy_LANG_STD_NZ_2008-03-05.dat fancy_STD_AU_2008-03-05.dat fancy_STD_HK_2008-03-06.dat fancy_STD_NZ_2008-03-05.dat i am trying to sort them like below: ... (3 Replies)
Discussion started by: gyankr
3 Replies

9. UNIX for Dummies Questions & Answers

awk Shell Script error : "Syntax Error : `Split' unexpected

hi there i write one awk script file in shell programing the code is related to dd/mm/yy to month, day year format but i get an error please can anybody help me out in this problem ?????? i give my code here including error awk ` # date-month -- convert mm/dd/yy to month day,... (2 Replies)
Discussion started by: Herry
2 Replies

10. Shell Programming and Scripting

Error in awk

var1=`echo "emp,dept,salgrade" | awk -F, '{print NF}'` count=1 while ; do i=`expr $count` tname=`echo "emp,dept,salgrade" | awk -F, '{ print $(echo $i) }'` count=$count+1; echo ${tname}; echo $count done I want to store in tname=emp, tname=dept,tname=salgrade I am getting... (2 Replies)
Discussion started by: dreams5617
2 Replies
Login or Register to Ask a Question