简体   繁体   English

用bash读取文本文件并发送curl查询

[英]Reading text file with bash and sending curl query

I have a text file containing data to classify: 我有一个文本文件,其中包含要分类的数据:

facture
iphone
comment acceder

I have a bash script reading my text file line per line, and sending each line using curl to a classifier written in Python 3.4 and running in a Docker container. 我有一个bash脚本,每行读取我的文本文件行,然后使用curl将每一行发送到用Python 3.4编写并在Docker容器中运行的分类器。 Here is the code of the script: 这是脚本的代码:

old_IFS=$IFS
IFS=$'\n'
for line in $(cat file.txt) do
    echo $line
    curl -d q=$line localhost:8099/v2
    echo $'\n'
    sleep 1
done
IFS=$old_IFS

localhost:8099/v2 is the adress of the Docker container. localhost:8099 / v2是Docker容器的地址。 For each query, the classifier prints the probabilities of belonging to the defined classes (3 classes in our case: assistance, actu and boutique). 对于每个查询,分类器都会显示属于已定义类的概率(在本例中为3类:助手,actu和精品店)。

Let's have a look at the output of the command 让我们看一下命令的输出

curl -d q=facture localhost:8099/v2

{"classes": ["assistance"], "levels": {"assistance": 0.915, "boutique": 0.085, "actu": 0.0}}

However, when I'm running the script, I have this output: 但是,当我运行脚本时,我得到以下输出:

facture
{"classes": ["boutique", "actu", "assistance"], "levels": {"boutique": 0.333, "actu": 0.333, "assistance": 0.333}}

iphone
{"classes": ["boutique", "actu", "assistance"], "levels": {"boutique": 0.333, "actu": 0.333, "assistance": 0.333}}

comment acceder
{"classes": ["assistance"], "levels": {"assistance": 0.886, "boutique": 0.113, "actu": 0.0}}

The probabilities are not correct, which leads me to think that the query is not well read by the script. 概率不正确,这使我认为脚本无法很好地读取查询。 This assumption is contradicted by the fact that the line is correctly printed by the script. 该假设与脚本正确打印该行的事实相矛盾。 I first thought of an encoding problem, but all my files are using utf8, and so does the terminal. 我首先想到了编码问题,但是我所有的文件都使用utf8,终端也是如此。

So my question is, is my script doing something wrong when reading the content of the file ? 所以我的问题是,我的脚本在读取文件内容时是否做错了什么?

Try : q="facture" ? 尝试: q="facture"

I believe that your console needs to assume a string as a key. 我相信您的控制台需要假设字符串为键。 As it doesn't recognize the pattern, it searches for all by default ? 由于无法识别模式,因此默认情况下会搜索所有模式?

I guess maybe is your file.txt is not unix format, try execute 我想可能是您的file.txt不是Unix格式,请尝试执行

dos2unix file.txt

first 第一

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM