Read line from file and process somethingWhat is the difference between touch file and > file?How to...

Is there really no use for MD5 anymore?

Pre-plastic human skin alternative

Is Diceware more secure than a long passphrase?

Could the terminal length of components like resistors be reduced?

How do I deal with a coworker that keeps asking to make small superficial changes to a report, and it is seriously triggering my anxiety?

Relationship between strut and baselineskip

How to have a sharp product image?

Does tea made with boiling water cool faster than tea made with boiled (but still hot) water?

What does ゆーか mean?

How do I check if a string is entirely made of the same substring?

Was there a shared-world project before "Thieves World"?

Does a large simulator bay have standard public address announcements?

Is there a way to generate a list of distinct numbers such that no two subsets ever have an equal sum?

Mistake in years of experience in resume?

Aligning equation numbers vertically

Why must Chinese maps be obfuscated?

Can an Area of Effect spell cast outside a Prismatic Wall extend inside it?

A ​Note ​on ​N!

Contradiction proof for inequality of P and NP?

Can SQL Server create collisions in system generated constraint names?

Phrase for the opposite of "foolproof"

a sore throat vs a strep throat vs strep throat

Why didn't the Space Shuttle bounce back into space as many times as possible so as to lose a lot of kinetic energy up there?

How to display Aura JS Errors Lightning Out



Read line from file and process something


What is the difference between touch file and > file?How to replace a string of text with input from another filePrint only one line at a time from text fileReading lines from a text file and creating a text file for each name on each lineHow to squash all the contents of a multi-line file onto one line?Why does `read -r` still only get the first word of each line?Remove the first part of each line of a text fileRead from text1 and write to text2How to read line by line multiple filesSelect the remaining text in a line from a log fileread input from a file and append that values to variables using shell






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







3















There are multiple lines in my input file but my output file Token.csv only has one output line like this:



4TF16B7GA129E
4TF16B7GA129S
4TF16B7GA129D
4TF16B7GA129X


How to process every line?



My Code:



epoch=$(date -d "`date`" +"%s")
StringCsv="/home/Desktop/TokenGenScript/SerialNos.csv"
StringToken=b5242a2d7973c1aca3723c834ba0d239

while IFS=$'n' read -r line || [ -n "$line" ]
do
j=$line
serial=${j}:${epoch}:${StringToken}
echo "$serial"|sha256sum > Token.csv
done < "$StringCsv"








share









New contributor




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
















  • 1





    and your code is not working or what is the problem? Btw, you don't need the doubled date.

    – RoVo
    17 hours ago


















3















There are multiple lines in my input file but my output file Token.csv only has one output line like this:



4TF16B7GA129E
4TF16B7GA129S
4TF16B7GA129D
4TF16B7GA129X


How to process every line?



My Code:



epoch=$(date -d "`date`" +"%s")
StringCsv="/home/Desktop/TokenGenScript/SerialNos.csv"
StringToken=b5242a2d7973c1aca3723c834ba0d239

while IFS=$'n' read -r line || [ -n "$line" ]
do
j=$line
serial=${j}:${epoch}:${StringToken}
echo "$serial"|sha256sum > Token.csv
done < "$StringCsv"








share









New contributor




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
















  • 1





    and your code is not working or what is the problem? Btw, you don't need the doubled date.

    – RoVo
    17 hours ago














3












3








3








There are multiple lines in my input file but my output file Token.csv only has one output line like this:



4TF16B7GA129E
4TF16B7GA129S
4TF16B7GA129D
4TF16B7GA129X


How to process every line?



My Code:



epoch=$(date -d "`date`" +"%s")
StringCsv="/home/Desktop/TokenGenScript/SerialNos.csv"
StringToken=b5242a2d7973c1aca3723c834ba0d239

while IFS=$'n' read -r line || [ -n "$line" ]
do
j=$line
serial=${j}:${epoch}:${StringToken}
echo "$serial"|sha256sum > Token.csv
done < "$StringCsv"








share









New contributor




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












There are multiple lines in my input file but my output file Token.csv only has one output line like this:



4TF16B7GA129E
4TF16B7GA129S
4TF16B7GA129D
4TF16B7GA129X


How to process every line?



My Code:



epoch=$(date -d "`date`" +"%s")
StringCsv="/home/Desktop/TokenGenScript/SerialNos.csv"
StringToken=b5242a2d7973c1aca3723c834ba0d239

while IFS=$'n' read -r line || [ -n "$line" ]
do
j=$line
serial=${j}:${epoch}:${StringToken}
echo "$serial"|sha256sum > Token.csv
done < "$StringCsv"






command-line bash scripts





share









New contributor




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










share









New contributor




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








share



share








edited 16 hours ago









WinEunuuchs2Unix

48.7k1198187




48.7k1198187






New contributor




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









asked 17 hours ago









Shubham AgrawalShubham Agrawal

191




191




New contributor




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





New contributor





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






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








  • 1





    and your code is not working or what is the problem? Btw, you don't need the doubled date.

    – RoVo
    17 hours ago














  • 1





    and your code is not working or what is the problem? Btw, you don't need the doubled date.

    – RoVo
    17 hours ago








1




1





and your code is not working or what is the problem? Btw, you don't need the doubled date.

– RoVo
17 hours ago





and your code is not working or what is the problem? Btw, you don't need the doubled date.

– RoVo
17 hours ago










4 Answers
4






active

oldest

votes


















4














Put the output redirection on the entire loop, not just the sha256sum command. Every time you redirect, you're recreating the output file from scratch. This will just create it once, and write to it repeatedly within the loop.



while IFS=$'n' read -r line || [ -n "$line" ]
do
j=$line
serial=${j}:${epoch}:${StringToken}
echo "$serial"|sha256sum
done < "$StringCsv" > Token.csv





share|improve this answer































    4














    You are writing to a file in a loop with this command:



    echo "$serial"|sha256sum > Token.csv


    However each time you loop you are erasing the file and writing a new entry. What you want to do is append (add to) the file each time you loop with this command:



    echo "$serial"|sha256sum >> Token.csv


    A single > tells bash to erase the file Token.csv and write the contents. A double >> tells bash to add to the end of the file.





    The bash script would now look like this:



    !/bin/bash
    epoch=$(date -d "`date`" +"%s")
    StringCsv="/home/Desktop/TokenGenScript/SerialNos.csv"
    StringToken=b5242a2d7973c1aca3723c834ba0d239

    > Token.csv # Empty file from last run

    while IFS=$'n' read -r line || [ -n "$line" ]
    do
    j=$line
    serial=${j}:${epoch}:${StringToken}
    echo "$serial"|sha256sum >> Token.csv # Append new record to end
    done < "$StringCsv"


    There are two ways to empty the file > Token.csv as used above and touch Token.csv. See:




    • What is the difference between touch file and > file?






    share|improve this answer


























    • There should probably be an explicit removal/truncation of Token.csv prior to the loop, to assure this run doesn't append to any existing file contents.

      – Monty Harder
      13 hours ago



















    0














    Use the command below for reading n lines from file:



    head -n 1 filename


    to write it to variable use this:



    var=$(head -n 1 filename);


    Or you can read nth line from file:



    sed -n '2p' filename


    The comand above will return second line of file. For your example you can use this:
    sed -n $i'p' filename
    where i is index.



    BUT, for your code you need an index that will increase every iteration.






    share|improve this answer










    New contributor




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




























      0














      Taking Why is using a shell loop to process text considered bad practice? way too seriously, and using GNU Awk's getline from a Coprocess:



      gawk -v stringToken=b5242a2d7973c1aca3723c834ba0d239 '
      BEGIN{cmd="sha256sum"; s = systime()}
      {
      print $0 s stringToken |& cmd; close(cmd,"to");
      cmd |& getline; close(cmd,"from")
      } 1
      ' SerialNos.csv > Token.csv





      share|improve this answer
























        Your Answer








        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "89"
        };
        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: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        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
        });


        }
        });






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










        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1138318%2fread-line-from-file-and-process-something%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        4














        Put the output redirection on the entire loop, not just the sha256sum command. Every time you redirect, you're recreating the output file from scratch. This will just create it once, and write to it repeatedly within the loop.



        while IFS=$'n' read -r line || [ -n "$line" ]
        do
        j=$line
        serial=${j}:${epoch}:${StringToken}
        echo "$serial"|sha256sum
        done < "$StringCsv" > Token.csv





        share|improve this answer




























          4














          Put the output redirection on the entire loop, not just the sha256sum command. Every time you redirect, you're recreating the output file from scratch. This will just create it once, and write to it repeatedly within the loop.



          while IFS=$'n' read -r line || [ -n "$line" ]
          do
          j=$line
          serial=${j}:${epoch}:${StringToken}
          echo "$serial"|sha256sum
          done < "$StringCsv" > Token.csv





          share|improve this answer


























            4












            4








            4







            Put the output redirection on the entire loop, not just the sha256sum command. Every time you redirect, you're recreating the output file from scratch. This will just create it once, and write to it repeatedly within the loop.



            while IFS=$'n' read -r line || [ -n "$line" ]
            do
            j=$line
            serial=${j}:${epoch}:${StringToken}
            echo "$serial"|sha256sum
            done < "$StringCsv" > Token.csv





            share|improve this answer













            Put the output redirection on the entire loop, not just the sha256sum command. Every time you redirect, you're recreating the output file from scratch. This will just create it once, and write to it repeatedly within the loop.



            while IFS=$'n' read -r line || [ -n "$line" ]
            do
            j=$line
            serial=${j}:${epoch}:${StringToken}
            echo "$serial"|sha256sum
            done < "$StringCsv" > Token.csv






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 12 hours ago









            BarmarBarmar

            1756




            1756

























                4














                You are writing to a file in a loop with this command:



                echo "$serial"|sha256sum > Token.csv


                However each time you loop you are erasing the file and writing a new entry. What you want to do is append (add to) the file each time you loop with this command:



                echo "$serial"|sha256sum >> Token.csv


                A single > tells bash to erase the file Token.csv and write the contents. A double >> tells bash to add to the end of the file.





                The bash script would now look like this:



                !/bin/bash
                epoch=$(date -d "`date`" +"%s")
                StringCsv="/home/Desktop/TokenGenScript/SerialNos.csv"
                StringToken=b5242a2d7973c1aca3723c834ba0d239

                > Token.csv # Empty file from last run

                while IFS=$'n' read -r line || [ -n "$line" ]
                do
                j=$line
                serial=${j}:${epoch}:${StringToken}
                echo "$serial"|sha256sum >> Token.csv # Append new record to end
                done < "$StringCsv"


                There are two ways to empty the file > Token.csv as used above and touch Token.csv. See:




                • What is the difference between touch file and > file?






                share|improve this answer


























                • There should probably be an explicit removal/truncation of Token.csv prior to the loop, to assure this run doesn't append to any existing file contents.

                  – Monty Harder
                  13 hours ago
















                4














                You are writing to a file in a loop with this command:



                echo "$serial"|sha256sum > Token.csv


                However each time you loop you are erasing the file and writing a new entry. What you want to do is append (add to) the file each time you loop with this command:



                echo "$serial"|sha256sum >> Token.csv


                A single > tells bash to erase the file Token.csv and write the contents. A double >> tells bash to add to the end of the file.





                The bash script would now look like this:



                !/bin/bash
                epoch=$(date -d "`date`" +"%s")
                StringCsv="/home/Desktop/TokenGenScript/SerialNos.csv"
                StringToken=b5242a2d7973c1aca3723c834ba0d239

                > Token.csv # Empty file from last run

                while IFS=$'n' read -r line || [ -n "$line" ]
                do
                j=$line
                serial=${j}:${epoch}:${StringToken}
                echo "$serial"|sha256sum >> Token.csv # Append new record to end
                done < "$StringCsv"


                There are two ways to empty the file > Token.csv as used above and touch Token.csv. See:




                • What is the difference between touch file and > file?






                share|improve this answer


























                • There should probably be an explicit removal/truncation of Token.csv prior to the loop, to assure this run doesn't append to any existing file contents.

                  – Monty Harder
                  13 hours ago














                4












                4








                4







                You are writing to a file in a loop with this command:



                echo "$serial"|sha256sum > Token.csv


                However each time you loop you are erasing the file and writing a new entry. What you want to do is append (add to) the file each time you loop with this command:



                echo "$serial"|sha256sum >> Token.csv


                A single > tells bash to erase the file Token.csv and write the contents. A double >> tells bash to add to the end of the file.





                The bash script would now look like this:



                !/bin/bash
                epoch=$(date -d "`date`" +"%s")
                StringCsv="/home/Desktop/TokenGenScript/SerialNos.csv"
                StringToken=b5242a2d7973c1aca3723c834ba0d239

                > Token.csv # Empty file from last run

                while IFS=$'n' read -r line || [ -n "$line" ]
                do
                j=$line
                serial=${j}:${epoch}:${StringToken}
                echo "$serial"|sha256sum >> Token.csv # Append new record to end
                done < "$StringCsv"


                There are two ways to empty the file > Token.csv as used above and touch Token.csv. See:




                • What is the difference between touch file and > file?






                share|improve this answer















                You are writing to a file in a loop with this command:



                echo "$serial"|sha256sum > Token.csv


                However each time you loop you are erasing the file and writing a new entry. What you want to do is append (add to) the file each time you loop with this command:



                echo "$serial"|sha256sum >> Token.csv


                A single > tells bash to erase the file Token.csv and write the contents. A double >> tells bash to add to the end of the file.





                The bash script would now look like this:



                !/bin/bash
                epoch=$(date -d "`date`" +"%s")
                StringCsv="/home/Desktop/TokenGenScript/SerialNos.csv"
                StringToken=b5242a2d7973c1aca3723c834ba0d239

                > Token.csv # Empty file from last run

                while IFS=$'n' read -r line || [ -n "$line" ]
                do
                j=$line
                serial=${j}:${epoch}:${StringToken}
                echo "$serial"|sha256sum >> Token.csv # Append new record to end
                done < "$StringCsv"


                There are two ways to empty the file > Token.csv as used above and touch Token.csv. See:




                • What is the difference between touch file and > file?







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 2 hours ago

























                answered 16 hours ago









                WinEunuuchs2UnixWinEunuuchs2Unix

                48.7k1198187




                48.7k1198187













                • There should probably be an explicit removal/truncation of Token.csv prior to the loop, to assure this run doesn't append to any existing file contents.

                  – Monty Harder
                  13 hours ago



















                • There should probably be an explicit removal/truncation of Token.csv prior to the loop, to assure this run doesn't append to any existing file contents.

                  – Monty Harder
                  13 hours ago

















                There should probably be an explicit removal/truncation of Token.csv prior to the loop, to assure this run doesn't append to any existing file contents.

                – Monty Harder
                13 hours ago





                There should probably be an explicit removal/truncation of Token.csv prior to the loop, to assure this run doesn't append to any existing file contents.

                – Monty Harder
                13 hours ago











                0














                Use the command below for reading n lines from file:



                head -n 1 filename


                to write it to variable use this:



                var=$(head -n 1 filename);


                Or you can read nth line from file:



                sed -n '2p' filename


                The comand above will return second line of file. For your example you can use this:
                sed -n $i'p' filename
                where i is index.



                BUT, for your code you need an index that will increase every iteration.






                share|improve this answer










                New contributor




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

























                  0














                  Use the command below for reading n lines from file:



                  head -n 1 filename


                  to write it to variable use this:



                  var=$(head -n 1 filename);


                  Or you can read nth line from file:



                  sed -n '2p' filename


                  The comand above will return second line of file. For your example you can use this:
                  sed -n $i'p' filename
                  where i is index.



                  BUT, for your code you need an index that will increase every iteration.






                  share|improve this answer










                  New contributor




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























                    0












                    0








                    0







                    Use the command below for reading n lines from file:



                    head -n 1 filename


                    to write it to variable use this:



                    var=$(head -n 1 filename);


                    Or you can read nth line from file:



                    sed -n '2p' filename


                    The comand above will return second line of file. For your example you can use this:
                    sed -n $i'p' filename
                    where i is index.



                    BUT, for your code you need an index that will increase every iteration.






                    share|improve this answer










                    New contributor




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










                    Use the command below for reading n lines from file:



                    head -n 1 filename


                    to write it to variable use this:



                    var=$(head -n 1 filename);


                    Or you can read nth line from file:



                    sed -n '2p' filename


                    The comand above will return second line of file. For your example you can use this:
                    sed -n $i'p' filename
                    where i is index.



                    BUT, for your code you need an index that will increase every iteration.







                    share|improve this answer










                    New contributor




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









                    share|improve this answer



                    share|improve this answer








                    edited 11 hours ago





















                    New contributor




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









                    answered 11 hours ago









                    Vlad GavriukVlad Gavriuk

                    1113




                    1113




                    New contributor




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





                    New contributor





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






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























                        0














                        Taking Why is using a shell loop to process text considered bad practice? way too seriously, and using GNU Awk's getline from a Coprocess:



                        gawk -v stringToken=b5242a2d7973c1aca3723c834ba0d239 '
                        BEGIN{cmd="sha256sum"; s = systime()}
                        {
                        print $0 s stringToken |& cmd; close(cmd,"to");
                        cmd |& getline; close(cmd,"from")
                        } 1
                        ' SerialNos.csv > Token.csv





                        share|improve this answer




























                          0














                          Taking Why is using a shell loop to process text considered bad practice? way too seriously, and using GNU Awk's getline from a Coprocess:



                          gawk -v stringToken=b5242a2d7973c1aca3723c834ba0d239 '
                          BEGIN{cmd="sha256sum"; s = systime()}
                          {
                          print $0 s stringToken |& cmd; close(cmd,"to");
                          cmd |& getline; close(cmd,"from")
                          } 1
                          ' SerialNos.csv > Token.csv





                          share|improve this answer


























                            0












                            0








                            0







                            Taking Why is using a shell loop to process text considered bad practice? way too seriously, and using GNU Awk's getline from a Coprocess:



                            gawk -v stringToken=b5242a2d7973c1aca3723c834ba0d239 '
                            BEGIN{cmd="sha256sum"; s = systime()}
                            {
                            print $0 s stringToken |& cmd; close(cmd,"to");
                            cmd |& getline; close(cmd,"from")
                            } 1
                            ' SerialNos.csv > Token.csv





                            share|improve this answer













                            Taking Why is using a shell loop to process text considered bad practice? way too seriously, and using GNU Awk's getline from a Coprocess:



                            gawk -v stringToken=b5242a2d7973c1aca3723c834ba0d239 '
                            BEGIN{cmd="sha256sum"; s = systime()}
                            {
                            print $0 s stringToken |& cmd; close(cmd,"to");
                            cmd |& getline; close(cmd,"from")
                            } 1
                            ' SerialNos.csv > Token.csv






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 9 hours ago









                            steeldriversteeldriver

                            71.4k11115187




                            71.4k11115187






















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










                                draft saved

                                draft discarded


















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













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












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
















                                Thanks for contributing an answer to Ask Ubuntu!


                                • 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%2faskubuntu.com%2fquestions%2f1138318%2fread-line-from-file-and-process-something%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

                                Why do type traits not work with types in namespace scope?What are POD types in C++?Why can templates only be...

                                Will tsunami waves travel forever if there was no land?Why do tsunami waves begin with the water flowing away...

                                Should I use Docker or LXD?How to cache (more) data on SSD/RAM to avoid spin up?Unable to get Windows File...