running a Python script for each line of a text 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 can I perform the following task in bash?



I need to write a script that runs a python script on each line of a text file as an input separately and then saves the result in json file named after the line it reads the file from.



So text file 10tweets.txt looks like below:



cat 10tweets.txt



Trump on the other hand goes all in on water boarding AND some. #GOPDebate
RT @wpjenna Donald Trump promises that he will not touch the 2nd amendment -- "unless we're going to make it stronger."
Trump 23%, Rubio 19%, Kasich & Bush 14%, Christie 10%, Cruz 9% #NHPrimary
@realDonaldTrump Thank you for saying you won't use vulger language anymore. Talk about Sanders & Clinton. Take Cruz as VP. Mexican votes!!!
RT @SurfPHX Mr. Trump @realDonaldTrump tweeted 25 minutes ago. You all do realize, that our future President hardly sleeps. He's a Fighter and a Worker!
go, Bernie #DemDebate
Sanders calls out Clinton on taking Foreign Policy advice from Warmonger Henry Kissinger https://t.co/xT5J4uh4m4 via @YouTube
Cruz, Rubio, and the Moral Bankruptcy of Progressive Identity Politics https://t.co/kSQstJXtKO via @NRO
RT @scarylawyerguy "Who does Bernie Sanders listen to on foreign policy." - A question Hillary had to raise b/c the media will not. #DemDebate
Why Did U of California Fire Tenured Riverside Professor? / Ted Cruz and Higher Ed -- ... - https://t.co/zFxa4Q70wh


and I want the output be like 1.json, 2.json, 3.json, 4.json, 5.json in output folder.



Not sure how to make use of exec entity_sentiment.py "$@" in a bash script and link it to each line from the file.
The way the script is ran is like below



$ python entity_sentiment.py sentiment-entities-text "Thank you for saying you won't use vulger language anymore"
Mentions:
Name: "vulger language"
Begin Offset : 35
Content : vulger language
Magnitude : 0.699999988079071
Sentiment : -0.699999988079071
Type : 2
Salience: 1.0
Sentiment: magnitude: 0.699999988079071
score: -0.699999988079071


In which for example, the input to script could be assumed the first line of the file.



Basically, running the following bash script only analyzes the last line of the file and saves it in 1.json



#!/bin/bash

n=1

while read -u 3 -r line; do
python entity_sentiment.py sentiment-entities-text "$line" > "$((n++)).json"
done 3< 10tweets.txt


Here's an snippet of what happens when I ran a suggest in bash IRC channel: https://pastebin.com/raw/VQpPFJYs and https://pastebin.com/raw/GQefrTX0










share|improve this question























  • This is how the output of echo $n $line::30 is supposed to look like: paste.ubuntu.com/p/87kYs6fvpn What is the output of /bin/bash --version?
    – muru
    Mar 5 at 1:13















up vote
-1
down vote

favorite












How can I perform the following task in bash?



I need to write a script that runs a python script on each line of a text file as an input separately and then saves the result in json file named after the line it reads the file from.



So text file 10tweets.txt looks like below:



cat 10tweets.txt



Trump on the other hand goes all in on water boarding AND some. #GOPDebate
RT @wpjenna Donald Trump promises that he will not touch the 2nd amendment -- "unless we're going to make it stronger."
Trump 23%, Rubio 19%, Kasich & Bush 14%, Christie 10%, Cruz 9% #NHPrimary
@realDonaldTrump Thank you for saying you won't use vulger language anymore. Talk about Sanders & Clinton. Take Cruz as VP. Mexican votes!!!
RT @SurfPHX Mr. Trump @realDonaldTrump tweeted 25 minutes ago. You all do realize, that our future President hardly sleeps. He's a Fighter and a Worker!
go, Bernie #DemDebate
Sanders calls out Clinton on taking Foreign Policy advice from Warmonger Henry Kissinger https://t.co/xT5J4uh4m4 via @YouTube
Cruz, Rubio, and the Moral Bankruptcy of Progressive Identity Politics https://t.co/kSQstJXtKO via @NRO
RT @scarylawyerguy "Who does Bernie Sanders listen to on foreign policy." - A question Hillary had to raise b/c the media will not. #DemDebate
Why Did U of California Fire Tenured Riverside Professor? / Ted Cruz and Higher Ed -- ... - https://t.co/zFxa4Q70wh


and I want the output be like 1.json, 2.json, 3.json, 4.json, 5.json in output folder.



Not sure how to make use of exec entity_sentiment.py "$@" in a bash script and link it to each line from the file.
The way the script is ran is like below



$ python entity_sentiment.py sentiment-entities-text "Thank you for saying you won't use vulger language anymore"
Mentions:
Name: "vulger language"
Begin Offset : 35
Content : vulger language
Magnitude : 0.699999988079071
Sentiment : -0.699999988079071
Type : 2
Salience: 1.0
Sentiment: magnitude: 0.699999988079071
score: -0.699999988079071


In which for example, the input to script could be assumed the first line of the file.



Basically, running the following bash script only analyzes the last line of the file and saves it in 1.json



#!/bin/bash

n=1

while read -u 3 -r line; do
python entity_sentiment.py sentiment-entities-text "$line" > "$((n++)).json"
done 3< 10tweets.txt


Here's an snippet of what happens when I ran a suggest in bash IRC channel: https://pastebin.com/raw/VQpPFJYs and https://pastebin.com/raw/GQefrTX0










share|improve this question























  • This is how the output of echo $n $line::30 is supposed to look like: paste.ubuntu.com/p/87kYs6fvpn What is the output of /bin/bash --version?
    – muru
    Mar 5 at 1:13













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











How can I perform the following task in bash?



I need to write a script that runs a python script on each line of a text file as an input separately and then saves the result in json file named after the line it reads the file from.



So text file 10tweets.txt looks like below:



cat 10tweets.txt



Trump on the other hand goes all in on water boarding AND some. #GOPDebate
RT @wpjenna Donald Trump promises that he will not touch the 2nd amendment -- "unless we're going to make it stronger."
Trump 23%, Rubio 19%, Kasich & Bush 14%, Christie 10%, Cruz 9% #NHPrimary
@realDonaldTrump Thank you for saying you won't use vulger language anymore. Talk about Sanders & Clinton. Take Cruz as VP. Mexican votes!!!
RT @SurfPHX Mr. Trump @realDonaldTrump tweeted 25 minutes ago. You all do realize, that our future President hardly sleeps. He's a Fighter and a Worker!
go, Bernie #DemDebate
Sanders calls out Clinton on taking Foreign Policy advice from Warmonger Henry Kissinger https://t.co/xT5J4uh4m4 via @YouTube
Cruz, Rubio, and the Moral Bankruptcy of Progressive Identity Politics https://t.co/kSQstJXtKO via @NRO
RT @scarylawyerguy "Who does Bernie Sanders listen to on foreign policy." - A question Hillary had to raise b/c the media will not. #DemDebate
Why Did U of California Fire Tenured Riverside Professor? / Ted Cruz and Higher Ed -- ... - https://t.co/zFxa4Q70wh


and I want the output be like 1.json, 2.json, 3.json, 4.json, 5.json in output folder.



Not sure how to make use of exec entity_sentiment.py "$@" in a bash script and link it to each line from the file.
The way the script is ran is like below



$ python entity_sentiment.py sentiment-entities-text "Thank you for saying you won't use vulger language anymore"
Mentions:
Name: "vulger language"
Begin Offset : 35
Content : vulger language
Magnitude : 0.699999988079071
Sentiment : -0.699999988079071
Type : 2
Salience: 1.0
Sentiment: magnitude: 0.699999988079071
score: -0.699999988079071


In which for example, the input to script could be assumed the first line of the file.



Basically, running the following bash script only analyzes the last line of the file and saves it in 1.json



#!/bin/bash

n=1

while read -u 3 -r line; do
python entity_sentiment.py sentiment-entities-text "$line" > "$((n++)).json"
done 3< 10tweets.txt


Here's an snippet of what happens when I ran a suggest in bash IRC channel: https://pastebin.com/raw/VQpPFJYs and https://pastebin.com/raw/GQefrTX0










share|improve this question















How can I perform the following task in bash?



I need to write a script that runs a python script on each line of a text file as an input separately and then saves the result in json file named after the line it reads the file from.



So text file 10tweets.txt looks like below:



cat 10tweets.txt



Trump on the other hand goes all in on water boarding AND some. #GOPDebate
RT @wpjenna Donald Trump promises that he will not touch the 2nd amendment -- "unless we're going to make it stronger."
Trump 23%, Rubio 19%, Kasich & Bush 14%, Christie 10%, Cruz 9% #NHPrimary
@realDonaldTrump Thank you for saying you won't use vulger language anymore. Talk about Sanders & Clinton. Take Cruz as VP. Mexican votes!!!
RT @SurfPHX Mr. Trump @realDonaldTrump tweeted 25 minutes ago. You all do realize, that our future President hardly sleeps. He's a Fighter and a Worker!
go, Bernie #DemDebate
Sanders calls out Clinton on taking Foreign Policy advice from Warmonger Henry Kissinger https://t.co/xT5J4uh4m4 via @YouTube
Cruz, Rubio, and the Moral Bankruptcy of Progressive Identity Politics https://t.co/kSQstJXtKO via @NRO
RT @scarylawyerguy "Who does Bernie Sanders listen to on foreign policy." - A question Hillary had to raise b/c the media will not. #DemDebate
Why Did U of California Fire Tenured Riverside Professor? / Ted Cruz and Higher Ed -- ... - https://t.co/zFxa4Q70wh


and I want the output be like 1.json, 2.json, 3.json, 4.json, 5.json in output folder.



Not sure how to make use of exec entity_sentiment.py "$@" in a bash script and link it to each line from the file.
The way the script is ran is like below



$ python entity_sentiment.py sentiment-entities-text "Thank you for saying you won't use vulger language anymore"
Mentions:
Name: "vulger language"
Begin Offset : 35
Content : vulger language
Magnitude : 0.699999988079071
Sentiment : -0.699999988079071
Type : 2
Salience: 1.0
Sentiment: magnitude: 0.699999988079071
score: -0.699999988079071


In which for example, the input to script could be assumed the first line of the file.



Basically, running the following bash script only analyzes the last line of the file and saves it in 1.json



#!/bin/bash

n=1

while read -u 3 -r line; do
python entity_sentiment.py sentiment-entities-text "$line" > "$((n++)).json"
done 3< 10tweets.txt


Here's an snippet of what happens when I ran a suggest in bash IRC channel: https://pastebin.com/raw/VQpPFJYs and https://pastebin.com/raw/GQefrTX0







command-line bash scripts python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 5 at 1:13

























asked Mar 4 at 23:47









Mona Jalal

1,05442236




1,05442236











  • This is how the output of echo $n $line::30 is supposed to look like: paste.ubuntu.com/p/87kYs6fvpn What is the output of /bin/bash --version?
    – muru
    Mar 5 at 1:13

















  • This is how the output of echo $n $line::30 is supposed to look like: paste.ubuntu.com/p/87kYs6fvpn What is the output of /bin/bash --version?
    – muru
    Mar 5 at 1:13
















This is how the output of echo $n $line::30 is supposed to look like: paste.ubuntu.com/p/87kYs6fvpn What is the output of /bin/bash --version?
– muru
Mar 5 at 1:13





This is how the output of echo $n $line::30 is supposed to look like: paste.ubuntu.com/p/87kYs6fvpn What is the output of /bin/bash --version?
– muru
Mar 5 at 1:13











2 Answers
2






active

oldest

votes

















up vote
0
down vote













You can read the input file line-by-line and apply a command to each line using something like



#!/bin/bash

n=1

while read -r line; do
python entity_sentiment.py sentiment-entities-text "$line" > "$((n++)).json"
done < input.txt


I don't see how exec entity_sentiment.py "$@" would be helpful.






share|improve this answer




















  • codepad.org/Iu6xV2jV didn't work. Only created one json file
    – Mona Jalal
    Mar 5 at 0:11











  • @MonaJalal your question refers to a file input.txt but your linked ls output does not appear to include such a file - you will need to replace input.txt by the name of your actual file.
    – steeldriver
    Mar 5 at 0:29










  • pastebin.com/raw/GQefrTX0
    – Mona Jalal
    Mar 5 at 0:39

















up vote
0
down vote













Thanks to IRC bash community



#!/bin/bash

n=1

while read -u 3 -r line; do
echo $n "$line::30"
python entity_sentiment.py sentiment-entities-text "$line" > "$((n++)).json"
((n++))
done 3< 10tweets.txt





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',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1011922%2frunning-a-python-script-for-each-line-of-a-text-file%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    You can read the input file line-by-line and apply a command to each line using something like



    #!/bin/bash

    n=1

    while read -r line; do
    python entity_sentiment.py sentiment-entities-text "$line" > "$((n++)).json"
    done < input.txt


    I don't see how exec entity_sentiment.py "$@" would be helpful.






    share|improve this answer




















    • codepad.org/Iu6xV2jV didn't work. Only created one json file
      – Mona Jalal
      Mar 5 at 0:11











    • @MonaJalal your question refers to a file input.txt but your linked ls output does not appear to include such a file - you will need to replace input.txt by the name of your actual file.
      – steeldriver
      Mar 5 at 0:29










    • pastebin.com/raw/GQefrTX0
      – Mona Jalal
      Mar 5 at 0:39














    up vote
    0
    down vote













    You can read the input file line-by-line and apply a command to each line using something like



    #!/bin/bash

    n=1

    while read -r line; do
    python entity_sentiment.py sentiment-entities-text "$line" > "$((n++)).json"
    done < input.txt


    I don't see how exec entity_sentiment.py "$@" would be helpful.






    share|improve this answer




















    • codepad.org/Iu6xV2jV didn't work. Only created one json file
      – Mona Jalal
      Mar 5 at 0:11











    • @MonaJalal your question refers to a file input.txt but your linked ls output does not appear to include such a file - you will need to replace input.txt by the name of your actual file.
      – steeldriver
      Mar 5 at 0:29










    • pastebin.com/raw/GQefrTX0
      – Mona Jalal
      Mar 5 at 0:39












    up vote
    0
    down vote










    up vote
    0
    down vote









    You can read the input file line-by-line and apply a command to each line using something like



    #!/bin/bash

    n=1

    while read -r line; do
    python entity_sentiment.py sentiment-entities-text "$line" > "$((n++)).json"
    done < input.txt


    I don't see how exec entity_sentiment.py "$@" would be helpful.






    share|improve this answer












    You can read the input file line-by-line and apply a command to each line using something like



    #!/bin/bash

    n=1

    while read -r line; do
    python entity_sentiment.py sentiment-entities-text "$line" > "$((n++)).json"
    done < input.txt


    I don't see how exec entity_sentiment.py "$@" would be helpful.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 5 at 0:02









    steeldriver

    63.3k1198167




    63.3k1198167











    • codepad.org/Iu6xV2jV didn't work. Only created one json file
      – Mona Jalal
      Mar 5 at 0:11











    • @MonaJalal your question refers to a file input.txt but your linked ls output does not appear to include such a file - you will need to replace input.txt by the name of your actual file.
      – steeldriver
      Mar 5 at 0:29










    • pastebin.com/raw/GQefrTX0
      – Mona Jalal
      Mar 5 at 0:39
















    • codepad.org/Iu6xV2jV didn't work. Only created one json file
      – Mona Jalal
      Mar 5 at 0:11











    • @MonaJalal your question refers to a file input.txt but your linked ls output does not appear to include such a file - you will need to replace input.txt by the name of your actual file.
      – steeldriver
      Mar 5 at 0:29










    • pastebin.com/raw/GQefrTX0
      – Mona Jalal
      Mar 5 at 0:39















    codepad.org/Iu6xV2jV didn't work. Only created one json file
    – Mona Jalal
    Mar 5 at 0:11





    codepad.org/Iu6xV2jV didn't work. Only created one json file
    – Mona Jalal
    Mar 5 at 0:11













    @MonaJalal your question refers to a file input.txt but your linked ls output does not appear to include such a file - you will need to replace input.txt by the name of your actual file.
    – steeldriver
    Mar 5 at 0:29




    @MonaJalal your question refers to a file input.txt but your linked ls output does not appear to include such a file - you will need to replace input.txt by the name of your actual file.
    – steeldriver
    Mar 5 at 0:29












    pastebin.com/raw/GQefrTX0
    – Mona Jalal
    Mar 5 at 0:39




    pastebin.com/raw/GQefrTX0
    – Mona Jalal
    Mar 5 at 0:39












    up vote
    0
    down vote













    Thanks to IRC bash community



    #!/bin/bash

    n=1

    while read -u 3 -r line; do
    echo $n "$line::30"
    python entity_sentiment.py sentiment-entities-text "$line" > "$((n++)).json"
    ((n++))
    done 3< 10tweets.txt





    share|improve this answer
























      up vote
      0
      down vote













      Thanks to IRC bash community



      #!/bin/bash

      n=1

      while read -u 3 -r line; do
      echo $n "$line::30"
      python entity_sentiment.py sentiment-entities-text "$line" > "$((n++)).json"
      ((n++))
      done 3< 10tweets.txt





      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        Thanks to IRC bash community



        #!/bin/bash

        n=1

        while read -u 3 -r line; do
        echo $n "$line::30"
        python entity_sentiment.py sentiment-entities-text "$line" > "$((n++)).json"
        ((n++))
        done 3< 10tweets.txt





        share|improve this answer












        Thanks to IRC bash community



        #!/bin/bash

        n=1

        while read -u 3 -r line; do
        echo $n "$line::30"
        python entity_sentiment.py sentiment-entities-text "$line" > "$((n++)).json"
        ((n++))
        done 3< 10tweets.txt






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 5 at 1:15









        Mona Jalal

        1,05442236




        1,05442236



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1011922%2frunning-a-python-script-for-each-line-of-a-text-file%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            GRUB: Fatal! inconsistent data read from (0x84) 0+xxxxxx

            Do not install recommended packages of dependencies

            What makes Checkinstall packages not suitable for distribution?