Linux: Change first line of CSV file to all uppercase

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











up vote
0
down vote

favorite
1












I have a set of csv files, and for each file, the first line contain field names for a data set. Some csv files have all upper case for the field names, others have all lower case for the field names. My question is how do I change every csv file so that the first line of each file (aka the field names) displays as all uppercase strings in each column?



Examples:
Dataset1.csv



a b c
x x x


Dataset2.csv



A B C
y y y


How do I make Dataset1.csv look like the following?



A B C
x x x






share|improve this question
























    up vote
    0
    down vote

    favorite
    1












    I have a set of csv files, and for each file, the first line contain field names for a data set. Some csv files have all upper case for the field names, others have all lower case for the field names. My question is how do I change every csv file so that the first line of each file (aka the field names) displays as all uppercase strings in each column?



    Examples:
    Dataset1.csv



    a b c
    x x x


    Dataset2.csv



    A B C
    y y y


    How do I make Dataset1.csv look like the following?



    A B C
    x x x






    share|improve this question






















      up vote
      0
      down vote

      favorite
      1









      up vote
      0
      down vote

      favorite
      1






      1





      I have a set of csv files, and for each file, the first line contain field names for a data set. Some csv files have all upper case for the field names, others have all lower case for the field names. My question is how do I change every csv file so that the first line of each file (aka the field names) displays as all uppercase strings in each column?



      Examples:
      Dataset1.csv



      a b c
      x x x


      Dataset2.csv



      A B C
      y y y


      How do I make Dataset1.csv look like the following?



      A B C
      x x x






      share|improve this question












      I have a set of csv files, and for each file, the first line contain field names for a data set. Some csv files have all upper case for the field names, others have all lower case for the field names. My question is how do I change every csv file so that the first line of each file (aka the field names) displays as all uppercase strings in each column?



      Examples:
      Dataset1.csv



      a b c
      x x x


      Dataset2.csv



      A B C
      y y y


      How do I make Dataset1.csv look like the following?



      A B C
      x x x








      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 9 at 1:46









      crayfishcray

      111




      111




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          3
          down vote













          It may be done with the option to Uppercase of GNU sed. In-place with the option -i:



          sed -i '1s/.*/U&/' Dataset1.csv





          share|improve this answer





























            up vote
            1
            down vote













            We can do by using awk and GNU sed



            Method1



            awk 'NR==1(i=toupper($0));print i' Dataset1.csv ; awk 'NR >=2 print $0' Dataset1.csv


            Output



            A B C
            x x x
            z z z


            Method 2



            sed '1s/.*/U&/g' Dataset1.csv


            Output



            A B C
            x x x
            z z z





            share|improve this answer


















            • 1




              awk 'NR==1print toupper($0)NR>1' for a simplification and a single pass through the file
              – Fox
              Feb 9 at 6:39







            • 1




              @Praveen, when someone takes the time to suggest an improvement to your answer, it’s nice to thank them, but it’s nicer still to actually improve your answer accordingly. If you like code golf, awk 'NR==1$0=toupper($0)1' is shorter still ;-).
              – Stephen Kitt
              Feb 9 at 9:15






            • 1




              Let me just qualify the above, obviously you needn’t change your answer if you disagree with the improvement!
              – Stephen Kitt
              Feb 9 at 9:24










            • @StephenKitt Thanks for input . Answer which you provided is more efficient than mine
              – Praveen Kumar BS
              Feb 10 at 9:19

















            up vote
            0
            down vote













            This is a repost from stackoverflow since someone told me my question was off-topic for the site and I got the following answers:



            Considering your file ends with a newline:



            head -1 Dataset1.csv | tr '[:lower:]' '[:upper:]' > Dataset1_new.csv ; tail -$(( $(wc -l Dataset1.csv | cut -d ' ' -f 8) - 1 )) Dataset1.csv > Dataset1_new.csv


            head -1 Dataset1.csv | tr '[:lower:]' '[:upper:]': takes the first line of your file, transforms it to uppercase & outputs it on stdout



            > Dataset1_new.csv: redirects the output to a new file called Dataset1_new.csv



            tail -$(( $(wc -l Dataset1.csv | cut -d ' ' -f 8) - 1 )) Dataset1.csv: outputs the rest of the lines



            > Dataset1_new.csv: again, redirects the output to our Dataset1_new.csv file



            You can do it with GNU sed:



            $ sed -i -e '1 s/(.*)/U1/' input.csv


            You can also use awk for this purpose:



            awk -i.bak 'NR==1 print toupper($0) NR>1' Dataset1.csv


            Explanations:



            awk will take a backup of your csv file and then for the first line (NR==1) will change the whole line $0 to uppercase then for the rest of the file (NR>1) will do its default action which is printing the line.






            share|improve this answer






















            • first method is too complex, yet I see no reason to dwonvote.
              – Archemar
              Feb 9 at 6:28










            • Yea sorry I should have commented. I downvoted because as op says it's a repost from stack overflow and I'm sure this question has been answered on here before as well.
              – Jesse_b
              Feb 9 at 12:49










            • The head/tail solution can be simplified to tr '[:lower:]' '[:upper:]'; cat; < file > file.new
              – glenn jackman
              Feb 9 at 17:31










            • and note that if you use wc with data from stdin, you don't need to cut -- lines=$(wc -l < file)
              – glenn jackman
              Feb 9 at 17:33











            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%2f422953%2flinux-change-first-line-of-csv-file-to-all-uppercase%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
            3
            down vote













            It may be done with the option to Uppercase of GNU sed. In-place with the option -i:



            sed -i '1s/.*/U&/' Dataset1.csv





            share|improve this answer


























              up vote
              3
              down vote













              It may be done with the option to Uppercase of GNU sed. In-place with the option -i:



              sed -i '1s/.*/U&/' Dataset1.csv





              share|improve this answer
























                up vote
                3
                down vote










                up vote
                3
                down vote









                It may be done with the option to Uppercase of GNU sed. In-place with the option -i:



                sed -i '1s/.*/U&/' Dataset1.csv





                share|improve this answer














                It may be done with the option to Uppercase of GNU sed. In-place with the option -i:



                sed -i '1s/.*/U&/' Dataset1.csv






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Feb 9 at 18:11

























                answered Feb 9 at 3:03









                Isaac

                6,6381734




                6,6381734






















                    up vote
                    1
                    down vote













                    We can do by using awk and GNU sed



                    Method1



                    awk 'NR==1(i=toupper($0));print i' Dataset1.csv ; awk 'NR >=2 print $0' Dataset1.csv


                    Output



                    A B C
                    x x x
                    z z z


                    Method 2



                    sed '1s/.*/U&/g' Dataset1.csv


                    Output



                    A B C
                    x x x
                    z z z





                    share|improve this answer


















                    • 1




                      awk 'NR==1print toupper($0)NR>1' for a simplification and a single pass through the file
                      – Fox
                      Feb 9 at 6:39







                    • 1




                      @Praveen, when someone takes the time to suggest an improvement to your answer, it’s nice to thank them, but it’s nicer still to actually improve your answer accordingly. If you like code golf, awk 'NR==1$0=toupper($0)1' is shorter still ;-).
                      – Stephen Kitt
                      Feb 9 at 9:15






                    • 1




                      Let me just qualify the above, obviously you needn’t change your answer if you disagree with the improvement!
                      – Stephen Kitt
                      Feb 9 at 9:24










                    • @StephenKitt Thanks for input . Answer which you provided is more efficient than mine
                      – Praveen Kumar BS
                      Feb 10 at 9:19














                    up vote
                    1
                    down vote













                    We can do by using awk and GNU sed



                    Method1



                    awk 'NR==1(i=toupper($0));print i' Dataset1.csv ; awk 'NR >=2 print $0' Dataset1.csv


                    Output



                    A B C
                    x x x
                    z z z


                    Method 2



                    sed '1s/.*/U&/g' Dataset1.csv


                    Output



                    A B C
                    x x x
                    z z z





                    share|improve this answer


















                    • 1




                      awk 'NR==1print toupper($0)NR>1' for a simplification and a single pass through the file
                      – Fox
                      Feb 9 at 6:39







                    • 1




                      @Praveen, when someone takes the time to suggest an improvement to your answer, it’s nice to thank them, but it’s nicer still to actually improve your answer accordingly. If you like code golf, awk 'NR==1$0=toupper($0)1' is shorter still ;-).
                      – Stephen Kitt
                      Feb 9 at 9:15






                    • 1




                      Let me just qualify the above, obviously you needn’t change your answer if you disagree with the improvement!
                      – Stephen Kitt
                      Feb 9 at 9:24










                    • @StephenKitt Thanks for input . Answer which you provided is more efficient than mine
                      – Praveen Kumar BS
                      Feb 10 at 9:19












                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    We can do by using awk and GNU sed



                    Method1



                    awk 'NR==1(i=toupper($0));print i' Dataset1.csv ; awk 'NR >=2 print $0' Dataset1.csv


                    Output



                    A B C
                    x x x
                    z z z


                    Method 2



                    sed '1s/.*/U&/g' Dataset1.csv


                    Output



                    A B C
                    x x x
                    z z z





                    share|improve this answer














                    We can do by using awk and GNU sed



                    Method1



                    awk 'NR==1(i=toupper($0));print i' Dataset1.csv ; awk 'NR >=2 print $0' Dataset1.csv


                    Output



                    A B C
                    x x x
                    z z z


                    Method 2



                    sed '1s/.*/U&/g' Dataset1.csv


                    Output



                    A B C
                    x x x
                    z z z






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Feb 9 at 8:47









                    Kusalananda

                    103k13202318




                    103k13202318










                    answered Feb 9 at 5:33









                    Praveen Kumar BS

                    1,010128




                    1,010128







                    • 1




                      awk 'NR==1print toupper($0)NR>1' for a simplification and a single pass through the file
                      – Fox
                      Feb 9 at 6:39







                    • 1




                      @Praveen, when someone takes the time to suggest an improvement to your answer, it’s nice to thank them, but it’s nicer still to actually improve your answer accordingly. If you like code golf, awk 'NR==1$0=toupper($0)1' is shorter still ;-).
                      – Stephen Kitt
                      Feb 9 at 9:15






                    • 1




                      Let me just qualify the above, obviously you needn’t change your answer if you disagree with the improvement!
                      – Stephen Kitt
                      Feb 9 at 9:24










                    • @StephenKitt Thanks for input . Answer which you provided is more efficient than mine
                      – Praveen Kumar BS
                      Feb 10 at 9:19












                    • 1




                      awk 'NR==1print toupper($0)NR>1' for a simplification and a single pass through the file
                      – Fox
                      Feb 9 at 6:39







                    • 1




                      @Praveen, when someone takes the time to suggest an improvement to your answer, it’s nice to thank them, but it’s nicer still to actually improve your answer accordingly. If you like code golf, awk 'NR==1$0=toupper($0)1' is shorter still ;-).
                      – Stephen Kitt
                      Feb 9 at 9:15






                    • 1




                      Let me just qualify the above, obviously you needn’t change your answer if you disagree with the improvement!
                      – Stephen Kitt
                      Feb 9 at 9:24










                    • @StephenKitt Thanks for input . Answer which you provided is more efficient than mine
                      – Praveen Kumar BS
                      Feb 10 at 9:19







                    1




                    1




                    awk 'NR==1print toupper($0)NR>1' for a simplification and a single pass through the file
                    – Fox
                    Feb 9 at 6:39





                    awk 'NR==1print toupper($0)NR>1' for a simplification and a single pass through the file
                    – Fox
                    Feb 9 at 6:39





                    1




                    1




                    @Praveen, when someone takes the time to suggest an improvement to your answer, it’s nice to thank them, but it’s nicer still to actually improve your answer accordingly. If you like code golf, awk 'NR==1$0=toupper($0)1' is shorter still ;-).
                    – Stephen Kitt
                    Feb 9 at 9:15




                    @Praveen, when someone takes the time to suggest an improvement to your answer, it’s nice to thank them, but it’s nicer still to actually improve your answer accordingly. If you like code golf, awk 'NR==1$0=toupper($0)1' is shorter still ;-).
                    – Stephen Kitt
                    Feb 9 at 9:15




                    1




                    1




                    Let me just qualify the above, obviously you needn’t change your answer if you disagree with the improvement!
                    – Stephen Kitt
                    Feb 9 at 9:24




                    Let me just qualify the above, obviously you needn’t change your answer if you disagree with the improvement!
                    – Stephen Kitt
                    Feb 9 at 9:24












                    @StephenKitt Thanks for input . Answer which you provided is more efficient than mine
                    – Praveen Kumar BS
                    Feb 10 at 9:19




                    @StephenKitt Thanks for input . Answer which you provided is more efficient than mine
                    – Praveen Kumar BS
                    Feb 10 at 9:19










                    up vote
                    0
                    down vote













                    This is a repost from stackoverflow since someone told me my question was off-topic for the site and I got the following answers:



                    Considering your file ends with a newline:



                    head -1 Dataset1.csv | tr '[:lower:]' '[:upper:]' > Dataset1_new.csv ; tail -$(( $(wc -l Dataset1.csv | cut -d ' ' -f 8) - 1 )) Dataset1.csv > Dataset1_new.csv


                    head -1 Dataset1.csv | tr '[:lower:]' '[:upper:]': takes the first line of your file, transforms it to uppercase & outputs it on stdout



                    > Dataset1_new.csv: redirects the output to a new file called Dataset1_new.csv



                    tail -$(( $(wc -l Dataset1.csv | cut -d ' ' -f 8) - 1 )) Dataset1.csv: outputs the rest of the lines



                    > Dataset1_new.csv: again, redirects the output to our Dataset1_new.csv file



                    You can do it with GNU sed:



                    $ sed -i -e '1 s/(.*)/U1/' input.csv


                    You can also use awk for this purpose:



                    awk -i.bak 'NR==1 print toupper($0) NR>1' Dataset1.csv


                    Explanations:



                    awk will take a backup of your csv file and then for the first line (NR==1) will change the whole line $0 to uppercase then for the rest of the file (NR>1) will do its default action which is printing the line.






                    share|improve this answer






















                    • first method is too complex, yet I see no reason to dwonvote.
                      – Archemar
                      Feb 9 at 6:28










                    • Yea sorry I should have commented. I downvoted because as op says it's a repost from stack overflow and I'm sure this question has been answered on here before as well.
                      – Jesse_b
                      Feb 9 at 12:49










                    • The head/tail solution can be simplified to tr '[:lower:]' '[:upper:]'; cat; < file > file.new
                      – glenn jackman
                      Feb 9 at 17:31










                    • and note that if you use wc with data from stdin, you don't need to cut -- lines=$(wc -l < file)
                      – glenn jackman
                      Feb 9 at 17:33















                    up vote
                    0
                    down vote













                    This is a repost from stackoverflow since someone told me my question was off-topic for the site and I got the following answers:



                    Considering your file ends with a newline:



                    head -1 Dataset1.csv | tr '[:lower:]' '[:upper:]' > Dataset1_new.csv ; tail -$(( $(wc -l Dataset1.csv | cut -d ' ' -f 8) - 1 )) Dataset1.csv > Dataset1_new.csv


                    head -1 Dataset1.csv | tr '[:lower:]' '[:upper:]': takes the first line of your file, transforms it to uppercase & outputs it on stdout



                    > Dataset1_new.csv: redirects the output to a new file called Dataset1_new.csv



                    tail -$(( $(wc -l Dataset1.csv | cut -d ' ' -f 8) - 1 )) Dataset1.csv: outputs the rest of the lines



                    > Dataset1_new.csv: again, redirects the output to our Dataset1_new.csv file



                    You can do it with GNU sed:



                    $ sed -i -e '1 s/(.*)/U1/' input.csv


                    You can also use awk for this purpose:



                    awk -i.bak 'NR==1 print toupper($0) NR>1' Dataset1.csv


                    Explanations:



                    awk will take a backup of your csv file and then for the first line (NR==1) will change the whole line $0 to uppercase then for the rest of the file (NR>1) will do its default action which is printing the line.






                    share|improve this answer






















                    • first method is too complex, yet I see no reason to dwonvote.
                      – Archemar
                      Feb 9 at 6:28










                    • Yea sorry I should have commented. I downvoted because as op says it's a repost from stack overflow and I'm sure this question has been answered on here before as well.
                      – Jesse_b
                      Feb 9 at 12:49










                    • The head/tail solution can be simplified to tr '[:lower:]' '[:upper:]'; cat; < file > file.new
                      – glenn jackman
                      Feb 9 at 17:31










                    • and note that if you use wc with data from stdin, you don't need to cut -- lines=$(wc -l < file)
                      – glenn jackman
                      Feb 9 at 17:33













                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    This is a repost from stackoverflow since someone told me my question was off-topic for the site and I got the following answers:



                    Considering your file ends with a newline:



                    head -1 Dataset1.csv | tr '[:lower:]' '[:upper:]' > Dataset1_new.csv ; tail -$(( $(wc -l Dataset1.csv | cut -d ' ' -f 8) - 1 )) Dataset1.csv > Dataset1_new.csv


                    head -1 Dataset1.csv | tr '[:lower:]' '[:upper:]': takes the first line of your file, transforms it to uppercase & outputs it on stdout



                    > Dataset1_new.csv: redirects the output to a new file called Dataset1_new.csv



                    tail -$(( $(wc -l Dataset1.csv | cut -d ' ' -f 8) - 1 )) Dataset1.csv: outputs the rest of the lines



                    > Dataset1_new.csv: again, redirects the output to our Dataset1_new.csv file



                    You can do it with GNU sed:



                    $ sed -i -e '1 s/(.*)/U1/' input.csv


                    You can also use awk for this purpose:



                    awk -i.bak 'NR==1 print toupper($0) NR>1' Dataset1.csv


                    Explanations:



                    awk will take a backup of your csv file and then for the first line (NR==1) will change the whole line $0 to uppercase then for the rest of the file (NR>1) will do its default action which is printing the line.






                    share|improve this answer














                    This is a repost from stackoverflow since someone told me my question was off-topic for the site and I got the following answers:



                    Considering your file ends with a newline:



                    head -1 Dataset1.csv | tr '[:lower:]' '[:upper:]' > Dataset1_new.csv ; tail -$(( $(wc -l Dataset1.csv | cut -d ' ' -f 8) - 1 )) Dataset1.csv > Dataset1_new.csv


                    head -1 Dataset1.csv | tr '[:lower:]' '[:upper:]': takes the first line of your file, transforms it to uppercase & outputs it on stdout



                    > Dataset1_new.csv: redirects the output to a new file called Dataset1_new.csv



                    tail -$(( $(wc -l Dataset1.csv | cut -d ' ' -f 8) - 1 )) Dataset1.csv: outputs the rest of the lines



                    > Dataset1_new.csv: again, redirects the output to our Dataset1_new.csv file



                    You can do it with GNU sed:



                    $ sed -i -e '1 s/(.*)/U1/' input.csv


                    You can also use awk for this purpose:



                    awk -i.bak 'NR==1 print toupper($0) NR>1' Dataset1.csv


                    Explanations:



                    awk will take a backup of your csv file and then for the first line (NR==1) will change the whole line $0 to uppercase then for the rest of the file (NR>1) will do its default action which is printing the line.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Feb 9 at 8:48









                    Kusalananda

                    103k13202318




                    103k13202318










                    answered Feb 9 at 1:50









                    crayfishcray

                    111




                    111











                    • first method is too complex, yet I see no reason to dwonvote.
                      – Archemar
                      Feb 9 at 6:28










                    • Yea sorry I should have commented. I downvoted because as op says it's a repost from stack overflow and I'm sure this question has been answered on here before as well.
                      – Jesse_b
                      Feb 9 at 12:49










                    • The head/tail solution can be simplified to tr '[:lower:]' '[:upper:]'; cat; < file > file.new
                      – glenn jackman
                      Feb 9 at 17:31










                    • and note that if you use wc with data from stdin, you don't need to cut -- lines=$(wc -l < file)
                      – glenn jackman
                      Feb 9 at 17:33

















                    • first method is too complex, yet I see no reason to dwonvote.
                      – Archemar
                      Feb 9 at 6:28










                    • Yea sorry I should have commented. I downvoted because as op says it's a repost from stack overflow and I'm sure this question has been answered on here before as well.
                      – Jesse_b
                      Feb 9 at 12:49










                    • The head/tail solution can be simplified to tr '[:lower:]' '[:upper:]'; cat; < file > file.new
                      – glenn jackman
                      Feb 9 at 17:31










                    • and note that if you use wc with data from stdin, you don't need to cut -- lines=$(wc -l < file)
                      – glenn jackman
                      Feb 9 at 17:33
















                    first method is too complex, yet I see no reason to dwonvote.
                    – Archemar
                    Feb 9 at 6:28




                    first method is too complex, yet I see no reason to dwonvote.
                    – Archemar
                    Feb 9 at 6:28












                    Yea sorry I should have commented. I downvoted because as op says it's a repost from stack overflow and I'm sure this question has been answered on here before as well.
                    – Jesse_b
                    Feb 9 at 12:49




                    Yea sorry I should have commented. I downvoted because as op says it's a repost from stack overflow and I'm sure this question has been answered on here before as well.
                    – Jesse_b
                    Feb 9 at 12:49












                    The head/tail solution can be simplified to tr '[:lower:]' '[:upper:]'; cat; < file > file.new
                    – glenn jackman
                    Feb 9 at 17:31




                    The head/tail solution can be simplified to tr '[:lower:]' '[:upper:]'; cat; < file > file.new
                    – glenn jackman
                    Feb 9 at 17:31












                    and note that if you use wc with data from stdin, you don't need to cut -- lines=$(wc -l < file)
                    – glenn jackman
                    Feb 9 at 17:33





                    and note that if you use wc with data from stdin, you don't need to cut -- lines=$(wc -l < file)
                    – glenn jackman
                    Feb 9 at 17:33













                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f422953%2flinux-change-first-line-of-csv-file-to-all-uppercase%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