facing error with awk command

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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















input:



123456.00|aswani|india|ap
23456.00|rani|us|tel
233|ramu|londan|vih


output:



aswani|ap
rani|tel
ramu|vih


I'm trying for this output with the command



awk '";OFS="print $2,$4'


but I'm getting output as:



|
rani|tel
ramu|vih


command was executing from 2 line but not from first line. i'm trying for output along with first line










share|improve this question






























    0















    input:



    123456.00|aswani|india|ap
    23456.00|rani|us|tel
    233|ramu|londan|vih


    output:



    aswani|ap
    rani|tel
    ramu|vih


    I'm trying for this output with the command



    awk '";OFS="print $2,$4'


    but I'm getting output as:



    |
    rani|tel
    ramu|vih


    command was executing from 2 line but not from first line. i'm trying for output along with first line










    share|improve this question


























      0












      0








      0








      input:



      123456.00|aswani|india|ap
      23456.00|rani|us|tel
      233|ramu|londan|vih


      output:



      aswani|ap
      rani|tel
      ramu|vih


      I'm trying for this output with the command



      awk '";OFS="print $2,$4'


      but I'm getting output as:



      |
      rani|tel
      ramu|vih


      command was executing from 2 line but not from first line. i'm trying for output along with first line










      share|improve this question
















      input:



      123456.00|aswani|india|ap
      23456.00|rani|us|tel
      233|ramu|londan|vih


      output:



      aswani|ap
      rani|tel
      ramu|vih


      I'm trying for this output with the command



      awk '";OFS="print $2,$4'


      but I'm getting output as:



      |
      rani|tel
      ramu|vih


      command was executing from 2 line but not from first line. i'm trying for output along with first line







      awk






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 7 at 17:08









      filbranden

      10.7k21847




      10.7k21847










      asked Mar 7 at 17:05









      AswaniAswani

      41




      41




















          2 Answers
          2






          active

          oldest

          votes


















          2














          You should put the first block (setting FS and OFS) inside a "BEGIN" block:



          awk 'BEGIN ";OFS=" print $2,$4'


          A "BEGIN" block will run before the first line of text is processed. In contrast, the stand-alone block you had will run on every line of text, but for the first line, setting FS will happen too late, since the fields will have been split already.



          An alternative is to set FS through an awk command line option, but you still need to take care of OFS:



          awk -F '|' '"; print $2,$4'


          Or:



          awk -F '|' -v OFS='|' 'print $2,$4'


          UPDATE: As pointed out by @Kusalananda, some awk implementations (such as BSD awk) will re-split the fields if FS gets reset, so that setting doesn't need to happen in a "BEGIN" block. In GNU awk (which is typically the one found in Linux distributions), that does not happen, so FS needs to be set before the line is split.






          share|improve this answer




















          • 1





            Can you add explanation?

            – Prvt_Yadv
            Mar 7 at 17:16











          • @PRY Added an explanation.

            – filbranden
            Mar 7 at 17:19











          • @Kusalananda I believe the problem is that it's setting FS too late, after the fields have been split on the first line. See my edited answer.

            – filbranden
            Mar 7 at 17:28











          • @filbranden You are correct. It's a GNU awk thing. I was testing with BSD awk and it's re-splitting as soon as you change FS. Good catch!

            – Kusalananda
            Mar 7 at 17:35






          • 1





            See also the FS = OFS = "|" syntax or awk -F '|' -v OFS='|'

            – Stéphane Chazelas
            Mar 7 at 17:39


















          2














          As filbranden points out GNU awk (and mawk) requires FS to be correctly set before reading the first line, otherwise it will split the line on the default field separator (a sequence of whitespaces). Your code sets FS after reading each line, so it will have the wrong value while processing the first line of the file.



          OpenBSD awk (at least) behaves differently and appears to split the current record when a field is accessed, but not before that. This means that your code actually works on an OpenBSD system.



          I'm just going to add to that and say that to extract a set of columns from a file with no further processing, the cut command is also useful:



          $ cut -d '|' -f 2,4 <file
          aswani|ap
          rani|tel
          ramu|vih





          share|improve this answer

























          • The splitting is done using the value of FS at the time the record is read, it's no about doing it before the first line. printf 'a,bnc,dn' | awk 'FS=","; print $1' prints a,b and c

            – Stéphane Chazelas
            Mar 7 at 17:48






          • 1





            @StéphaneChazelas That prints a and c on OpenBSD.

            – Kusalananda
            Mar 7 at 17:49











          • Then OpenBSD awk is not POSIX compliant in that regard. Solaris's /usr/xpg4/bin/awk and busybox awk behave like GNU awk.

            – Stéphane Chazelas
            Mar 7 at 17:54






          • 1





            In BSDs, the splitting is done using the value of IFS at the time the first reference to a field or NF is made.

            – Stéphane Chazelas
            Mar 7 at 17:59







          • 1





            Note that the cut and awk behaviour differ on lines with fewer than 4 fields (also note the non-intuitive behaviour of cut when the input has only one field (no separator))

            – Stéphane Chazelas
            Mar 7 at 18:02











          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%2f504959%2ffacing-error-with-awk-command%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          You should put the first block (setting FS and OFS) inside a "BEGIN" block:



          awk 'BEGIN ";OFS=" print $2,$4'


          A "BEGIN" block will run before the first line of text is processed. In contrast, the stand-alone block you had will run on every line of text, but for the first line, setting FS will happen too late, since the fields will have been split already.



          An alternative is to set FS through an awk command line option, but you still need to take care of OFS:



          awk -F '|' '"; print $2,$4'


          Or:



          awk -F '|' -v OFS='|' 'print $2,$4'


          UPDATE: As pointed out by @Kusalananda, some awk implementations (such as BSD awk) will re-split the fields if FS gets reset, so that setting doesn't need to happen in a "BEGIN" block. In GNU awk (which is typically the one found in Linux distributions), that does not happen, so FS needs to be set before the line is split.






          share|improve this answer




















          • 1





            Can you add explanation?

            – Prvt_Yadv
            Mar 7 at 17:16











          • @PRY Added an explanation.

            – filbranden
            Mar 7 at 17:19











          • @Kusalananda I believe the problem is that it's setting FS too late, after the fields have been split on the first line. See my edited answer.

            – filbranden
            Mar 7 at 17:28











          • @filbranden You are correct. It's a GNU awk thing. I was testing with BSD awk and it's re-splitting as soon as you change FS. Good catch!

            – Kusalananda
            Mar 7 at 17:35






          • 1





            See also the FS = OFS = "|" syntax or awk -F '|' -v OFS='|'

            – Stéphane Chazelas
            Mar 7 at 17:39















          2














          You should put the first block (setting FS and OFS) inside a "BEGIN" block:



          awk 'BEGIN ";OFS=" print $2,$4'


          A "BEGIN" block will run before the first line of text is processed. In contrast, the stand-alone block you had will run on every line of text, but for the first line, setting FS will happen too late, since the fields will have been split already.



          An alternative is to set FS through an awk command line option, but you still need to take care of OFS:



          awk -F '|' '"; print $2,$4'


          Or:



          awk -F '|' -v OFS='|' 'print $2,$4'


          UPDATE: As pointed out by @Kusalananda, some awk implementations (such as BSD awk) will re-split the fields if FS gets reset, so that setting doesn't need to happen in a "BEGIN" block. In GNU awk (which is typically the one found in Linux distributions), that does not happen, so FS needs to be set before the line is split.






          share|improve this answer




















          • 1





            Can you add explanation?

            – Prvt_Yadv
            Mar 7 at 17:16











          • @PRY Added an explanation.

            – filbranden
            Mar 7 at 17:19











          • @Kusalananda I believe the problem is that it's setting FS too late, after the fields have been split on the first line. See my edited answer.

            – filbranden
            Mar 7 at 17:28











          • @filbranden You are correct. It's a GNU awk thing. I was testing with BSD awk and it's re-splitting as soon as you change FS. Good catch!

            – Kusalananda
            Mar 7 at 17:35






          • 1





            See also the FS = OFS = "|" syntax or awk -F '|' -v OFS='|'

            – Stéphane Chazelas
            Mar 7 at 17:39













          2












          2








          2







          You should put the first block (setting FS and OFS) inside a "BEGIN" block:



          awk 'BEGIN ";OFS=" print $2,$4'


          A "BEGIN" block will run before the first line of text is processed. In contrast, the stand-alone block you had will run on every line of text, but for the first line, setting FS will happen too late, since the fields will have been split already.



          An alternative is to set FS through an awk command line option, but you still need to take care of OFS:



          awk -F '|' '"; print $2,$4'


          Or:



          awk -F '|' -v OFS='|' 'print $2,$4'


          UPDATE: As pointed out by @Kusalananda, some awk implementations (such as BSD awk) will re-split the fields if FS gets reset, so that setting doesn't need to happen in a "BEGIN" block. In GNU awk (which is typically the one found in Linux distributions), that does not happen, so FS needs to be set before the line is split.






          share|improve this answer















          You should put the first block (setting FS and OFS) inside a "BEGIN" block:



          awk 'BEGIN ";OFS=" print $2,$4'


          A "BEGIN" block will run before the first line of text is processed. In contrast, the stand-alone block you had will run on every line of text, but for the first line, setting FS will happen too late, since the fields will have been split already.



          An alternative is to set FS through an awk command line option, but you still need to take care of OFS:



          awk -F '|' '"; print $2,$4'


          Or:



          awk -F '|' -v OFS='|' 'print $2,$4'


          UPDATE: As pointed out by @Kusalananda, some awk implementations (such as BSD awk) will re-split the fields if FS gets reset, so that setting doesn't need to happen in a "BEGIN" block. In GNU awk (which is typically the one found in Linux distributions), that does not happen, so FS needs to be set before the line is split.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 7 at 17:40

























          answered Mar 7 at 17:12









          filbrandenfilbranden

          10.7k21847




          10.7k21847







          • 1





            Can you add explanation?

            – Prvt_Yadv
            Mar 7 at 17:16











          • @PRY Added an explanation.

            – filbranden
            Mar 7 at 17:19











          • @Kusalananda I believe the problem is that it's setting FS too late, after the fields have been split on the first line. See my edited answer.

            – filbranden
            Mar 7 at 17:28











          • @filbranden You are correct. It's a GNU awk thing. I was testing with BSD awk and it's re-splitting as soon as you change FS. Good catch!

            – Kusalananda
            Mar 7 at 17:35






          • 1





            See also the FS = OFS = "|" syntax or awk -F '|' -v OFS='|'

            – Stéphane Chazelas
            Mar 7 at 17:39












          • 1





            Can you add explanation?

            – Prvt_Yadv
            Mar 7 at 17:16











          • @PRY Added an explanation.

            – filbranden
            Mar 7 at 17:19











          • @Kusalananda I believe the problem is that it's setting FS too late, after the fields have been split on the first line. See my edited answer.

            – filbranden
            Mar 7 at 17:28











          • @filbranden You are correct. It's a GNU awk thing. I was testing with BSD awk and it's re-splitting as soon as you change FS. Good catch!

            – Kusalananda
            Mar 7 at 17:35






          • 1





            See also the FS = OFS = "|" syntax or awk -F '|' -v OFS='|'

            – Stéphane Chazelas
            Mar 7 at 17:39







          1




          1





          Can you add explanation?

          – Prvt_Yadv
          Mar 7 at 17:16





          Can you add explanation?

          – Prvt_Yadv
          Mar 7 at 17:16













          @PRY Added an explanation.

          – filbranden
          Mar 7 at 17:19





          @PRY Added an explanation.

          – filbranden
          Mar 7 at 17:19













          @Kusalananda I believe the problem is that it's setting FS too late, after the fields have been split on the first line. See my edited answer.

          – filbranden
          Mar 7 at 17:28





          @Kusalananda I believe the problem is that it's setting FS too late, after the fields have been split on the first line. See my edited answer.

          – filbranden
          Mar 7 at 17:28













          @filbranden You are correct. It's a GNU awk thing. I was testing with BSD awk and it's re-splitting as soon as you change FS. Good catch!

          – Kusalananda
          Mar 7 at 17:35





          @filbranden You are correct. It's a GNU awk thing. I was testing with BSD awk and it's re-splitting as soon as you change FS. Good catch!

          – Kusalananda
          Mar 7 at 17:35




          1




          1





          See also the FS = OFS = "|" syntax or awk -F '|' -v OFS='|'

          – Stéphane Chazelas
          Mar 7 at 17:39





          See also the FS = OFS = "|" syntax or awk -F '|' -v OFS='|'

          – Stéphane Chazelas
          Mar 7 at 17:39













          2














          As filbranden points out GNU awk (and mawk) requires FS to be correctly set before reading the first line, otherwise it will split the line on the default field separator (a sequence of whitespaces). Your code sets FS after reading each line, so it will have the wrong value while processing the first line of the file.



          OpenBSD awk (at least) behaves differently and appears to split the current record when a field is accessed, but not before that. This means that your code actually works on an OpenBSD system.



          I'm just going to add to that and say that to extract a set of columns from a file with no further processing, the cut command is also useful:



          $ cut -d '|' -f 2,4 <file
          aswani|ap
          rani|tel
          ramu|vih





          share|improve this answer

























          • The splitting is done using the value of FS at the time the record is read, it's no about doing it before the first line. printf 'a,bnc,dn' | awk 'FS=","; print $1' prints a,b and c

            – Stéphane Chazelas
            Mar 7 at 17:48






          • 1





            @StéphaneChazelas That prints a and c on OpenBSD.

            – Kusalananda
            Mar 7 at 17:49











          • Then OpenBSD awk is not POSIX compliant in that regard. Solaris's /usr/xpg4/bin/awk and busybox awk behave like GNU awk.

            – Stéphane Chazelas
            Mar 7 at 17:54






          • 1





            In BSDs, the splitting is done using the value of IFS at the time the first reference to a field or NF is made.

            – Stéphane Chazelas
            Mar 7 at 17:59







          • 1





            Note that the cut and awk behaviour differ on lines with fewer than 4 fields (also note the non-intuitive behaviour of cut when the input has only one field (no separator))

            – Stéphane Chazelas
            Mar 7 at 18:02















          2














          As filbranden points out GNU awk (and mawk) requires FS to be correctly set before reading the first line, otherwise it will split the line on the default field separator (a sequence of whitespaces). Your code sets FS after reading each line, so it will have the wrong value while processing the first line of the file.



          OpenBSD awk (at least) behaves differently and appears to split the current record when a field is accessed, but not before that. This means that your code actually works on an OpenBSD system.



          I'm just going to add to that and say that to extract a set of columns from a file with no further processing, the cut command is also useful:



          $ cut -d '|' -f 2,4 <file
          aswani|ap
          rani|tel
          ramu|vih





          share|improve this answer

























          • The splitting is done using the value of FS at the time the record is read, it's no about doing it before the first line. printf 'a,bnc,dn' | awk 'FS=","; print $1' prints a,b and c

            – Stéphane Chazelas
            Mar 7 at 17:48






          • 1





            @StéphaneChazelas That prints a and c on OpenBSD.

            – Kusalananda
            Mar 7 at 17:49











          • Then OpenBSD awk is not POSIX compliant in that regard. Solaris's /usr/xpg4/bin/awk and busybox awk behave like GNU awk.

            – Stéphane Chazelas
            Mar 7 at 17:54






          • 1





            In BSDs, the splitting is done using the value of IFS at the time the first reference to a field or NF is made.

            – Stéphane Chazelas
            Mar 7 at 17:59







          • 1





            Note that the cut and awk behaviour differ on lines with fewer than 4 fields (also note the non-intuitive behaviour of cut when the input has only one field (no separator))

            – Stéphane Chazelas
            Mar 7 at 18:02













          2












          2








          2







          As filbranden points out GNU awk (and mawk) requires FS to be correctly set before reading the first line, otherwise it will split the line on the default field separator (a sequence of whitespaces). Your code sets FS after reading each line, so it will have the wrong value while processing the first line of the file.



          OpenBSD awk (at least) behaves differently and appears to split the current record when a field is accessed, but not before that. This means that your code actually works on an OpenBSD system.



          I'm just going to add to that and say that to extract a set of columns from a file with no further processing, the cut command is also useful:



          $ cut -d '|' -f 2,4 <file
          aswani|ap
          rani|tel
          ramu|vih





          share|improve this answer















          As filbranden points out GNU awk (and mawk) requires FS to be correctly set before reading the first line, otherwise it will split the line on the default field separator (a sequence of whitespaces). Your code sets FS after reading each line, so it will have the wrong value while processing the first line of the file.



          OpenBSD awk (at least) behaves differently and appears to split the current record when a field is accessed, but not before that. This means that your code actually works on an OpenBSD system.



          I'm just going to add to that and say that to extract a set of columns from a file with no further processing, the cut command is also useful:



          $ cut -d '|' -f 2,4 <file
          aswani|ap
          rani|tel
          ramu|vih






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 7 at 18:09

























          answered Mar 7 at 17:42









          KusalanandaKusalananda

          139k17261433




          139k17261433












          • The splitting is done using the value of FS at the time the record is read, it's no about doing it before the first line. printf 'a,bnc,dn' | awk 'FS=","; print $1' prints a,b and c

            – Stéphane Chazelas
            Mar 7 at 17:48






          • 1





            @StéphaneChazelas That prints a and c on OpenBSD.

            – Kusalananda
            Mar 7 at 17:49











          • Then OpenBSD awk is not POSIX compliant in that regard. Solaris's /usr/xpg4/bin/awk and busybox awk behave like GNU awk.

            – Stéphane Chazelas
            Mar 7 at 17:54






          • 1





            In BSDs, the splitting is done using the value of IFS at the time the first reference to a field or NF is made.

            – Stéphane Chazelas
            Mar 7 at 17:59







          • 1





            Note that the cut and awk behaviour differ on lines with fewer than 4 fields (also note the non-intuitive behaviour of cut when the input has only one field (no separator))

            – Stéphane Chazelas
            Mar 7 at 18:02

















          • The splitting is done using the value of FS at the time the record is read, it's no about doing it before the first line. printf 'a,bnc,dn' | awk 'FS=","; print $1' prints a,b and c

            – Stéphane Chazelas
            Mar 7 at 17:48






          • 1





            @StéphaneChazelas That prints a and c on OpenBSD.

            – Kusalananda
            Mar 7 at 17:49











          • Then OpenBSD awk is not POSIX compliant in that regard. Solaris's /usr/xpg4/bin/awk and busybox awk behave like GNU awk.

            – Stéphane Chazelas
            Mar 7 at 17:54






          • 1





            In BSDs, the splitting is done using the value of IFS at the time the first reference to a field or NF is made.

            – Stéphane Chazelas
            Mar 7 at 17:59







          • 1





            Note that the cut and awk behaviour differ on lines with fewer than 4 fields (also note the non-intuitive behaviour of cut when the input has only one field (no separator))

            – Stéphane Chazelas
            Mar 7 at 18:02
















          The splitting is done using the value of FS at the time the record is read, it's no about doing it before the first line. printf 'a,bnc,dn' | awk 'FS=","; print $1' prints a,b and c

          – Stéphane Chazelas
          Mar 7 at 17:48





          The splitting is done using the value of FS at the time the record is read, it's no about doing it before the first line. printf 'a,bnc,dn' | awk 'FS=","; print $1' prints a,b and c

          – Stéphane Chazelas
          Mar 7 at 17:48




          1




          1





          @StéphaneChazelas That prints a and c on OpenBSD.

          – Kusalananda
          Mar 7 at 17:49





          @StéphaneChazelas That prints a and c on OpenBSD.

          – Kusalananda
          Mar 7 at 17:49













          Then OpenBSD awk is not POSIX compliant in that regard. Solaris's /usr/xpg4/bin/awk and busybox awk behave like GNU awk.

          – Stéphane Chazelas
          Mar 7 at 17:54





          Then OpenBSD awk is not POSIX compliant in that regard. Solaris's /usr/xpg4/bin/awk and busybox awk behave like GNU awk.

          – Stéphane Chazelas
          Mar 7 at 17:54




          1




          1





          In BSDs, the splitting is done using the value of IFS at the time the first reference to a field or NF is made.

          – Stéphane Chazelas
          Mar 7 at 17:59






          In BSDs, the splitting is done using the value of IFS at the time the first reference to a field or NF is made.

          – Stéphane Chazelas
          Mar 7 at 17:59





          1




          1





          Note that the cut and awk behaviour differ on lines with fewer than 4 fields (also note the non-intuitive behaviour of cut when the input has only one field (no separator))

          – Stéphane Chazelas
          Mar 7 at 18:02





          Note that the cut and awk behaviour differ on lines with fewer than 4 fields (also note the non-intuitive behaviour of cut when the input has only one field (no separator))

          – Stéphane Chazelas
          Mar 7 at 18:02

















          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%2f504959%2ffacing-error-with-awk-command%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?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?