ejecutar un script de Python para cada línea de un archivo de texto

ejecutar un script de Python para cada línea de un archivo de texto

¿Cómo puedo realizar la siguiente tarea en bash?

Necesito escribir un script que ejecute un script de Python en cada línea de un archivo de texto como entrada por separado y luego guarde el resultado en un archivo json con el nombre de la línea desde la que lee el archivo.

Entonces el archivo de texto 10tweets.txt se ve así:

gato 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

y quiero que la salida sea como 1.json, 2.json, 3.json, 4.json, 5.json en la carpeta de salida.

No estoy seguro de cómo utilizarlo exec entity_sentiment.py "$@"en un script bash y vincularlo a cada línea del archivo. La forma en que se ejecuta el script es la siguiente

$ 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

En el que, por ejemplo, la entrada al script podría asumirse como la primera línea del archivo.

Básicamente, ejecutar el siguiente script bash solo analiza la última línea del archivo y lo guarda en 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

Aquí hay un fragmento de lo que sucede cuando ejecuto una sugerencia en el canal IRC de bash:https://pastebin.com/raw/VQpPFJYsyhttps://pastebin.com/raw/GQefrTX0

Respuesta1

Puede readingresar el archivo línea por línea y aplicar un comando a cada línea usando algo como

#!/bin/bash

n=1

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

No veo cómo exec entity_sentiment.py "$@"sería útil.

Respuesta2

Gracias a la comunidad IRC bash

#!/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

información relacionada