Using AWK: If value exists, assign to variable

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











up vote
0
down vote

favorite












Really sorry if this has been asked before as it feels basic, I'm just not connecting the dots and it's making me tear my hair out!



Essentially I have a text file , and I'm assigning the second field of particular rows to a variable i.e



VAR=$(awk 'NR==1 print $2' entry.txt)


However, sometimes on row one in the text file, it will have 4 fields. I would like to say



 If ROW 1 has 4 fields 
VAR = $2, $3 & $4 concatenated
Else
VAR = $2


I'm sure this is basic awk, I'm just undertaking a small project and have never used it before. I know i should read a book, but this really is a tiny script!



Thanks







share|improve this question























    up vote
    0
    down vote

    favorite












    Really sorry if this has been asked before as it feels basic, I'm just not connecting the dots and it's making me tear my hair out!



    Essentially I have a text file , and I'm assigning the second field of particular rows to a variable i.e



    VAR=$(awk 'NR==1 print $2' entry.txt)


    However, sometimes on row one in the text file, it will have 4 fields. I would like to say



     If ROW 1 has 4 fields 
    VAR = $2, $3 & $4 concatenated
    Else
    VAR = $2


    I'm sure this is basic awk, I'm just undertaking a small project and have never used it before. I know i should read a book, but this really is a tiny script!



    Thanks







    share|improve this question





















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Really sorry if this has been asked before as it feels basic, I'm just not connecting the dots and it's making me tear my hair out!



      Essentially I have a text file , and I'm assigning the second field of particular rows to a variable i.e



      VAR=$(awk 'NR==1 print $2' entry.txt)


      However, sometimes on row one in the text file, it will have 4 fields. I would like to say



       If ROW 1 has 4 fields 
      VAR = $2, $3 & $4 concatenated
      Else
      VAR = $2


      I'm sure this is basic awk, I'm just undertaking a small project and have never used it before. I know i should read a book, but this really is a tiny script!



      Thanks







      share|improve this question











      Really sorry if this has been asked before as it feels basic, I'm just not connecting the dots and it's making me tear my hair out!



      Essentially I have a text file , and I'm assigning the second field of particular rows to a variable i.e



      VAR=$(awk 'NR==1 print $2' entry.txt)


      However, sometimes on row one in the text file, it will have 4 fields. I would like to say



       If ROW 1 has 4 fields 
      VAR = $2, $3 & $4 concatenated
      Else
      VAR = $2


      I'm sure this is basic awk, I'm just undertaking a small project and have never used it before. I know i should read a book, but this really is a tiny script!



      Thanks









      share|improve this question










      share|improve this question




      share|improve this question









      asked Apr 20 at 15:48









      Harry S

      31




      31




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          awk 'NF == 4 print $2 $3 $4; exit print $2; exit ' file


          This concatenates the second, third and fourth fields if there are exactly four whitespace-delimited fields on the first line of the file. Otherwise, it will return the second field from the first line of the file. The program exits as soon as it has outputted anything, so we are certain that it's only the first line that we'll ever see.



          With different logic:



          awk 'NF == 4 out = $2 $3 $4 NF != 4 out = $4 print out; exit ' file


          Either of these commands goes inside your command substitution:



          var=$( awk ... )





          share|improve this answer























          • Thank you, even simply seeing these examples helped me understand my small syntax errors that ultimately lead to the hair-pulling!
            – Harry S
            Apr 23 at 15:31

















          up vote
          0
          down vote













          awk 'NR==1 && NF==4 var=$2 $3 $4 NR==1 && NF!=4 var=$2 ' /path/to/input





          share|improve this answer





















          • Thank you, that was very helpful in understanding how to apply the problem to any row of a file
            – Harry S
            Apr 23 at 15:32

















          up vote
          0
          down vote













          You can do



          awk '$1=""; print (NF==4)?$0:$2; exit' OFS= infile


          The Ternary condition operator (NF==4)?$0:$2 checks if the Number of Fields were equal with 4 (when fields are separated with whitespaces (awk default FS) ) then print the whole line $0 after we striping the first field $1="" else : print filed#2 $2.






          share|improve this answer























            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',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            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%2f438972%2fusing-awk-if-value-exists-assign-to-variable%23new-answer', 'question_page');

            );

            Post as a guest






























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            awk 'NF == 4 print $2 $3 $4; exit print $2; exit ' file


            This concatenates the second, third and fourth fields if there are exactly four whitespace-delimited fields on the first line of the file. Otherwise, it will return the second field from the first line of the file. The program exits as soon as it has outputted anything, so we are certain that it's only the first line that we'll ever see.



            With different logic:



            awk 'NF == 4 out = $2 $3 $4 NF != 4 out = $4 print out; exit ' file


            Either of these commands goes inside your command substitution:



            var=$( awk ... )





            share|improve this answer























            • Thank you, even simply seeing these examples helped me understand my small syntax errors that ultimately lead to the hair-pulling!
              – Harry S
              Apr 23 at 15:31














            up vote
            1
            down vote



            accepted










            awk 'NF == 4 print $2 $3 $4; exit print $2; exit ' file


            This concatenates the second, third and fourth fields if there are exactly four whitespace-delimited fields on the first line of the file. Otherwise, it will return the second field from the first line of the file. The program exits as soon as it has outputted anything, so we are certain that it's only the first line that we'll ever see.



            With different logic:



            awk 'NF == 4 out = $2 $3 $4 NF != 4 out = $4 print out; exit ' file


            Either of these commands goes inside your command substitution:



            var=$( awk ... )





            share|improve this answer























            • Thank you, even simply seeing these examples helped me understand my small syntax errors that ultimately lead to the hair-pulling!
              – Harry S
              Apr 23 at 15:31












            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






            awk 'NF == 4 print $2 $3 $4; exit print $2; exit ' file


            This concatenates the second, third and fourth fields if there are exactly four whitespace-delimited fields on the first line of the file. Otherwise, it will return the second field from the first line of the file. The program exits as soon as it has outputted anything, so we are certain that it's only the first line that we'll ever see.



            With different logic:



            awk 'NF == 4 out = $2 $3 $4 NF != 4 out = $4 print out; exit ' file


            Either of these commands goes inside your command substitution:



            var=$( awk ... )





            share|improve this answer















            awk 'NF == 4 print $2 $3 $4; exit print $2; exit ' file


            This concatenates the second, third and fourth fields if there are exactly four whitespace-delimited fields on the first line of the file. Otherwise, it will return the second field from the first line of the file. The program exits as soon as it has outputted anything, so we are certain that it's only the first line that we'll ever see.



            With different logic:



            awk 'NF == 4 out = $2 $3 $4 NF != 4 out = $4 print out; exit ' file


            Either of these commands goes inside your command substitution:



            var=$( awk ... )






            share|improve this answer















            share|improve this answer



            share|improve this answer








            edited Apr 23 at 15:43


























            answered Apr 20 at 15:52









            Kusalananda

            102k13199315




            102k13199315











            • Thank you, even simply seeing these examples helped me understand my small syntax errors that ultimately lead to the hair-pulling!
              – Harry S
              Apr 23 at 15:31
















            • Thank you, even simply seeing these examples helped me understand my small syntax errors that ultimately lead to the hair-pulling!
              – Harry S
              Apr 23 at 15:31















            Thank you, even simply seeing these examples helped me understand my small syntax errors that ultimately lead to the hair-pulling!
            – Harry S
            Apr 23 at 15:31




            Thank you, even simply seeing these examples helped me understand my small syntax errors that ultimately lead to the hair-pulling!
            – Harry S
            Apr 23 at 15:31












            up vote
            0
            down vote













            awk 'NR==1 && NF==4 var=$2 $3 $4 NR==1 && NF!=4 var=$2 ' /path/to/input





            share|improve this answer





















            • Thank you, that was very helpful in understanding how to apply the problem to any row of a file
              – Harry S
              Apr 23 at 15:32














            up vote
            0
            down vote













            awk 'NR==1 && NF==4 var=$2 $3 $4 NR==1 && NF!=4 var=$2 ' /path/to/input





            share|improve this answer





















            • Thank you, that was very helpful in understanding how to apply the problem to any row of a file
              – Harry S
              Apr 23 at 15:32












            up vote
            0
            down vote










            up vote
            0
            down vote









            awk 'NR==1 && NF==4 var=$2 $3 $4 NR==1 && NF!=4 var=$2 ' /path/to/input





            share|improve this answer













            awk 'NR==1 && NF==4 var=$2 $3 $4 NR==1 && NF!=4 var=$2 ' /path/to/input






            share|improve this answer













            share|improve this answer



            share|improve this answer











            answered Apr 20 at 15:52









            DopeGhoti

            40k54779




            40k54779











            • Thank you, that was very helpful in understanding how to apply the problem to any row of a file
              – Harry S
              Apr 23 at 15:32
















            • Thank you, that was very helpful in understanding how to apply the problem to any row of a file
              – Harry S
              Apr 23 at 15:32















            Thank you, that was very helpful in understanding how to apply the problem to any row of a file
            – Harry S
            Apr 23 at 15:32




            Thank you, that was very helpful in understanding how to apply the problem to any row of a file
            – Harry S
            Apr 23 at 15:32










            up vote
            0
            down vote













            You can do



            awk '$1=""; print (NF==4)?$0:$2; exit' OFS= infile


            The Ternary condition operator (NF==4)?$0:$2 checks if the Number of Fields were equal with 4 (when fields are separated with whitespaces (awk default FS) ) then print the whole line $0 after we striping the first field $1="" else : print filed#2 $2.






            share|improve this answer



























              up vote
              0
              down vote













              You can do



              awk '$1=""; print (NF==4)?$0:$2; exit' OFS= infile


              The Ternary condition operator (NF==4)?$0:$2 checks if the Number of Fields were equal with 4 (when fields are separated with whitespaces (awk default FS) ) then print the whole line $0 after we striping the first field $1="" else : print filed#2 $2.






              share|improve this answer

























                up vote
                0
                down vote










                up vote
                0
                down vote









                You can do



                awk '$1=""; print (NF==4)?$0:$2; exit' OFS= infile


                The Ternary condition operator (NF==4)?$0:$2 checks if the Number of Fields were equal with 4 (when fields are separated with whitespaces (awk default FS) ) then print the whole line $0 after we striping the first field $1="" else : print filed#2 $2.






                share|improve this answer















                You can do



                awk '$1=""; print (NF==4)?$0:$2; exit' OFS= infile


                The Ternary condition operator (NF==4)?$0:$2 checks if the Number of Fields were equal with 4 (when fields are separated with whitespaces (awk default FS) ) then print the whole line $0 after we striping the first field $1="" else : print filed#2 $2.







                share|improve this answer















                share|improve this answer



                share|improve this answer








                edited Apr 23 at 15:46


























                answered Apr 20 at 15:52









                αғsнιη

                14.8k82462




                14.8k82462






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f438972%2fusing-awk-if-value-exists-assign-to-variable%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Peggy Mitchell

                    Palaiologos

                    The Forum (Inglewood, California)