running a Python script for each line of a text file

Clash 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
command-line bash scripts python
add a comment |Â
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
command-line bash scripts python
This is how the output ofecho $n $line::30is supposed to look like: paste.ubuntu.com/p/87kYs6fvpn What is the output of/bin/bash --version?
â muru
Mar 5 at 1:13
add a comment |Â
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
command-line bash scripts python
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
command-line bash scripts python
edited Mar 5 at 1:13
asked Mar 4 at 23:47
Mona Jalal
1,05442236
1,05442236
This is how the output ofecho $n $line::30is supposed to look like: paste.ubuntu.com/p/87kYs6fvpn What is the output of/bin/bash --version?
â muru
Mar 5 at 1:13
add a comment |Â
This is how the output ofecho $n $line::30is 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
add a comment |Â
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.
codepad.org/Iu6xV2jV didn't work. Only created one json file
â Mona Jalal
Mar 5 at 0:11
@MonaJalal your question refers to a fileinput.txtbut your linkedlsoutput does not appear to include such a file - you will need to replaceinput.txtby the name of your actual file.
â steeldriver
Mar 5 at 0:29
pastebin.com/raw/GQefrTX0
â Mona Jalal
Mar 5 at 0:39
add a comment |Â
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
add a comment |Â
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.
codepad.org/Iu6xV2jV didn't work. Only created one json file
â Mona Jalal
Mar 5 at 0:11
@MonaJalal your question refers to a fileinput.txtbut your linkedlsoutput does not appear to include such a file - you will need to replaceinput.txtby the name of your actual file.
â steeldriver
Mar 5 at 0:29
pastebin.com/raw/GQefrTX0
â Mona Jalal
Mar 5 at 0:39
add a comment |Â
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.
codepad.org/Iu6xV2jV didn't work. Only created one json file
â Mona Jalal
Mar 5 at 0:11
@MonaJalal your question refers to a fileinput.txtbut your linkedlsoutput does not appear to include such a file - you will need to replaceinput.txtby the name of your actual file.
â steeldriver
Mar 5 at 0:29
pastebin.com/raw/GQefrTX0
â Mona Jalal
Mar 5 at 0:39
add a comment |Â
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.
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.
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 fileinput.txtbut your linkedlsoutput does not appear to include such a file - you will need to replaceinput.txtby the name of your actual file.
â steeldriver
Mar 5 at 0:29
pastebin.com/raw/GQefrTX0
â Mona Jalal
Mar 5 at 0:39
add a comment |Â
codepad.org/Iu6xV2jV didn't work. Only created one json file
â Mona Jalal
Mar 5 at 0:11
@MonaJalal your question refers to a fileinput.txtbut your linkedlsoutput does not appear to include such a file - you will need to replaceinput.txtby 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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
answered Mar 5 at 1:15
Mona Jalal
1,05442236
1,05442236
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
This is how the output of
echo $n $line::30is supposed to look like: paste.ubuntu.com/p/87kYs6fvpn What is the output of/bin/bash --version?â muru
Mar 5 at 1:13