How to get specific words by index in a line bash









up vote
0
down vote

favorite












I have a script that takes parameters such as:



script.sh 1 3


I want to then go through a text file and print out the first and third words from each line. I have simply no idea how to do this. If anyone could help I'd really appreciate it...



This is what I currently have:



counter=0
wordcounter=0

param=$(echo "$3" | tr , " ")

words()
for word in $1; do
for col in $param; do
if [ $wordcounter -eq $col ]; then
echo $word
fi
done
done
wordcounter=$((wordcounter + 1))


eachline() tr , " ")
for word in $newline; do
if [ $counter -gt 3 ]; then
echo "$word"
fi
counter=$((counter + 1))
done
if [ $counter -gt 0 ]; then
words "$newline"
fi
counter=$((counter + 1))


while read line; do
eachline $line
done < company/employee.txt









share|improve this question























  • what have you tried so far?
    – oguzismail
    Nov 10 at 12:40










  • I'm currently trying to make a second script that takes a line as parameter, then running each line from the file into the script.
    – Raph117
    Nov 10 at 12:42










  • In the script itself I'm setting a counter and ignoring the number of words in the first line
    – Raph117
    Nov 10 at 12:42










  • It's very very very hacky
    – Raph117
    Nov 10 at 12:42






  • 2




    Will cut -d " " -f 1,3 file do?
    – James Brown
    Nov 10 at 13:27














up vote
0
down vote

favorite












I have a script that takes parameters such as:



script.sh 1 3


I want to then go through a text file and print out the first and third words from each line. I have simply no idea how to do this. If anyone could help I'd really appreciate it...



This is what I currently have:



counter=0
wordcounter=0

param=$(echo "$3" | tr , " ")

words()
for word in $1; do
for col in $param; do
if [ $wordcounter -eq $col ]; then
echo $word
fi
done
done
wordcounter=$((wordcounter + 1))


eachline() tr , " ")
for word in $newline; do
if [ $counter -gt 3 ]; then
echo "$word"
fi
counter=$((counter + 1))
done
if [ $counter -gt 0 ]; then
words "$newline"
fi
counter=$((counter + 1))


while read line; do
eachline $line
done < company/employee.txt









share|improve this question























  • what have you tried so far?
    – oguzismail
    Nov 10 at 12:40










  • I'm currently trying to make a second script that takes a line as parameter, then running each line from the file into the script.
    – Raph117
    Nov 10 at 12:42










  • In the script itself I'm setting a counter and ignoring the number of words in the first line
    – Raph117
    Nov 10 at 12:42










  • It's very very very hacky
    – Raph117
    Nov 10 at 12:42






  • 2




    Will cut -d " " -f 1,3 file do?
    – James Brown
    Nov 10 at 13:27












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a script that takes parameters such as:



script.sh 1 3


I want to then go through a text file and print out the first and third words from each line. I have simply no idea how to do this. If anyone could help I'd really appreciate it...



This is what I currently have:



counter=0
wordcounter=0

param=$(echo "$3" | tr , " ")

words()
for word in $1; do
for col in $param; do
if [ $wordcounter -eq $col ]; then
echo $word
fi
done
done
wordcounter=$((wordcounter + 1))


eachline() tr , " ")
for word in $newline; do
if [ $counter -gt 3 ]; then
echo "$word"
fi
counter=$((counter + 1))
done
if [ $counter -gt 0 ]; then
words "$newline"
fi
counter=$((counter + 1))


while read line; do
eachline $line
done < company/employee.txt









share|improve this question















I have a script that takes parameters such as:



script.sh 1 3


I want to then go through a text file and print out the first and third words from each line. I have simply no idea how to do this. If anyone could help I'd really appreciate it...



This is what I currently have:



counter=0
wordcounter=0

param=$(echo "$3" | tr , " ")

words()
for word in $1; do
for col in $param; do
if [ $wordcounter -eq $col ]; then
echo $word
fi
done
done
wordcounter=$((wordcounter + 1))


eachline() tr , " ")
for word in $newline; do
if [ $counter -gt 3 ]; then
echo "$word"
fi
counter=$((counter + 1))
done
if [ $counter -gt 0 ]; then
words "$newline"
fi
counter=$((counter + 1))


while read line; do
eachline $line
done < company/employee.txt






bash shell text






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 12:57

























asked Nov 10 at 12:11









Raph117

152110




152110











  • what have you tried so far?
    – oguzismail
    Nov 10 at 12:40










  • I'm currently trying to make a second script that takes a line as parameter, then running each line from the file into the script.
    – Raph117
    Nov 10 at 12:42










  • In the script itself I'm setting a counter and ignoring the number of words in the first line
    – Raph117
    Nov 10 at 12:42










  • It's very very very hacky
    – Raph117
    Nov 10 at 12:42






  • 2




    Will cut -d " " -f 1,3 file do?
    – James Brown
    Nov 10 at 13:27
















  • what have you tried so far?
    – oguzismail
    Nov 10 at 12:40










  • I'm currently trying to make a second script that takes a line as parameter, then running each line from the file into the script.
    – Raph117
    Nov 10 at 12:42










  • In the script itself I'm setting a counter and ignoring the number of words in the first line
    – Raph117
    Nov 10 at 12:42










  • It's very very very hacky
    – Raph117
    Nov 10 at 12:42






  • 2




    Will cut -d " " -f 1,3 file do?
    – James Brown
    Nov 10 at 13:27















what have you tried so far?
– oguzismail
Nov 10 at 12:40




what have you tried so far?
– oguzismail
Nov 10 at 12:40












I'm currently trying to make a second script that takes a line as parameter, then running each line from the file into the script.
– Raph117
Nov 10 at 12:42




I'm currently trying to make a second script that takes a line as parameter, then running each line from the file into the script.
– Raph117
Nov 10 at 12:42












In the script itself I'm setting a counter and ignoring the number of words in the first line
– Raph117
Nov 10 at 12:42




In the script itself I'm setting a counter and ignoring the number of words in the first line
– Raph117
Nov 10 at 12:42












It's very very very hacky
– Raph117
Nov 10 at 12:42




It's very very very hacky
– Raph117
Nov 10 at 12:42




2




2




Will cut -d " " -f 1,3 file do?
– James Brown
Nov 10 at 13:27




Will cut -d " " -f 1,3 file do?
– James Brown
Nov 10 at 13:27












1 Answer
1






active

oldest

votes

















up vote
3
down vote













Use awk:



$ awk 'print $1 " " $3' file


for file:



1 2 3 4 5
6 7 8 9 0


Output is:



1 3
6 8


In bash script:



#!/bin/bash

awk_command="print";
for i in "$@"; do
awk_command="$awk_command $$i " "";
done
awk_command="$awk_command";
awk "$awk_command" file


With this script you can pass any number of indexes:



For 1 and 2:



$ ./script.sh 1 2
1 2
6 7


For 1, 2 and 5:



$ ./script.sh 1 2 5
1 2 5
6 7 0





share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    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: 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
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238821%2fhow-to-get-specific-words-by-index-in-a-line-bash%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote













    Use awk:



    $ awk 'print $1 " " $3' file


    for file:



    1 2 3 4 5
    6 7 8 9 0


    Output is:



    1 3
    6 8


    In bash script:



    #!/bin/bash

    awk_command="print";
    for i in "$@"; do
    awk_command="$awk_command $$i " "";
    done
    awk_command="$awk_command";
    awk "$awk_command" file


    With this script you can pass any number of indexes:



    For 1 and 2:



    $ ./script.sh 1 2
    1 2
    6 7


    For 1, 2 and 5:



    $ ./script.sh 1 2 5
    1 2 5
    6 7 0





    share|improve this answer
























      up vote
      3
      down vote













      Use awk:



      $ awk 'print $1 " " $3' file


      for file:



      1 2 3 4 5
      6 7 8 9 0


      Output is:



      1 3
      6 8


      In bash script:



      #!/bin/bash

      awk_command="print";
      for i in "$@"; do
      awk_command="$awk_command $$i " "";
      done
      awk_command="$awk_command";
      awk "$awk_command" file


      With this script you can pass any number of indexes:



      For 1 and 2:



      $ ./script.sh 1 2
      1 2
      6 7


      For 1, 2 and 5:



      $ ./script.sh 1 2 5
      1 2 5
      6 7 0





      share|improve this answer






















        up vote
        3
        down vote










        up vote
        3
        down vote









        Use awk:



        $ awk 'print $1 " " $3' file


        for file:



        1 2 3 4 5
        6 7 8 9 0


        Output is:



        1 3
        6 8


        In bash script:



        #!/bin/bash

        awk_command="print";
        for i in "$@"; do
        awk_command="$awk_command $$i " "";
        done
        awk_command="$awk_command";
        awk "$awk_command" file


        With this script you can pass any number of indexes:



        For 1 and 2:



        $ ./script.sh 1 2
        1 2
        6 7


        For 1, 2 and 5:



        $ ./script.sh 1 2 5
        1 2 5
        6 7 0





        share|improve this answer












        Use awk:



        $ awk 'print $1 " " $3' file


        for file:



        1 2 3 4 5
        6 7 8 9 0


        Output is:



        1 3
        6 8


        In bash script:



        #!/bin/bash

        awk_command="print";
        for i in "$@"; do
        awk_command="$awk_command $$i " "";
        done
        awk_command="$awk_command";
        awk "$awk_command" file


        With this script you can pass any number of indexes:



        For 1 and 2:



        $ ./script.sh 1 2
        1 2
        6 7


        For 1, 2 and 5:



        $ ./script.sh 1 2 5
        1 2 5
        6 7 0






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 12:39









        caco3

        7211415




        7211415



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238821%2fhow-to-get-specific-words-by-index-in-a-line-bash%23new-answer', 'question_page');

            );

            Post as a guest














































































            這個網誌中的熱門文章

            Barbados

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3