.cfg file to .awk file

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












1















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









share|improve this question




























    1















    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









    share|improve this question


























      1












      1








      1








      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









      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 19 at 17:15









      Kusalananda

      135k17255423




      135k17255423










      asked Feb 19 at 16:57









      gaurav_hodadegaurav_hodade

      203




      203




















          1 Answer
          1






          active

          oldest

          votes


















          1














          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?





          share|improve this answer

























          • Thank you. It worked.

            – gaurav_hodade
            Feb 22 at 6:18










          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
          );



          );













          draft saved

          draft discarded


















          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









          1














          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?





          share|improve this answer

























          • Thank you. It worked.

            – gaurav_hodade
            Feb 22 at 6:18















          1














          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?





          share|improve this answer

























          • Thank you. It worked.

            – gaurav_hodade
            Feb 22 at 6:18













          1












          1








          1







          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?





          share|improve this answer















          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?






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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

















          • 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

















          draft saved

          draft discarded
















































          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.




          draft saved


          draft discarded














          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





















































          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






          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay