Link to home
Start Free TrialLog in
Avatar of Jasmin shahrzad
Jasmin shahrzad

asked on

awk sed

I have a file with cloumns from tables
i want to create a file like this

select name ||','|| fname ||','|| id from test;

i write a awk command like this

gawk 'BEGIN{print "select"} {print ($1,"||","'\''",",","'\''","||" )} END{print "from test;"} ' cloumns.txt |paste -sd " " >test.sql

but i get a file look like this
select name || ' , ' || fname || ' , ' ||  id|| ' , ' ||
there is to much space between || and '
and the llast one is not be there.
Avatar of Bill Prew
Bill Prew

Can you provide a sample of the input file, and a matching sample of the output you want?

~bp
If I understand you, and there are three fields in columns.txt separated by spaces, then this produces the line you started with.

Save the awk script below and then run from a command line like this:

gawk -f EE28997856.awk columns.txt > sql.txt

EE28997856.awk
{
   printf("select %s ||','|| %s ||','|| %s from test;\n", $1, $2, $3)
}

Open in new window

~bp
Avatar of Jasmin shahrzad

ASKER

no no,
i have a one file with 100-200(column.txt) line only one word in one line (which means 1 variable in awk ($1))
what i want is output like this
select
$1||','||
$1||','||
...
...
from table_name
for ex.

select
name ||','||
fname ||','||
id ||','||
id2
from table_name
 
last variable should not have ||','|| after
Okay, try this AWK script then.  I don't see why you want the SQL concatenation operators in there since you are generating SQL, but I left that option in with a comment in front of it.  I added a version of that line without the pipes in case that works better.  Use whichever works best.

*EDIT* I see the last field can't have the comma trailing it, I'll add that in a second...

BEGIN {
   print("select")
}

{
#   printf("%s||','||\n", $1)
   printf("%s,\n", $1)
}

END {
   print("from test;\n")
}

Open in new window

~bp
perfect it's what i want. but what about the last line from column file. see what i get

DUP_ULT_BEN_ACC||','||
DUP_ULT_BEN_ADDR||','||
from test;
 the last one should not have ||','||  before from
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
perfect thank you
Welcome.

~bp