对文本文件的每一行运行 Python 脚本

对文本文件的每一行运行 Python 脚本

如何在 bash 中执行以下任务?

我需要编写一个脚本,对文本文件的每一行分别运行一个 python 脚本作为输入,然后将结果保存在以读取文件的行命名的 json 文件中。

因此文本文件 10tweets.txt 如下所示:

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

我希望输出文件夹中的输出像 1.json、2.json、3.json、4.json、5.json。

不确定如何在 bash 脚本中使用exec entity_sentiment.py "$@"并将其链接到文件中的每一行。脚本的运行方式如下

$ 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

例如,脚本的输入可以假定为文件的第一行。

基本上,运行以下 bash 脚本只会分析文件的最后一行并将其保存在 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

下面是我在 bash IRC 频道中运行建议时发生的片段:https://pastebin.com/raw/VQpPFJYshttps://pastebin.com/raw/GQefrTX0

答案1

您可以read逐行输入文件,并使用类似以下命令将命令应用于每一行

#!/bin/bash

n=1

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

我不明白这exec entity_sentiment.py "$@"会有什么帮助。

答案2

感谢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

相关内容