Split and concatenate (create command line arguments from input file)

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











up vote
1
down vote

favorite












How to concatenate text from file of lines in format:



line1
line2
...


to get results like



-o line1:1 -o line2:1 ...


I found solution how to concatenate with a separator like this:



ds=`cat list.txt`
$ds//$'n'/','


But I can't figure out how to add prefix to each entry.










share|improve this question









New contributor




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















  • 2




    For this task you should use sed or awk
    – mrc02_kr
    4 hours ago










  • @mrc02_kr Be a bit careful with your edits. The result in this question was never delimited by newlines in the original. I've fixed it.
    – Kusalananda
    2 hours ago















up vote
1
down vote

favorite












How to concatenate text from file of lines in format:



line1
line2
...


to get results like



-o line1:1 -o line2:1 ...


I found solution how to concatenate with a separator like this:



ds=`cat list.txt`
$ds//$'n'/','


But I can't figure out how to add prefix to each entry.










share|improve this question









New contributor




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















  • 2




    For this task you should use sed or awk
    – mrc02_kr
    4 hours ago










  • @mrc02_kr Be a bit careful with your edits. The result in this question was never delimited by newlines in the original. I've fixed it.
    – Kusalananda
    2 hours ago













up vote
1
down vote

favorite









up vote
1
down vote

favorite











How to concatenate text from file of lines in format:



line1
line2
...


to get results like



-o line1:1 -o line2:1 ...


I found solution how to concatenate with a separator like this:



ds=`cat list.txt`
$ds//$'n'/','


But I can't figure out how to add prefix to each entry.










share|improve this question









New contributor




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











How to concatenate text from file of lines in format:



line1
line2
...


to get results like



-o line1:1 -o line2:1 ...


I found solution how to concatenate with a separator like this:



ds=`cat list.txt`
$ds//$'n'/','


But I can't figure out how to add prefix to each entry.







linux shell-script split






share|improve this question









New contributor




wikisky 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




wikisky 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








edited 2 hours ago









Kusalananda

110k15214339




110k15214339






New contributor




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









asked 5 hours ago









wikisky

82




82




New contributor




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





New contributor





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






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







  • 2




    For this task you should use sed or awk
    – mrc02_kr
    4 hours ago










  • @mrc02_kr Be a bit careful with your edits. The result in this question was never delimited by newlines in the original. I've fixed it.
    – Kusalananda
    2 hours ago













  • 2




    For this task you should use sed or awk
    – mrc02_kr
    4 hours ago










  • @mrc02_kr Be a bit careful with your edits. The result in this question was never delimited by newlines in the original. I've fixed it.
    – Kusalananda
    2 hours ago








2




2




For this task you should use sed or awk
– mrc02_kr
4 hours ago




For this task you should use sed or awk
– mrc02_kr
4 hours ago












@mrc02_kr Be a bit careful with your edits. The result in this question was never delimited by newlines in the original. I've fixed it.
– Kusalananda
2 hours ago





@mrc02_kr Be a bit careful with your edits. The result in this question was never delimited by newlines in the original. I've fixed it.
– Kusalananda
2 hours ago











3 Answers
3






active

oldest

votes

















up vote
3
down vote



accepted










This depends on what you want to do with the string that you create. It looks like a set of command line options, so I'm going to assume that you want to use it as such together with some utility called util.



Here's a solution for /bin/sh:



#!/bin/sh

listfile=$1

set --
while IFS= read -r line; do
set -- "$@" -o "$line:1"
done <$listfile

util "$@"


This reads from the file given on the command line of the script and for each line read from that file, it sets the positional parameters to include -o and LINE:1 where LINE is the line read from the file.



After reading all the lines, it calls util with the constructed list of command line arguments. By using "$@" (with the double quotes) we ensure that each individual item in the constructed list of arguments is individually quoted.



With bash and using a bash array to hold the command line arguments that we create:



#!/bin/bash

listfile=$1

while IFS= read -r line; do
args+=( -o "$line:1" )
done <$listfile

util "$args[@]"


In both the examples above, the quoting is important. Likewise is the fact that we create an array of separate items (each -o and each LINE:1 are items in the list). Another way to do it would have been to create a single string -o LINE1:1 -o LINE2:1 etc., but this would have been interpreted as one single argument if used as util "$string" and would have undergone word splitting and filename globbing if used as util $string (this would have not worked if any line in the input file contained spaces, tabs or filename globbing characters).



Both scripts above would be used as



$ ./script.sh file


where script.sh is the executable script file and file is the input file name to read from.



Related:



  • Understanding "IFS= read -r line"





share|improve this answer





























    up vote
    1
    down vote













    sed 's/(.*)/-o 1:1/' file.txt | xargs


    The sed substitution reads as: replace the text in a given line with "-o the_original_text:1". Then the xargs command takes those modified lines of text and concatenates them as appropriate for command line arguments






    share|improve this answer





























      up vote
      0
      down vote













      With a recent shell (e.g. bash), try



      mapfile -t TMP <file
      TMP=($TMP[@]/%/:1)
      echo $TMP[@]/#/-o
      -o line1:1 -o line2:1 -o line3:1




      share




















        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
        );



        );






        wikisky 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%2f476478%2fsplit-and-concatenate-create-command-line-arguments-from-input-file%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



        accepted










        This depends on what you want to do with the string that you create. It looks like a set of command line options, so I'm going to assume that you want to use it as such together with some utility called util.



        Here's a solution for /bin/sh:



        #!/bin/sh

        listfile=$1

        set --
        while IFS= read -r line; do
        set -- "$@" -o "$line:1"
        done <$listfile

        util "$@"


        This reads from the file given on the command line of the script and for each line read from that file, it sets the positional parameters to include -o and LINE:1 where LINE is the line read from the file.



        After reading all the lines, it calls util with the constructed list of command line arguments. By using "$@" (with the double quotes) we ensure that each individual item in the constructed list of arguments is individually quoted.



        With bash and using a bash array to hold the command line arguments that we create:



        #!/bin/bash

        listfile=$1

        while IFS= read -r line; do
        args+=( -o "$line:1" )
        done <$listfile

        util "$args[@]"


        In both the examples above, the quoting is important. Likewise is the fact that we create an array of separate items (each -o and each LINE:1 are items in the list). Another way to do it would have been to create a single string -o LINE1:1 -o LINE2:1 etc., but this would have been interpreted as one single argument if used as util "$string" and would have undergone word splitting and filename globbing if used as util $string (this would have not worked if any line in the input file contained spaces, tabs or filename globbing characters).



        Both scripts above would be used as



        $ ./script.sh file


        where script.sh is the executable script file and file is the input file name to read from.



        Related:



        • Understanding "IFS= read -r line"





        share|improve this answer


























          up vote
          3
          down vote



          accepted










          This depends on what you want to do with the string that you create. It looks like a set of command line options, so I'm going to assume that you want to use it as such together with some utility called util.



          Here's a solution for /bin/sh:



          #!/bin/sh

          listfile=$1

          set --
          while IFS= read -r line; do
          set -- "$@" -o "$line:1"
          done <$listfile

          util "$@"


          This reads from the file given on the command line of the script and for each line read from that file, it sets the positional parameters to include -o and LINE:1 where LINE is the line read from the file.



          After reading all the lines, it calls util with the constructed list of command line arguments. By using "$@" (with the double quotes) we ensure that each individual item in the constructed list of arguments is individually quoted.



          With bash and using a bash array to hold the command line arguments that we create:



          #!/bin/bash

          listfile=$1

          while IFS= read -r line; do
          args+=( -o "$line:1" )
          done <$listfile

          util "$args[@]"


          In both the examples above, the quoting is important. Likewise is the fact that we create an array of separate items (each -o and each LINE:1 are items in the list). Another way to do it would have been to create a single string -o LINE1:1 -o LINE2:1 etc., but this would have been interpreted as one single argument if used as util "$string" and would have undergone word splitting and filename globbing if used as util $string (this would have not worked if any line in the input file contained spaces, tabs or filename globbing characters).



          Both scripts above would be used as



          $ ./script.sh file


          where script.sh is the executable script file and file is the input file name to read from.



          Related:



          • Understanding "IFS= read -r line"





          share|improve this answer
























            up vote
            3
            down vote



            accepted







            up vote
            3
            down vote



            accepted






            This depends on what you want to do with the string that you create. It looks like a set of command line options, so I'm going to assume that you want to use it as such together with some utility called util.



            Here's a solution for /bin/sh:



            #!/bin/sh

            listfile=$1

            set --
            while IFS= read -r line; do
            set -- "$@" -o "$line:1"
            done <$listfile

            util "$@"


            This reads from the file given on the command line of the script and for each line read from that file, it sets the positional parameters to include -o and LINE:1 where LINE is the line read from the file.



            After reading all the lines, it calls util with the constructed list of command line arguments. By using "$@" (with the double quotes) we ensure that each individual item in the constructed list of arguments is individually quoted.



            With bash and using a bash array to hold the command line arguments that we create:



            #!/bin/bash

            listfile=$1

            while IFS= read -r line; do
            args+=( -o "$line:1" )
            done <$listfile

            util "$args[@]"


            In both the examples above, the quoting is important. Likewise is the fact that we create an array of separate items (each -o and each LINE:1 are items in the list). Another way to do it would have been to create a single string -o LINE1:1 -o LINE2:1 etc., but this would have been interpreted as one single argument if used as util "$string" and would have undergone word splitting and filename globbing if used as util $string (this would have not worked if any line in the input file contained spaces, tabs or filename globbing characters).



            Both scripts above would be used as



            $ ./script.sh file


            where script.sh is the executable script file and file is the input file name to read from.



            Related:



            • Understanding "IFS= read -r line"





            share|improve this answer














            This depends on what you want to do with the string that you create. It looks like a set of command line options, so I'm going to assume that you want to use it as such together with some utility called util.



            Here's a solution for /bin/sh:



            #!/bin/sh

            listfile=$1

            set --
            while IFS= read -r line; do
            set -- "$@" -o "$line:1"
            done <$listfile

            util "$@"


            This reads from the file given on the command line of the script and for each line read from that file, it sets the positional parameters to include -o and LINE:1 where LINE is the line read from the file.



            After reading all the lines, it calls util with the constructed list of command line arguments. By using "$@" (with the double quotes) we ensure that each individual item in the constructed list of arguments is individually quoted.



            With bash and using a bash array to hold the command line arguments that we create:



            #!/bin/bash

            listfile=$1

            while IFS= read -r line; do
            args+=( -o "$line:1" )
            done <$listfile

            util "$args[@]"


            In both the examples above, the quoting is important. Likewise is the fact that we create an array of separate items (each -o and each LINE:1 are items in the list). Another way to do it would have been to create a single string -o LINE1:1 -o LINE2:1 etc., but this would have been interpreted as one single argument if used as util "$string" and would have undergone word splitting and filename globbing if used as util $string (this would have not worked if any line in the input file contained spaces, tabs or filename globbing characters).



            Both scripts above would be used as



            $ ./script.sh file


            where script.sh is the executable script file and file is the input file name to read from.



            Related:



            • Understanding "IFS= read -r line"






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 4 hours ago

























            answered 4 hours ago









            Kusalananda

            110k15214339




            110k15214339






















                up vote
                1
                down vote













                sed 's/(.*)/-o 1:1/' file.txt | xargs


                The sed substitution reads as: replace the text in a given line with "-o the_original_text:1". Then the xargs command takes those modified lines of text and concatenates them as appropriate for command line arguments






                share|improve this answer


























                  up vote
                  1
                  down vote













                  sed 's/(.*)/-o 1:1/' file.txt | xargs


                  The sed substitution reads as: replace the text in a given line with "-o the_original_text:1". Then the xargs command takes those modified lines of text and concatenates them as appropriate for command line arguments






                  share|improve this answer
























                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    sed 's/(.*)/-o 1:1/' file.txt | xargs


                    The sed substitution reads as: replace the text in a given line with "-o the_original_text:1". Then the xargs command takes those modified lines of text and concatenates them as appropriate for command line arguments






                    share|improve this answer














                    sed 's/(.*)/-o 1:1/' file.txt | xargs


                    The sed substitution reads as: replace the text in a given line with "-o the_original_text:1". Then the xargs command takes those modified lines of text and concatenates them as appropriate for command line arguments







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 15 mins ago









                    RudiC

                    2,242110




                    2,242110










                    answered 4 hours ago









                    diametralpitch

                    20414




                    20414




















                        up vote
                        0
                        down vote













                        With a recent shell (e.g. bash), try



                        mapfile -t TMP <file
                        TMP=($TMP[@]/%/:1)
                        echo $TMP[@]/#/-o
                        -o line1:1 -o line2:1 -o line3:1




                        share
























                          up vote
                          0
                          down vote













                          With a recent shell (e.g. bash), try



                          mapfile -t TMP <file
                          TMP=($TMP[@]/%/:1)
                          echo $TMP[@]/#/-o
                          -o line1:1 -o line2:1 -o line3:1




                          share






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            With a recent shell (e.g. bash), try



                            mapfile -t TMP <file
                            TMP=($TMP[@]/%/:1)
                            echo $TMP[@]/#/-o
                            -o line1:1 -o line2:1 -o line3:1




                            share












                            With a recent shell (e.g. bash), try



                            mapfile -t TMP <file
                            TMP=($TMP[@]/%/:1)
                            echo $TMP[@]/#/-o
                            -o line1:1 -o line2:1 -o line3:1





                            share











                            share


                            share










                            answered 4 mins ago









                            RudiC

                            2,242110




                            2,242110




















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









                                 

                                draft saved


                                draft discarded


















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












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











                                wikisky 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%2f476478%2fsplit-and-concatenate-create-command-line-arguments-from-input-file%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Comments

                                Popular posts from this blog

                                What does second last employer means? [closed]

                                List of Gilmore Girls characters

                                One-line joke