Creating and exporting/importing variable with awk?
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have a file where every line is a new customers information, such as
Pop corn, pop@gmail.com, 50, 200
I am currently trying to see if it is possible to use a awk/gawk script
to assign each column to a variable and then pipe those variables to sed
to be substituted into a template? I would be running both awk/gawk and sed from within a bash script. What is a good implementation I could follow to get this done? I'm trying not to use grep
, because I'm trying to learn how to cohesively use awk/gawk, sed, and bash scripts together or perl
since I haven't learned it yet.
shell-script awk sed gawk
add a comment |Â
up vote
0
down vote
favorite
I have a file where every line is a new customers information, such as
Pop corn, pop@gmail.com, 50, 200
I am currently trying to see if it is possible to use a awk/gawk script
to assign each column to a variable and then pipe those variables to sed
to be substituted into a template? I would be running both awk/gawk and sed from within a bash script. What is a good implementation I could follow to get this done? I'm trying not to use grep
, because I'm trying to learn how to cohesively use awk/gawk, sed, and bash scripts together or perl
since I haven't learned it yet.
shell-script awk sed gawk
Do you want to assign each column to a shell variable or to anawk
variable?
â Satà  Katsura
Oct 2 '17 at 3:39
I thought I would go ahead at first at useawk
variables if it was possible (which I now know is), if it were not then I would have tried using variables declared with in the bash script and exporting them toawk
.
â below_avg_st
Oct 2 '17 at 3:43
Depending on the specifics, it may be that you don't needsed
, or that you don't need the shell variable to do your templating, or evenawk
. Since you only give minimal info about what it is you actually want to do ("substitute (something) into a template"), it's difficult to give a "good implementation" for anything other than the parsing of the file that you talk about at the start.
â Kusalananda
Oct 2 '17 at 5:25
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a file where every line is a new customers information, such as
Pop corn, pop@gmail.com, 50, 200
I am currently trying to see if it is possible to use a awk/gawk script
to assign each column to a variable and then pipe those variables to sed
to be substituted into a template? I would be running both awk/gawk and sed from within a bash script. What is a good implementation I could follow to get this done? I'm trying not to use grep
, because I'm trying to learn how to cohesively use awk/gawk, sed, and bash scripts together or perl
since I haven't learned it yet.
shell-script awk sed gawk
I have a file where every line is a new customers information, such as
Pop corn, pop@gmail.com, 50, 200
I am currently trying to see if it is possible to use a awk/gawk script
to assign each column to a variable and then pipe those variables to sed
to be substituted into a template? I would be running both awk/gawk and sed from within a bash script. What is a good implementation I could follow to get this done? I'm trying not to use grep
, because I'm trying to learn how to cohesively use awk/gawk, sed, and bash scripts together or perl
since I haven't learned it yet.
shell-script awk sed gawk
shell-script awk sed gawk
edited Oct 2 '17 at 5:17
ñÃÂsýù÷
15.7k92563
15.7k92563
asked Oct 2 '17 at 3:31
below_avg_st
84
84
Do you want to assign each column to a shell variable or to anawk
variable?
â Satà  Katsura
Oct 2 '17 at 3:39
I thought I would go ahead at first at useawk
variables if it was possible (which I now know is), if it were not then I would have tried using variables declared with in the bash script and exporting them toawk
.
â below_avg_st
Oct 2 '17 at 3:43
Depending on the specifics, it may be that you don't needsed
, or that you don't need the shell variable to do your templating, or evenawk
. Since you only give minimal info about what it is you actually want to do ("substitute (something) into a template"), it's difficult to give a "good implementation" for anything other than the parsing of the file that you talk about at the start.
â Kusalananda
Oct 2 '17 at 5:25
add a comment |Â
Do you want to assign each column to a shell variable or to anawk
variable?
â Satà  Katsura
Oct 2 '17 at 3:39
I thought I would go ahead at first at useawk
variables if it was possible (which I now know is), if it were not then I would have tried using variables declared with in the bash script and exporting them toawk
.
â below_avg_st
Oct 2 '17 at 3:43
Depending on the specifics, it may be that you don't needsed
, or that you don't need the shell variable to do your templating, or evenawk
. Since you only give minimal info about what it is you actually want to do ("substitute (something) into a template"), it's difficult to give a "good implementation" for anything other than the parsing of the file that you talk about at the start.
â Kusalananda
Oct 2 '17 at 5:25
Do you want to assign each column to a shell variable or to an
awk
variable?â Satà  Katsura
Oct 2 '17 at 3:39
Do you want to assign each column to a shell variable or to an
awk
variable?â Satà  Katsura
Oct 2 '17 at 3:39
I thought I would go ahead at first at use
awk
variables if it was possible (which I now know is), if it were not then I would have tried using variables declared with in the bash script and exporting them to awk
.â below_avg_st
Oct 2 '17 at 3:43
I thought I would go ahead at first at use
awk
variables if it was possible (which I now know is), if it were not then I would have tried using variables declared with in the bash script and exporting them to awk
.â below_avg_st
Oct 2 '17 at 3:43
Depending on the specifics, it may be that you don't need
sed
, or that you don't need the shell variable to do your templating, or even awk
. Since you only give minimal info about what it is you actually want to do ("substitute (something) into a template"), it's difficult to give a "good implementation" for anything other than the parsing of the file that you talk about at the start.â Kusalananda
Oct 2 '17 at 5:25
Depending on the specifics, it may be that you don't need
sed
, or that you don't need the shell variable to do your templating, or even awk
. Since you only give minimal info about what it is you actually want to do ("substitute (something) into a template"), it's difficult to give a "good implementation" for anything other than the parsing of the file that you talk about at the start.â Kusalananda
Oct 2 '17 at 5:25
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
It seems to me you should be better off doing something like:
while IFS=, read -r name email x y _ignored_; do
email=$email# x=$x# y=$y#
sendmail -oem -oi -t << EOF
To: $name <$email>
Subject: Whatever
x=$x y=$y
EOF
done < file.csv
That, is since you're going to run at least one command per line anyway, use the shell to read the lines into variables. And use shell variable expansion in the here-document instead of sed
to do the template expansion.
awk
wouldn't help much here as while it can run commands with its system()
or getline()
(but via running a shell to parse a command line), it cannot set environment variables for those, other than building the shell code that sets those variables. So, it would become quite convoluted and less efficient. Something like:
CODE='sendmail -oem -oi -t << EOF
to: $name <$email>
Subject: Whatever
x=$x y=$y
EOF' awk -F ', *' -v q="'" '
function shquote(s)
gsub(q, q "\" q q, s)
return q s q
system("name="shquote($1)" email="shquote($2)" x="shquote($3)
" y="shquote($4)"n"ENVIRON["CODE"])' < file.csv
add a comment |Â
up vote
0
down vote
Use split
function of awk
to read the line into the array with specific delimiter.
awk 'split($0, array, /,/)' customers_file
Then you can pass to sed
what elements of this array you want to feed it with just printing those elements within awk
.
is this pearl? or issplit
also an implementation in bash/awk?
â below_avg_st
Oct 2 '17 at 4:33
Hi, this isawk
andsplit
is its built in function.
â Ã±ÃÂsýù÷
Oct 2 '17 at 4:38
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
It seems to me you should be better off doing something like:
while IFS=, read -r name email x y _ignored_; do
email=$email# x=$x# y=$y#
sendmail -oem -oi -t << EOF
To: $name <$email>
Subject: Whatever
x=$x y=$y
EOF
done < file.csv
That, is since you're going to run at least one command per line anyway, use the shell to read the lines into variables. And use shell variable expansion in the here-document instead of sed
to do the template expansion.
awk
wouldn't help much here as while it can run commands with its system()
or getline()
(but via running a shell to parse a command line), it cannot set environment variables for those, other than building the shell code that sets those variables. So, it would become quite convoluted and less efficient. Something like:
CODE='sendmail -oem -oi -t << EOF
to: $name <$email>
Subject: Whatever
x=$x y=$y
EOF' awk -F ', *' -v q="'" '
function shquote(s)
gsub(q, q "\" q q, s)
return q s q
system("name="shquote($1)" email="shquote($2)" x="shquote($3)
" y="shquote($4)"n"ENVIRON["CODE"])' < file.csv
add a comment |Â
up vote
2
down vote
It seems to me you should be better off doing something like:
while IFS=, read -r name email x y _ignored_; do
email=$email# x=$x# y=$y#
sendmail -oem -oi -t << EOF
To: $name <$email>
Subject: Whatever
x=$x y=$y
EOF
done < file.csv
That, is since you're going to run at least one command per line anyway, use the shell to read the lines into variables. And use shell variable expansion in the here-document instead of sed
to do the template expansion.
awk
wouldn't help much here as while it can run commands with its system()
or getline()
(but via running a shell to parse a command line), it cannot set environment variables for those, other than building the shell code that sets those variables. So, it would become quite convoluted and less efficient. Something like:
CODE='sendmail -oem -oi -t << EOF
to: $name <$email>
Subject: Whatever
x=$x y=$y
EOF' awk -F ', *' -v q="'" '
function shquote(s)
gsub(q, q "\" q q, s)
return q s q
system("name="shquote($1)" email="shquote($2)" x="shquote($3)
" y="shquote($4)"n"ENVIRON["CODE"])' < file.csv
add a comment |Â
up vote
2
down vote
up vote
2
down vote
It seems to me you should be better off doing something like:
while IFS=, read -r name email x y _ignored_; do
email=$email# x=$x# y=$y#
sendmail -oem -oi -t << EOF
To: $name <$email>
Subject: Whatever
x=$x y=$y
EOF
done < file.csv
That, is since you're going to run at least one command per line anyway, use the shell to read the lines into variables. And use shell variable expansion in the here-document instead of sed
to do the template expansion.
awk
wouldn't help much here as while it can run commands with its system()
or getline()
(but via running a shell to parse a command line), it cannot set environment variables for those, other than building the shell code that sets those variables. So, it would become quite convoluted and less efficient. Something like:
CODE='sendmail -oem -oi -t << EOF
to: $name <$email>
Subject: Whatever
x=$x y=$y
EOF' awk -F ', *' -v q="'" '
function shquote(s)
gsub(q, q "\" q q, s)
return q s q
system("name="shquote($1)" email="shquote($2)" x="shquote($3)
" y="shquote($4)"n"ENVIRON["CODE"])' < file.csv
It seems to me you should be better off doing something like:
while IFS=, read -r name email x y _ignored_; do
email=$email# x=$x# y=$y#
sendmail -oem -oi -t << EOF
To: $name <$email>
Subject: Whatever
x=$x y=$y
EOF
done < file.csv
That, is since you're going to run at least one command per line anyway, use the shell to read the lines into variables. And use shell variable expansion in the here-document instead of sed
to do the template expansion.
awk
wouldn't help much here as while it can run commands with its system()
or getline()
(but via running a shell to parse a command line), it cannot set environment variables for those, other than building the shell code that sets those variables. So, it would become quite convoluted and less efficient. Something like:
CODE='sendmail -oem -oi -t << EOF
to: $name <$email>
Subject: Whatever
x=$x y=$y
EOF' awk -F ', *' -v q="'" '
function shquote(s)
gsub(q, q "\" q q, s)
return q s q
system("name="shquote($1)" email="shquote($2)" x="shquote($3)
" y="shquote($4)"n"ENVIRON["CODE"])' < file.csv
edited Oct 2 '17 at 9:40
answered Oct 2 '17 at 9:02
Stéphane Chazelas
283k53522859
283k53522859
add a comment |Â
add a comment |Â
up vote
0
down vote
Use split
function of awk
to read the line into the array with specific delimiter.
awk 'split($0, array, /,/)' customers_file
Then you can pass to sed
what elements of this array you want to feed it with just printing those elements within awk
.
is this pearl? or issplit
also an implementation in bash/awk?
â below_avg_st
Oct 2 '17 at 4:33
Hi, this isawk
andsplit
is its built in function.
â Ã±ÃÂsýù÷
Oct 2 '17 at 4:38
add a comment |Â
up vote
0
down vote
Use split
function of awk
to read the line into the array with specific delimiter.
awk 'split($0, array, /,/)' customers_file
Then you can pass to sed
what elements of this array you want to feed it with just printing those elements within awk
.
is this pearl? or issplit
also an implementation in bash/awk?
â below_avg_st
Oct 2 '17 at 4:33
Hi, this isawk
andsplit
is its built in function.
â Ã±ÃÂsýù÷
Oct 2 '17 at 4:38
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Use split
function of awk
to read the line into the array with specific delimiter.
awk 'split($0, array, /,/)' customers_file
Then you can pass to sed
what elements of this array you want to feed it with just printing those elements within awk
.
Use split
function of awk
to read the line into the array with specific delimiter.
awk 'split($0, array, /,/)' customers_file
Then you can pass to sed
what elements of this array you want to feed it with just printing those elements within awk
.
edited Oct 2 '17 at 7:17
answered Oct 2 '17 at 3:36
ñÃÂsýù÷
15.7k92563
15.7k92563
is this pearl? or issplit
also an implementation in bash/awk?
â below_avg_st
Oct 2 '17 at 4:33
Hi, this isawk
andsplit
is its built in function.
â Ã±ÃÂsýù÷
Oct 2 '17 at 4:38
add a comment |Â
is this pearl? or issplit
also an implementation in bash/awk?
â below_avg_st
Oct 2 '17 at 4:33
Hi, this isawk
andsplit
is its built in function.
â Ã±ÃÂsýù÷
Oct 2 '17 at 4:38
is this pearl? or is
split
also an implementation in bash/awk?â below_avg_st
Oct 2 '17 at 4:33
is this pearl? or is
split
also an implementation in bash/awk?â below_avg_st
Oct 2 '17 at 4:33
Hi, this is
awk
and split
is its built in function.â Ã±ÃÂsýù÷
Oct 2 '17 at 4:38
Hi, this is
awk
and split
is its built in function.â Ã±ÃÂsýù÷
Oct 2 '17 at 4:38
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f395551%2fcreating-and-exporting-importing-variable-with-awk%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Do you want to assign each column to a shell variable or to an
awk
variable?â Satà  Katsura
Oct 2 '17 at 3:39
I thought I would go ahead at first at use
awk
variables if it was possible (which I now know is), if it were not then I would have tried using variables declared with in the bash script and exporting them toawk
.â below_avg_st
Oct 2 '17 at 3:43
Depending on the specifics, it may be that you don't need
sed
, or that you don't need the shell variable to do your templating, or evenawk
. Since you only give minimal info about what it is you actually want to do ("substitute (something) into a template"), it's difficult to give a "good implementation" for anything other than the parsing of the file that you talk about at the start.â Kusalananda
Oct 2 '17 at 5:25