Using NF with FPAT – what am I doing wrong?












1















I'm trying to figure out, and to learn AWK, why, when NF is used with FPAT regex, the comma is considered a field. I prefer using NF and FPAT:



1) NF – to limit the output to the actual number of fields for the record



2) FPAT – to handle an embedded comma in a quoted field like line 3:



 "Bus Driver, City/Transit",51


3) the awk script is used for several input files that have a different number of columns for record 6 - record 6 is the column name/header for the contents of the file...



The output from testing, the first, test1, uses a fixed value for number of fields, the second, test2, uses NF for the number of fields.



I've been using this script to experiment with some ideas/concepts - I'm not well versed with AWK so I'm trying to learn...



thank you - any help is much appreciated



using gawk 4.1.4



    BEGIN {
FPAT = "(^,)|([^,]+)|("[^"]+")"
OFS = "t"
}

NR == 6 {

for (i = 1; 6 >= i; ++i) {
#for (i = 1; NF >= i; ++i) {

colName[i] = $i
print "Column Name: " colName[i]

}

{ print "", "number of fields: " NF }
}


Input File starting at record 6: NR == 6 {...



    Occupation,States Licensed 
Barber,51
"Bus Driver, City/Transit",51


The output I expect/want:



    Column Name: Occupation
Column Name: States Licensed
number of fields: 2


test 1: for (i = 1; 6 >= i; ++i) {...



output is correct - what I expect/want, except, of course, for the 4 columns/fields that are not valid but are shown because of using a fixed value of 6.



    Column Name: Occupation
Column Name: States Licensed
Column Name:
Column Name:
Column Name:
Column Name:
number of fields: 2


test 2: for (i = 1; NF >= i; ++i) {...



output is NOT what I expect/want; note the comma is a indicate as a field



    Column Name: Occupation
Column Name: ,
Column Name: States Licensed
number of fields: 3









share|improve this question









New contributor




TomM is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    1















    I'm trying to figure out, and to learn AWK, why, when NF is used with FPAT regex, the comma is considered a field. I prefer using NF and FPAT:



    1) NF – to limit the output to the actual number of fields for the record



    2) FPAT – to handle an embedded comma in a quoted field like line 3:



     "Bus Driver, City/Transit",51


    3) the awk script is used for several input files that have a different number of columns for record 6 - record 6 is the column name/header for the contents of the file...



    The output from testing, the first, test1, uses a fixed value for number of fields, the second, test2, uses NF for the number of fields.



    I've been using this script to experiment with some ideas/concepts - I'm not well versed with AWK so I'm trying to learn...



    thank you - any help is much appreciated



    using gawk 4.1.4



        BEGIN {
    FPAT = "(^,)|([^,]+)|("[^"]+")"
    OFS = "t"
    }

    NR == 6 {

    for (i = 1; 6 >= i; ++i) {
    #for (i = 1; NF >= i; ++i) {

    colName[i] = $i
    print "Column Name: " colName[i]

    }

    { print "", "number of fields: " NF }
    }


    Input File starting at record 6: NR == 6 {...



        Occupation,States Licensed 
    Barber,51
    "Bus Driver, City/Transit",51


    The output I expect/want:



        Column Name: Occupation
    Column Name: States Licensed
    number of fields: 2


    test 1: for (i = 1; 6 >= i; ++i) {...



    output is correct - what I expect/want, except, of course, for the 4 columns/fields that are not valid but are shown because of using a fixed value of 6.



        Column Name: Occupation
    Column Name: States Licensed
    Column Name:
    Column Name:
    Column Name:
    Column Name:
    number of fields: 2


    test 2: for (i = 1; NF >= i; ++i) {...



    output is NOT what I expect/want; note the comma is a indicate as a field



        Column Name: Occupation
    Column Name: ,
    Column Name: States Licensed
    number of fields: 3









    share|improve this question









    New contributor




    TomM is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      1












      1








      1








      I'm trying to figure out, and to learn AWK, why, when NF is used with FPAT regex, the comma is considered a field. I prefer using NF and FPAT:



      1) NF – to limit the output to the actual number of fields for the record



      2) FPAT – to handle an embedded comma in a quoted field like line 3:



       "Bus Driver, City/Transit",51


      3) the awk script is used for several input files that have a different number of columns for record 6 - record 6 is the column name/header for the contents of the file...



      The output from testing, the first, test1, uses a fixed value for number of fields, the second, test2, uses NF for the number of fields.



      I've been using this script to experiment with some ideas/concepts - I'm not well versed with AWK so I'm trying to learn...



      thank you - any help is much appreciated



      using gawk 4.1.4



          BEGIN {
      FPAT = "(^,)|([^,]+)|("[^"]+")"
      OFS = "t"
      }

      NR == 6 {

      for (i = 1; 6 >= i; ++i) {
      #for (i = 1; NF >= i; ++i) {

      colName[i] = $i
      print "Column Name: " colName[i]

      }

      { print "", "number of fields: " NF }
      }


      Input File starting at record 6: NR == 6 {...



          Occupation,States Licensed 
      Barber,51
      "Bus Driver, City/Transit",51


      The output I expect/want:



          Column Name: Occupation
      Column Name: States Licensed
      number of fields: 2


      test 1: for (i = 1; 6 >= i; ++i) {...



      output is correct - what I expect/want, except, of course, for the 4 columns/fields that are not valid but are shown because of using a fixed value of 6.



          Column Name: Occupation
      Column Name: States Licensed
      Column Name:
      Column Name:
      Column Name:
      Column Name:
      number of fields: 2


      test 2: for (i = 1; NF >= i; ++i) {...



      output is NOT what I expect/want; note the comma is a indicate as a field



          Column Name: Occupation
      Column Name: ,
      Column Name: States Licensed
      number of fields: 3









      share|improve this question









      New contributor




      TomM is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      I'm trying to figure out, and to learn AWK, why, when NF is used with FPAT regex, the comma is considered a field. I prefer using NF and FPAT:



      1) NF – to limit the output to the actual number of fields for the record



      2) FPAT – to handle an embedded comma in a quoted field like line 3:



       "Bus Driver, City/Transit",51


      3) the awk script is used for several input files that have a different number of columns for record 6 - record 6 is the column name/header for the contents of the file...



      The output from testing, the first, test1, uses a fixed value for number of fields, the second, test2, uses NF for the number of fields.



      I've been using this script to experiment with some ideas/concepts - I'm not well versed with AWK so I'm trying to learn...



      thank you - any help is much appreciated



      using gawk 4.1.4



          BEGIN {
      FPAT = "(^,)|([^,]+)|("[^"]+")"
      OFS = "t"
      }

      NR == 6 {

      for (i = 1; 6 >= i; ++i) {
      #for (i = 1; NF >= i; ++i) {

      colName[i] = $i
      print "Column Name: " colName[i]

      }

      { print "", "number of fields: " NF }
      }


      Input File starting at record 6: NR == 6 {...



          Occupation,States Licensed 
      Barber,51
      "Bus Driver, City/Transit",51


      The output I expect/want:



          Column Name: Occupation
      Column Name: States Licensed
      number of fields: 2


      test 1: for (i = 1; 6 >= i; ++i) {...



      output is correct - what I expect/want, except, of course, for the 4 columns/fields that are not valid but are shown because of using a fixed value of 6.



          Column Name: Occupation
      Column Name: States Licensed
      Column Name:
      Column Name:
      Column Name:
      Column Name:
      number of fields: 2


      test 2: for (i = 1; NF >= i; ++i) {...



      output is NOT what I expect/want; note the comma is a indicate as a field



          Column Name: Occupation
      Column Name: ,
      Column Name: States Licensed
      number of fields: 3






      text-processing awk csv gawk






      share|improve this question









      New contributor




      TomM is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      TomM is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 19 mins ago









      G-Man

      13.2k93566




      13.2k93566






      New contributor




      TomM is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 51 mins ago









      TomMTomM

      62




      62




      New contributor




      TomM is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      TomM is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      TomM is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          0






          active

          oldest

          votes











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


          }
          });






          TomM is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f503891%2fusing-nf-with-fpat-what-am-i-doing-wrong%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          TomM is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          TomM is a new contributor. Be nice, and check out our Code of Conduct.













          TomM is a new contributor. Be nice, and check out our Code of Conduct.












          TomM is a new contributor. Be nice, and check out our Code of Conduct.
















          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%2f503891%2fusing-nf-with-fpat-what-am-i-doing-wrong%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

          Histoire des bourses de valeurs

          Why is there Russian traffic in my log files?

          Rename multiple files to decrement number in file name?