从Linux bash在Windows中形成的文本文件中读取内容 [英] Read content from text file formed in Windows in Linux bash
问题描述
我正在尝试使用wget和url从数据库下载文件.例如
I am trying to download files from a database using wget and url. E.g.
wget" http://www.rcsb.org/pdb/files/1BXS. pdb "
因此url的格式如下: http://www.rcsb.org/pdb/文件/($ idnumber).pdb"
So format of the url is as such: http://www.rcsb.org/pdb/files/($idnumber).pdb"
但是我有很多文件要下载;所以我写了一个bash脚本,该脚本从文本文件中读取id_numbers,形成url字符串并通过wget下载.
But I have many files to download; so I wrote a bash script that reads id_numbers from a text file, forms url string and downloads by wget.
!/bin/bash
while read line
do
url="http://www.rcsb.org/pdb/files/$line.pdb"
echo -e $url
wget $url
done < id_numbers.txt
但是,URL字符串的格式为
However, url string is formed as
.pdb://www.rcsb.org/pdb/files/4H80
因此,.pdb
用http
补充.我不知道为什么.有人有主意吗?
如何设置其格式,使网址为
So, .pdb
is repleced with http
. I cannot figure out why. Does anyone have an idea?
How can I format it so url is
"http://www.rcsb.org/pdb/files/($idnumber).pdb"
? 非常感谢.
注意.该问题被标记为如何在bash中连接字符串?"的重复项.但我实际上是在要求别的东西.在问这个问题之前,我先阅读了这个问题,结果发现我的问题是在Windows中准备txt文件,而不是真正的字符串混淆.我编辑了问题标题.我希望现在更加清楚.
Note. This question was marked as duplicate of 'How to concatenate strings in bash?' but I was actually asking for something else. I read that question before asking this one and it turns out my problem was with preparing the txt file in Windows not really string concetanation. I edited question title. I hope it is more clear now.
推荐答案
听起来您的id_numbers.txt文件具有DOS/Windows风格的行尾(回车符后跟换行符),而不是普通的unix行尾(仅换行) ).结果是read
认为该行以回车符结尾,$line
实际上在末尾有一个回车符,并且该行嵌入了url中,从而引起各种混乱.
It sounds like your id_numbers.txt file has DOS/Windows-style line endings (carriage return followed by linefeed characters) instead of plain unix line endings (just linefeed). The result is that read
thinks the line ends with a carriage return, $line
actually has a carriage return at the end, and that gets embedded in the url, causing various confusion.
有几种解决方法.使用该变量时,您可以对其进行bash修剪:
There are several ways to solve this. You could have bash trim the carriage return from the variable when you use it:
url="http://www.rcsb.org/pdb/files/${line%$'\r'}.pdb"
或者您可以让read
修剪它,方法是将回车符算作空格(read
将从其读取的内容中删除前导和尾随空白):
Or you could have read
trim it by telling it that carriage return counts as whitespace (read
will trim leading and trailing whitespace from what it reads):
while IFS=$'\r' read line
或者您可以使用dos2unix之类的命令(或操作系统上的等效命令)来转换id_numbers.txt文件.
Or you could use a command like dos2unix (or whatever the equivalent is on your OS) to convert the id_numbers.txt file.
这篇关于从Linux bash在Windows中形成的文本文件中读取内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!