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.
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,"||","'\''",",","'\''"
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.
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
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)
}
~bp
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
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...
*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")
}
~bp
ASKER
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
DUP_ULT_BEN_ACC||','||
DUP_ULT_BEN_ADDR||','||
from test;
the last one should not have ||','|| before from
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
perfect thank you
Welcome.
~bp
~bp
~bp