How to read a file with two columns into a script so that each line read as two variablesLoop over a file and...

How to deal with moral/legal subjects in writing?

How can characters/players identify that a polymorphed dragon is a dragon?

Employers keep telling me my college isn't good enough - is there any way to fix this?

When an electron changes its spin, or any other intrinsic property, is it still the same electron?

Confirming the Identity of a (Friendly) Reviewer After the Reviews

Can the Mage Hand cantrip be used to trip an enemy who is running away?

How can I effectively communicate to recruiters that a phone call is not possible?

Would dual wielding daggers be a viable choice for a covert bodyguard?

How do we handle pauses in a dialogue?

Are there any balance issues in allowing two half-feats to be taken without the Ability Score Increase instead of a feat?

Salt, pepper, herbs and spices

Does the Pole of Angling's command word require an action?

Shortest hex dumping program

What specific instant in time in the MCU has been depicted the most times?

Print the last, middle and first character of your code

What does the phrase "head down the rat's hole" mean here?

OR-backed serious games

How would vampires avoid contracting diseases?

Why isn't pressure filtration popular compared to vacuum filtration?

Is there a strong legal guarantee that the U.S. can give to another country that it won't attack them?

How to befriend private nested class

Is "I do not want you to go nowhere" a case of "DOUBLE-NEGATIVES" as claimed by Grammarly?

How can I truly shut down ssh server?

How do you move up one folder in Finder?



How to read a file with two columns into a script so that each line read as two variables


Loop over a file and read values from two columns into variablesReformat each two-line sequence into two columnsRead non-bash variables from a file into bash scriptHow to Save variables in a script that can be shared between two runs of awk against the same input file in the script?Read columns from file into separate variablesHow to take the values from two columns in a txt file and match them to values in anotherScript comparing two files, match two strings anywhere on lineusing “for” loop read a file line by line continuously by considering each line as a inputscript to parse file for two consecutive lines of unequal lengthGrep 3 Capital Letters and Digits into Two Variables






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







0















I have a file that has two columns like such.



cat test.txt

100 2007
FFF 1993
7E7 1994
4BB 1995


I need to input each of the lines into a script, and have the two columns assigned as separate variables so I can run them through a different loop, and then move onto the next line.



I have a long list of files that I need to perform a find and replace function for each of the pairs. I am converting sets to year which I already have mapped out.



My script will look something like this.



SET=$1
YEAR=$2

for i in `ls` ; do
sed -i "" 's/"$1"/"$2"/g' $i;
done


I cant figure out how to have the test.txt loop the script with the new lines.










share|improve this question







New contributor



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


























    0















    I have a file that has two columns like such.



    cat test.txt

    100 2007
    FFF 1993
    7E7 1994
    4BB 1995


    I need to input each of the lines into a script, and have the two columns assigned as separate variables so I can run them through a different loop, and then move onto the next line.



    I have a long list of files that I need to perform a find and replace function for each of the pairs. I am converting sets to year which I already have mapped out.



    My script will look something like this.



    SET=$1
    YEAR=$2

    for i in `ls` ; do
    sed -i "" 's/"$1"/"$2"/g' $i;
    done


    I cant figure out how to have the test.txt loop the script with the new lines.










    share|improve this question







    New contributor



    maclian 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








      I have a file that has two columns like such.



      cat test.txt

      100 2007
      FFF 1993
      7E7 1994
      4BB 1995


      I need to input each of the lines into a script, and have the two columns assigned as separate variables so I can run them through a different loop, and then move onto the next line.



      I have a long list of files that I need to perform a find and replace function for each of the pairs. I am converting sets to year which I already have mapped out.



      My script will look something like this.



      SET=$1
      YEAR=$2

      for i in `ls` ; do
      sed -i "" 's/"$1"/"$2"/g' $i;
      done


      I cant figure out how to have the test.txt loop the script with the new lines.










      share|improve this question







      New contributor



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











      I have a file that has two columns like such.



      cat test.txt

      100 2007
      FFF 1993
      7E7 1994
      4BB 1995


      I need to input each of the lines into a script, and have the two columns assigned as separate variables so I can run them through a different loop, and then move onto the next line.



      I have a long list of files that I need to perform a find and replace function for each of the pairs. I am converting sets to year which I already have mapped out.



      My script will look something like this.



      SET=$1
      YEAR=$2

      for i in `ls` ; do
      sed -i "" 's/"$1"/"$2"/g' $i;
      done


      I cant figure out how to have the test.txt loop the script with the new lines.







      bash shell-script shell text-processing variable






      share|improve this question







      New contributor



      maclian 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 question







      New contributor



      maclian 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 question




      share|improve this question






      New contributor



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








      asked 1 hour ago









      maclianmaclian

      1




      1




      New contributor



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




      New contributor




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
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You can use read to split the lines based on whitespace (which it looks like your file uses). Something like this:



          while read set year; do
          DoStuffWith "$set" "$year"
          done <test.txt


          Other notes:




          • Note that I used lowercase variable names in the above example. There are a bunch of all-caps names with special meanings, and the safest way to avoid conflicts with them is to stick to lower- or mixed-case variables (unless you want the special meaning).



          • Don't iterate over the output of ls, just use for i in *; do. And double-quote the reference to $i to avoid weird parsing in some cases.



            Except you don't need to do that, sed can iterate over files all by itself if you use sed -i "" 'whatever' *.




          • In the sed command, it looks like you're trying to use variable substitutions and double-quotes inside single-quoted strings, and that doesn't work. If you don't want the double-quotes to be literally part of the search/replace strings, use this instead:



            sed -i "" "s/$set/$year/g" *


            If you do want literal double-quotes in the search and replace strings, escape them:



            sed -i "" "s/"$set"/"$year"/g" *



          • It looks like you're planning to loop through the file, and for each line loop over every file replacing that one string. Why not have a single run do all the replacements? That is, rather than running sed with the command s/100/2007/g, and then again with s/FFF/1993/g, and then again... why not run it just once with the command s/100/2007/g; s/FFF/1993/g; s/7E7/1994/g; ...?



            You'd need to build the sed command in a loop, something like this:



            sedCommand=""
            while read set year; do
            sedCommand+="s/$set/$year/g; "
            done <test.txt

            sed -i "" "$sedCommand" *



          • Finally, global replacements like this worry me. Are you sure that "100" doesn't occur in any file except those instances where it should be replaced by "2007"? Does that include the test.txt file?



            Do you have a backup of all of these files? You really should before turning something like this loose on anything you don't want to lose.








          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',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });






            maclian 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%2funix.stackexchange.com%2fquestions%2f529304%2fhow-to-read-a-file-with-two-columns-into-a-script-so-that-each-line-read-as-two%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            You can use read to split the lines based on whitespace (which it looks like your file uses). Something like this:



            while read set year; do
            DoStuffWith "$set" "$year"
            done <test.txt


            Other notes:




            • Note that I used lowercase variable names in the above example. There are a bunch of all-caps names with special meanings, and the safest way to avoid conflicts with them is to stick to lower- or mixed-case variables (unless you want the special meaning).



            • Don't iterate over the output of ls, just use for i in *; do. And double-quote the reference to $i to avoid weird parsing in some cases.



              Except you don't need to do that, sed can iterate over files all by itself if you use sed -i "" 'whatever' *.




            • In the sed command, it looks like you're trying to use variable substitutions and double-quotes inside single-quoted strings, and that doesn't work. If you don't want the double-quotes to be literally part of the search/replace strings, use this instead:



              sed -i "" "s/$set/$year/g" *


              If you do want literal double-quotes in the search and replace strings, escape them:



              sed -i "" "s/"$set"/"$year"/g" *



            • It looks like you're planning to loop through the file, and for each line loop over every file replacing that one string. Why not have a single run do all the replacements? That is, rather than running sed with the command s/100/2007/g, and then again with s/FFF/1993/g, and then again... why not run it just once with the command s/100/2007/g; s/FFF/1993/g; s/7E7/1994/g; ...?



              You'd need to build the sed command in a loop, something like this:



              sedCommand=""
              while read set year; do
              sedCommand+="s/$set/$year/g; "
              done <test.txt

              sed -i "" "$sedCommand" *



            • Finally, global replacements like this worry me. Are you sure that "100" doesn't occur in any file except those instances where it should be replaced by "2007"? Does that include the test.txt file?



              Do you have a backup of all of these files? You really should before turning something like this loose on anything you don't want to lose.








            share|improve this answer




























              1














              You can use read to split the lines based on whitespace (which it looks like your file uses). Something like this:



              while read set year; do
              DoStuffWith "$set" "$year"
              done <test.txt


              Other notes:




              • Note that I used lowercase variable names in the above example. There are a bunch of all-caps names with special meanings, and the safest way to avoid conflicts with them is to stick to lower- or mixed-case variables (unless you want the special meaning).



              • Don't iterate over the output of ls, just use for i in *; do. And double-quote the reference to $i to avoid weird parsing in some cases.



                Except you don't need to do that, sed can iterate over files all by itself if you use sed -i "" 'whatever' *.




              • In the sed command, it looks like you're trying to use variable substitutions and double-quotes inside single-quoted strings, and that doesn't work. If you don't want the double-quotes to be literally part of the search/replace strings, use this instead:



                sed -i "" "s/$set/$year/g" *


                If you do want literal double-quotes in the search and replace strings, escape them:



                sed -i "" "s/"$set"/"$year"/g" *



              • It looks like you're planning to loop through the file, and for each line loop over every file replacing that one string. Why not have a single run do all the replacements? That is, rather than running sed with the command s/100/2007/g, and then again with s/FFF/1993/g, and then again... why not run it just once with the command s/100/2007/g; s/FFF/1993/g; s/7E7/1994/g; ...?



                You'd need to build the sed command in a loop, something like this:



                sedCommand=""
                while read set year; do
                sedCommand+="s/$set/$year/g; "
                done <test.txt

                sed -i "" "$sedCommand" *



              • Finally, global replacements like this worry me. Are you sure that "100" doesn't occur in any file except those instances where it should be replaced by "2007"? Does that include the test.txt file?



                Do you have a backup of all of these files? You really should before turning something like this loose on anything you don't want to lose.








              share|improve this answer


























                1












                1








                1







                You can use read to split the lines based on whitespace (which it looks like your file uses). Something like this:



                while read set year; do
                DoStuffWith "$set" "$year"
                done <test.txt


                Other notes:




                • Note that I used lowercase variable names in the above example. There are a bunch of all-caps names with special meanings, and the safest way to avoid conflicts with them is to stick to lower- or mixed-case variables (unless you want the special meaning).



                • Don't iterate over the output of ls, just use for i in *; do. And double-quote the reference to $i to avoid weird parsing in some cases.



                  Except you don't need to do that, sed can iterate over files all by itself if you use sed -i "" 'whatever' *.




                • In the sed command, it looks like you're trying to use variable substitutions and double-quotes inside single-quoted strings, and that doesn't work. If you don't want the double-quotes to be literally part of the search/replace strings, use this instead:



                  sed -i "" "s/$set/$year/g" *


                  If you do want literal double-quotes in the search and replace strings, escape them:



                  sed -i "" "s/"$set"/"$year"/g" *



                • It looks like you're planning to loop through the file, and for each line loop over every file replacing that one string. Why not have a single run do all the replacements? That is, rather than running sed with the command s/100/2007/g, and then again with s/FFF/1993/g, and then again... why not run it just once with the command s/100/2007/g; s/FFF/1993/g; s/7E7/1994/g; ...?



                  You'd need to build the sed command in a loop, something like this:



                  sedCommand=""
                  while read set year; do
                  sedCommand+="s/$set/$year/g; "
                  done <test.txt

                  sed -i "" "$sedCommand" *



                • Finally, global replacements like this worry me. Are you sure that "100" doesn't occur in any file except those instances where it should be replaced by "2007"? Does that include the test.txt file?



                  Do you have a backup of all of these files? You really should before turning something like this loose on anything you don't want to lose.








                share|improve this answer













                You can use read to split the lines based on whitespace (which it looks like your file uses). Something like this:



                while read set year; do
                DoStuffWith "$set" "$year"
                done <test.txt


                Other notes:




                • Note that I used lowercase variable names in the above example. There are a bunch of all-caps names with special meanings, and the safest way to avoid conflicts with them is to stick to lower- or mixed-case variables (unless you want the special meaning).



                • Don't iterate over the output of ls, just use for i in *; do. And double-quote the reference to $i to avoid weird parsing in some cases.



                  Except you don't need to do that, sed can iterate over files all by itself if you use sed -i "" 'whatever' *.




                • In the sed command, it looks like you're trying to use variable substitutions and double-quotes inside single-quoted strings, and that doesn't work. If you don't want the double-quotes to be literally part of the search/replace strings, use this instead:



                  sed -i "" "s/$set/$year/g" *


                  If you do want literal double-quotes in the search and replace strings, escape them:



                  sed -i "" "s/"$set"/"$year"/g" *



                • It looks like you're planning to loop through the file, and for each line loop over every file replacing that one string. Why not have a single run do all the replacements? That is, rather than running sed with the command s/100/2007/g, and then again with s/FFF/1993/g, and then again... why not run it just once with the command s/100/2007/g; s/FFF/1993/g; s/7E7/1994/g; ...?



                  You'd need to build the sed command in a loop, something like this:



                  sedCommand=""
                  while read set year; do
                  sedCommand+="s/$set/$year/g; "
                  done <test.txt

                  sed -i "" "$sedCommand" *



                • Finally, global replacements like this worry me. Are you sure that "100" doesn't occur in any file except those instances where it should be replaced by "2007"? Does that include the test.txt file?



                  Do you have a backup of all of these files? You really should before turning something like this loose on anything you don't want to lose.









                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 48 mins ago









                Gordon DavissonGordon Davisson

                1,4458 silver badges7 bronze badges




                1,4458 silver badges7 bronze badges






















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










                    draft saved

                    draft discarded


















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













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












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
















                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f529304%2fhow-to-read-a-file-with-two-columns-into-a-script-so-that-each-line-read-as-two%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

                    Hudson River Historic District Contents Geography History The district today Aesthetics Cultural...

                    The number designs the writing. Feandra Aversely Definition: The act of ingrafting a sprig or shoot of one...

                    Ayherre Geografie Demografie Externe links Navigatiemenu43° 23′ NB, 1° 15′ WL43° 23′ NB, 1°...