AWK: Insert copy of column in the middle of csv

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











up vote
2
down vote

favorite












Example csv:



AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH


Now i wan't a copy column 2 (BBB) and add it in front of column 3 so the file looks like:



AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH









share|improve this question

























    up vote
    2
    down vote

    favorite












    Example csv:



    AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH


    Now i wan't a copy column 2 (BBB) and add it in front of column 3 so the file looks like:



    AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH









    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      Example csv:



      AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH


      Now i wan't a copy column 2 (BBB) and add it in front of column 3 so the file looks like:



      AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH









      share|improve this question













      Example csv:



      AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH


      Now i wan't a copy column 2 (BBB) and add it in front of column 3 so the file looks like:



      AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH






      awk






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 31 at 9:08









      T-One

      536




      536




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          7
          down vote



          accepted










          $ cat test.txt
          AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH
          AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH
          AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH

          $ awk -F, '$2=$2","$21' OFS=, test.txt
          AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH
          AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH
          AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH





          share|improve this answer
















          • 1




            much cleaner than my answer :)
            – RobotJohnny
            Aug 31 at 9:15










          • In this case I think this is enough: awk '$2=$2" "$21' test.txt
            – Claes Wikner
            Sep 1 at 22:47

















          up vote
          3
          down vote













          awk 'print $1,$2,$2,$3,$4,$5,$6,$7,$8' file.csv



          Example:



           ➤ echo "AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH" | awk 'print $1,$2,$2,$3,$4,$5,$6,$7,$8'
          AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH





          share|improve this answer




















          • Thanks, that's a solution i already thought about but the real csv file has more then 50 fields so the print gets very long.
            – T-One
            Aug 31 at 9:15






          • 1




            check out @Kamaraj's answer, should do the trick for you
            – RobotJohnny
            Aug 31 at 9:16










          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%2f465964%2fawk-insert-copy-of-column-in-the-middle-of-csv%23new-answer', 'question_page');

          );

          Post as a guest






























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          7
          down vote



          accepted










          $ cat test.txt
          AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH
          AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH
          AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH

          $ awk -F, '$2=$2","$21' OFS=, test.txt
          AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH
          AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH
          AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH





          share|improve this answer
















          • 1




            much cleaner than my answer :)
            – RobotJohnny
            Aug 31 at 9:15










          • In this case I think this is enough: awk '$2=$2" "$21' test.txt
            – Claes Wikner
            Sep 1 at 22:47














          up vote
          7
          down vote



          accepted










          $ cat test.txt
          AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH
          AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH
          AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH

          $ awk -F, '$2=$2","$21' OFS=, test.txt
          AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH
          AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH
          AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH





          share|improve this answer
















          • 1




            much cleaner than my answer :)
            – RobotJohnny
            Aug 31 at 9:15










          • In this case I think this is enough: awk '$2=$2" "$21' test.txt
            – Claes Wikner
            Sep 1 at 22:47












          up vote
          7
          down vote



          accepted







          up vote
          7
          down vote



          accepted






          $ cat test.txt
          AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH
          AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH
          AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH

          $ awk -F, '$2=$2","$21' OFS=, test.txt
          AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH
          AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH
          AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH





          share|improve this answer












          $ cat test.txt
          AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH
          AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH
          AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH

          $ awk -F, '$2=$2","$21' OFS=, test.txt
          AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH
          AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH
          AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 31 at 9:11









          Kamaraj

          2,9081413




          2,9081413







          • 1




            much cleaner than my answer :)
            – RobotJohnny
            Aug 31 at 9:15










          • In this case I think this is enough: awk '$2=$2" "$21' test.txt
            – Claes Wikner
            Sep 1 at 22:47












          • 1




            much cleaner than my answer :)
            – RobotJohnny
            Aug 31 at 9:15










          • In this case I think this is enough: awk '$2=$2" "$21' test.txt
            – Claes Wikner
            Sep 1 at 22:47







          1




          1




          much cleaner than my answer :)
          – RobotJohnny
          Aug 31 at 9:15




          much cleaner than my answer :)
          – RobotJohnny
          Aug 31 at 9:15












          In this case I think this is enough: awk '$2=$2" "$21' test.txt
          – Claes Wikner
          Sep 1 at 22:47




          In this case I think this is enough: awk '$2=$2" "$21' test.txt
          – Claes Wikner
          Sep 1 at 22:47












          up vote
          3
          down vote













          awk 'print $1,$2,$2,$3,$4,$5,$6,$7,$8' file.csv



          Example:



           ➤ echo "AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH" | awk 'print $1,$2,$2,$3,$4,$5,$6,$7,$8'
          AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH





          share|improve this answer




















          • Thanks, that's a solution i already thought about but the real csv file has more then 50 fields so the print gets very long.
            – T-One
            Aug 31 at 9:15






          • 1




            check out @Kamaraj's answer, should do the trick for you
            – RobotJohnny
            Aug 31 at 9:16














          up vote
          3
          down vote













          awk 'print $1,$2,$2,$3,$4,$5,$6,$7,$8' file.csv



          Example:



           ➤ echo "AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH" | awk 'print $1,$2,$2,$3,$4,$5,$6,$7,$8'
          AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH





          share|improve this answer




















          • Thanks, that's a solution i already thought about but the real csv file has more then 50 fields so the print gets very long.
            – T-One
            Aug 31 at 9:15






          • 1




            check out @Kamaraj's answer, should do the trick for you
            – RobotJohnny
            Aug 31 at 9:16












          up vote
          3
          down vote










          up vote
          3
          down vote









          awk 'print $1,$2,$2,$3,$4,$5,$6,$7,$8' file.csv



          Example:



           ➤ echo "AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH" | awk 'print $1,$2,$2,$3,$4,$5,$6,$7,$8'
          AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH





          share|improve this answer












          awk 'print $1,$2,$2,$3,$4,$5,$6,$7,$8' file.csv



          Example:



           ➤ echo "AAA, BBB, CCC, DDD, EEE, FFF, GGG, HHH" | awk 'print $1,$2,$2,$3,$4,$5,$6,$7,$8'
          AAA, BBB, BBB, CCC, DDD, EEE, FFF, GGG, HHH






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 31 at 9:10









          RobotJohnny

          702216




          702216











          • Thanks, that's a solution i already thought about but the real csv file has more then 50 fields so the print gets very long.
            – T-One
            Aug 31 at 9:15






          • 1




            check out @Kamaraj's answer, should do the trick for you
            – RobotJohnny
            Aug 31 at 9:16
















          • Thanks, that's a solution i already thought about but the real csv file has more then 50 fields so the print gets very long.
            – T-One
            Aug 31 at 9:15






          • 1




            check out @Kamaraj's answer, should do the trick for you
            – RobotJohnny
            Aug 31 at 9:16















          Thanks, that's a solution i already thought about but the real csv file has more then 50 fields so the print gets very long.
          – T-One
          Aug 31 at 9:15




          Thanks, that's a solution i already thought about but the real csv file has more then 50 fields so the print gets very long.
          – T-One
          Aug 31 at 9:15




          1




          1




          check out @Kamaraj's answer, should do the trick for you
          – RobotJohnny
          Aug 31 at 9:16




          check out @Kamaraj's answer, should do the trick for you
          – RobotJohnny
          Aug 31 at 9:16

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f465964%2fawk-insert-copy-of-column-in-the-middle-of-csv%23new-answer', 'question_page');

          );

          Post as a guest













































































          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