Bash: 从文件中解析 Urls,处理它们然后从文件中删除它们
Bash: Parse Urls from file, process them and then remove them from the file
我正在尝试自动化一个过程,系统将在该过程中获取文件的内容(每行 1 Url),使用 wget 从站点(https 文件夹)获取文件,然后删除文件中的行。
我已经尝试了几次,但 sed 部分(最后)无法理解字符串(我尝试转义字符)并将其从该文件中删除!
cat File
https://something.net/xxx/data/Folder1/
https://something.net/xxx/data/Folder2/
https://something.net/xxx/data/Folder3/
我的代码行是:
cat File | xargs -n1 -I @ bash -c 'wget -r -nd -l 1 -c -A rar,zip,7z,txt,jpg,iso,sfv,md5,pdf --no-parent --restrict-file-names=nocontrol --user=test --password=pass --no-check-certificate "@" -P /mnt/USB/ && sed -e 's|@||g' File'
一直到 sed -e 's|@||g' File
部分..
提前致谢!
我相信你只需要在 sed -e
之后使用双引号。而不是:
'...&& sed -e 's|@||g' File'
你需要
'...&& sed -e '"'s|@||g'"' File'
我明白你想做什么,但我不明白 sed
命令包括管道。也许是一些我不理解的奇特格式。
无论如何,我认为 sed 命令应该是这样的...
sed -e 's/@//g'
此命令将从流中删除所有@。
希望对您有所帮助!
尽可能不要使用 cat。这是不好的做法,大文件可能会出现问题...您可以更改
cat File | xargs -n1 -I @ bash -c
到
for siteUrl in $( < "File" ); do
使用带双引号的 sed 更正确也更简单...我的变体:
scriptDir=$( dirname -- "[=12=]" )
for siteUrl in $( < "$scriptDir/File.txt" )
do
if [[ -z "$siteUrl" ]]; then break; fi # break line if him empty
wget -r -nd -l 1 -c -A rar,zip,7z,txt,jpg,iso,sfv,md5,pdf --no-parent --restrict-file-names=nocontrol --user=test --password=pass --no-check-certificate "$siteUrl" -P /mnt/USB/ && sed -i "s|$siteUrl||g" "$scriptDir/File.txt"
done
@beliy 的回答看起来不错!
如果你想要一条线,你可以这样做:
while read -r line; do \
wget -r -nd -l 1 -c -A rar,zip,7z,txt,jpg,iso,sfv,md5,pdf \
--no-parent --restrict-file-names=nocontrol --user=test \
--password=pass --no-check-certificate "$line" -P /mnt/USB/ \
&& sed -i -e '\|'"$line"'|d' "File.txt"; \
done < File.txt
编辑:
You need to add a \
in front of the first pipe
我正在尝试自动化一个过程,系统将在该过程中获取文件的内容(每行 1 Url),使用 wget 从站点(https 文件夹)获取文件,然后删除文件中的行。
我已经尝试了几次,但 sed 部分(最后)无法理解字符串(我尝试转义字符)并将其从该文件中删除!
cat File
https://something.net/xxx/data/Folder1/
https://something.net/xxx/data/Folder2/
https://something.net/xxx/data/Folder3/
我的代码行是:
cat File | xargs -n1 -I @ bash -c 'wget -r -nd -l 1 -c -A rar,zip,7z,txt,jpg,iso,sfv,md5,pdf --no-parent --restrict-file-names=nocontrol --user=test --password=pass --no-check-certificate "@" -P /mnt/USB/ && sed -e 's|@||g' File'
一直到 sed -e 's|@||g' File
部分..
提前致谢!
我相信你只需要在 sed -e
之后使用双引号。而不是:
'...&& sed -e 's|@||g' File'
你需要
'...&& sed -e '"'s|@||g'"' File'
我明白你想做什么,但我不明白 sed
命令包括管道。也许是一些我不理解的奇特格式。
无论如何,我认为 sed 命令应该是这样的...
sed -e 's/@//g'
此命令将从流中删除所有@。
希望对您有所帮助!
尽可能不要使用 cat。这是不好的做法,大文件可能会出现问题...您可以更改
cat File | xargs -n1 -I @ bash -c
到
for siteUrl in $( < "File" ); do
使用带双引号的 sed 更正确也更简单...我的变体:
scriptDir=$( dirname -- "[=12=]" )
for siteUrl in $( < "$scriptDir/File.txt" )
do
if [[ -z "$siteUrl" ]]; then break; fi # break line if him empty
wget -r -nd -l 1 -c -A rar,zip,7z,txt,jpg,iso,sfv,md5,pdf --no-parent --restrict-file-names=nocontrol --user=test --password=pass --no-check-certificate "$siteUrl" -P /mnt/USB/ && sed -i "s|$siteUrl||g" "$scriptDir/File.txt"
done
@beliy 的回答看起来不错!
如果你想要一条线,你可以这样做:
while read -r line; do \
wget -r -nd -l 1 -c -A rar,zip,7z,txt,jpg,iso,sfv,md5,pdf \
--no-parent --restrict-file-names=nocontrol --user=test \
--password=pass --no-check-certificate "$line" -P /mnt/USB/ \
&& sed -i -e '\|'"$line"'|d' "File.txt"; \
done < File.txt
编辑:
You need to add a \
in front of the first pipe