.cfg file to .awk file
Clash Royale CLAN TAG#URR8PPP
My color.cfg file from where I am reading a file is as below.
marks,<,80,green
marks,>,80,yellow
I want to convert above color.cfg file into color.awk as below
function check()
if ( marks < 80 )
return "green"
if (marks > 800)
return "yellow"
I have a shell script like below to convert .cfg to .awk.
LINECT=0
while read LINE
do
var1[$LINECT]=$(echo $LINE | cut -d ',' -f1)
var2[$LINECT]=$(echo $LINE | cut -d ',' -f2)
var3[$LINECT]=$(echo $LINE | cut -d ',' -f3)
var4[$LINECT]=$(echo $LINE | cut -d ',' -f4)
LINECT=$((LINECT+1))
done < color.cfg
echo $var1[@]
echo $var2[@]
echo $var3[@]
echo $var4[@]
echo $LINECT
cat <<EOF > color.awk
function check()
if ($var1[0] $var2[0] $var3[0] )
return "$var4[0]"
EOF
My problem is how to write repetitive “if blocks” while writing a color.awk
I have written “if block once but how to write if have 2 or 3 or 4 condition ?
cat <<EOF > color.awk
function check()
if ($var1[0] $var2[0] $var3[0] )
return "$var4[0]"
EOF
shell-script awk
add a comment |
My color.cfg file from where I am reading a file is as below.
marks,<,80,green
marks,>,80,yellow
I want to convert above color.cfg file into color.awk as below
function check()
if ( marks < 80 )
return "green"
if (marks > 800)
return "yellow"
I have a shell script like below to convert .cfg to .awk.
LINECT=0
while read LINE
do
var1[$LINECT]=$(echo $LINE | cut -d ',' -f1)
var2[$LINECT]=$(echo $LINE | cut -d ',' -f2)
var3[$LINECT]=$(echo $LINE | cut -d ',' -f3)
var4[$LINECT]=$(echo $LINE | cut -d ',' -f4)
LINECT=$((LINECT+1))
done < color.cfg
echo $var1[@]
echo $var2[@]
echo $var3[@]
echo $var4[@]
echo $LINECT
cat <<EOF > color.awk
function check()
if ($var1[0] $var2[0] $var3[0] )
return "$var4[0]"
EOF
My problem is how to write repetitive “if blocks” while writing a color.awk
I have written “if block once but how to write if have 2 or 3 or 4 condition ?
cat <<EOF > color.awk
function check()
if ($var1[0] $var2[0] $var3[0] )
return "$var4[0]"
EOF
shell-script awk
add a comment |
My color.cfg file from where I am reading a file is as below.
marks,<,80,green
marks,>,80,yellow
I want to convert above color.cfg file into color.awk as below
function check()
if ( marks < 80 )
return "green"
if (marks > 800)
return "yellow"
I have a shell script like below to convert .cfg to .awk.
LINECT=0
while read LINE
do
var1[$LINECT]=$(echo $LINE | cut -d ',' -f1)
var2[$LINECT]=$(echo $LINE | cut -d ',' -f2)
var3[$LINECT]=$(echo $LINE | cut -d ',' -f3)
var4[$LINECT]=$(echo $LINE | cut -d ',' -f4)
LINECT=$((LINECT+1))
done < color.cfg
echo $var1[@]
echo $var2[@]
echo $var3[@]
echo $var4[@]
echo $LINECT
cat <<EOF > color.awk
function check()
if ($var1[0] $var2[0] $var3[0] )
return "$var4[0]"
EOF
My problem is how to write repetitive “if blocks” while writing a color.awk
I have written “if block once but how to write if have 2 or 3 or 4 condition ?
cat <<EOF > color.awk
function check()
if ($var1[0] $var2[0] $var3[0] )
return "$var4[0]"
EOF
shell-script awk
My color.cfg file from where I am reading a file is as below.
marks,<,80,green
marks,>,80,yellow
I want to convert above color.cfg file into color.awk as below
function check()
if ( marks < 80 )
return "green"
if (marks > 800)
return "yellow"
I have a shell script like below to convert .cfg to .awk.
LINECT=0
while read LINE
do
var1[$LINECT]=$(echo $LINE | cut -d ',' -f1)
var2[$LINECT]=$(echo $LINE | cut -d ',' -f2)
var3[$LINECT]=$(echo $LINE | cut -d ',' -f3)
var4[$LINECT]=$(echo $LINE | cut -d ',' -f4)
LINECT=$((LINECT+1))
done < color.cfg
echo $var1[@]
echo $var2[@]
echo $var3[@]
echo $var4[@]
echo $LINECT
cat <<EOF > color.awk
function check()
if ($var1[0] $var2[0] $var3[0] )
return "$var4[0]"
EOF
My problem is how to write repetitive “if blocks” while writing a color.awk
I have written “if block once but how to write if have 2 or 3 or 4 condition ?
cat <<EOF > color.awk
function check()
if ($var1[0] $var2[0] $var3[0] )
return "$var4[0]"
EOF
shell-script awk
shell-script awk
edited Feb 19 at 17:15
Kusalananda
135k17255423
135k17255423
asked Feb 19 at 16:57
gaurav_hodadegaurav_hodade
203
203
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
It makes sense to use an awk
script to create an awk
function...
$ awk -f script.awk file
function check()
if (marks < 80) return "green"
if (marks > 80) return "yellow"
Where script.awk
is
BEGIN
FS = ","
printf("function check() n")
printf("tif (%s %s %s) return "%s"n", $1, $2, $3, $4)
END
printf("n")
This simply loops over the lines in the comma-separated file and uses the fields to output if
and return
statements. There will be as many of these as there are lines in the input file.
I let the BEGIN
and END
blocks do the function header and footer, and the BEGIN
block additionally sets the input field separator to a comma.
No check for valid input is performed. This would probably go into the main block in the body of the script.
Redirect the output of the script to a file, e.g. color.awk
.
Adding an extra line to the input file,
marks,==,80,red
would result in
function check()
if (marks < 80) return "green"
if (marks > 80) return "yellow"
if (marks == 80) return "red"
Related:
- Why is using a shell loop to process text considered bad practice?
Thank you. It worked.
– gaurav_hodade
Feb 22 at 6:18
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f501639%2fcfg-file-to-awk-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
It makes sense to use an awk
script to create an awk
function...
$ awk -f script.awk file
function check()
if (marks < 80) return "green"
if (marks > 80) return "yellow"
Where script.awk
is
BEGIN
FS = ","
printf("function check() n")
printf("tif (%s %s %s) return "%s"n", $1, $2, $3, $4)
END
printf("n")
This simply loops over the lines in the comma-separated file and uses the fields to output if
and return
statements. There will be as many of these as there are lines in the input file.
I let the BEGIN
and END
blocks do the function header and footer, and the BEGIN
block additionally sets the input field separator to a comma.
No check for valid input is performed. This would probably go into the main block in the body of the script.
Redirect the output of the script to a file, e.g. color.awk
.
Adding an extra line to the input file,
marks,==,80,red
would result in
function check()
if (marks < 80) return "green"
if (marks > 80) return "yellow"
if (marks == 80) return "red"
Related:
- Why is using a shell loop to process text considered bad practice?
Thank you. It worked.
– gaurav_hodade
Feb 22 at 6:18
add a comment |
It makes sense to use an awk
script to create an awk
function...
$ awk -f script.awk file
function check()
if (marks < 80) return "green"
if (marks > 80) return "yellow"
Where script.awk
is
BEGIN
FS = ","
printf("function check() n")
printf("tif (%s %s %s) return "%s"n", $1, $2, $3, $4)
END
printf("n")
This simply loops over the lines in the comma-separated file and uses the fields to output if
and return
statements. There will be as many of these as there are lines in the input file.
I let the BEGIN
and END
blocks do the function header and footer, and the BEGIN
block additionally sets the input field separator to a comma.
No check for valid input is performed. This would probably go into the main block in the body of the script.
Redirect the output of the script to a file, e.g. color.awk
.
Adding an extra line to the input file,
marks,==,80,red
would result in
function check()
if (marks < 80) return "green"
if (marks > 80) return "yellow"
if (marks == 80) return "red"
Related:
- Why is using a shell loop to process text considered bad practice?
Thank you. It worked.
– gaurav_hodade
Feb 22 at 6:18
add a comment |
It makes sense to use an awk
script to create an awk
function...
$ awk -f script.awk file
function check()
if (marks < 80) return "green"
if (marks > 80) return "yellow"
Where script.awk
is
BEGIN
FS = ","
printf("function check() n")
printf("tif (%s %s %s) return "%s"n", $1, $2, $3, $4)
END
printf("n")
This simply loops over the lines in the comma-separated file and uses the fields to output if
and return
statements. There will be as many of these as there are lines in the input file.
I let the BEGIN
and END
blocks do the function header and footer, and the BEGIN
block additionally sets the input field separator to a comma.
No check for valid input is performed. This would probably go into the main block in the body of the script.
Redirect the output of the script to a file, e.g. color.awk
.
Adding an extra line to the input file,
marks,==,80,red
would result in
function check()
if (marks < 80) return "green"
if (marks > 80) return "yellow"
if (marks == 80) return "red"
Related:
- Why is using a shell loop to process text considered bad practice?
It makes sense to use an awk
script to create an awk
function...
$ awk -f script.awk file
function check()
if (marks < 80) return "green"
if (marks > 80) return "yellow"
Where script.awk
is
BEGIN
FS = ","
printf("function check() n")
printf("tif (%s %s %s) return "%s"n", $1, $2, $3, $4)
END
printf("n")
This simply loops over the lines in the comma-separated file and uses the fields to output if
and return
statements. There will be as many of these as there are lines in the input file.
I let the BEGIN
and END
blocks do the function header and footer, and the BEGIN
block additionally sets the input field separator to a comma.
No check for valid input is performed. This would probably go into the main block in the body of the script.
Redirect the output of the script to a file, e.g. color.awk
.
Adding an extra line to the input file,
marks,==,80,red
would result in
function check()
if (marks < 80) return "green"
if (marks > 80) return "yellow"
if (marks == 80) return "red"
Related:
- Why is using a shell loop to process text considered bad practice?
edited Feb 19 at 17:24
answered Feb 19 at 17:08
KusalanandaKusalananda
135k17255423
135k17255423
Thank you. It worked.
– gaurav_hodade
Feb 22 at 6:18
add a comment |
Thank you. It worked.
– gaurav_hodade
Feb 22 at 6:18
Thank you. It worked.
– gaurav_hodade
Feb 22 at 6:18
Thank you. It worked.
– gaurav_hodade
Feb 22 at 6:18
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f501639%2fcfg-file-to-awk-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown