我徒劳地试图用这个脚本来解决我的问题。
#!/bin/bash
for a in `wget -O - "https://pastebin.com/raw/7BFC94Ty"`; do echo "local-zone: \""$a" redirect\"""local-data: \""$a" A 0.0.0.0\"" >> adstest.conf; done
结果如下
local-zone: "101com.com
redirect"local-data: "101com.com
A 0.0.0.0"
但这不是正确的格式,这是预期的结果:
local-zone: "101com.com" redirect
local-data: "101com.com A 0.0.0.0"
但我找不到这个该死的解决方案。我也在寻找能够使用本地文件而不是网址的解决方案,因为我有一个40 000行的文件,我想没有网络主机。
感谢您的帮助!
答案 0 :(得分:1)
为了解决@ chepner的问题,我们可以使用sed
来处理流:
wget -O - "https://pastebin.com/raw/7BFC94Ty" | dos2unix | sed 's/\(.*\)/local-zone: "\1" redirect\nlocal-data: "\1 A 0.0.0.0"/' > adstest.conf
它只需要使用\(.*\)
保存的行的整个模式空间,并将其替换为我们想要的文本,将模式空间放回到我们需要的\1
,并使用\n
放入换行符。
答案 1 :(得分:0)
首先,您必须通过dos2unix
运行它以摆脱Windoze换行符,然后执行两个单独的echo
并将整个循环的结果发送到输出文件:
for a in `wget -O - "https://pastebin.com/raw/7BFC94Ty" | dos2unix`; do
echo 'local-zone: "'$a'" redirect'
echo 'local-data: "'$a' A 0.0.0.0"'
done >> adstest.conf
我也使用单引号,所以我不必逃避你想要打印的双打。
示例结果:
$ rm adstest.conf
$ for a in `wget -O - "https://pastebin.com/raw/7BFC94Ty" | dos2unix`; do echo 'local-zone: "'$a'" redirect'; echo 'local-data: "'$a' A 0.0.0.0"'; done >> adstest.conf
--2017-07-07 14:12:07-- https://pastebin.com/raw/7BFC94Ty
Resolving pastebin.com (pastebin.com)... 104.20.208.21, 104.20.209.21
Connecting to pastebin.com (pastebin.com)|104.20.208.21|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/plain]
Saving to: ‘STDOUT’
- [ <=> ] 385.23K --.-KB/s in 0.09s
2017-07-07 14:12:07 (3.97 MB/s) - written to stdout [394475]
$ wc -l adstest.conf
41860 adstest.conf
$ head adstest.conf
local-zone: "101com.com" redirect
local-data: "101com.com A 0.0.0.0"
local-zone: "101order.com" redirect
local-data: "101order.com A 0.0.0.0"
local-zone: "123found.com" redirect
local-data: "123found.com A 0.0.0.0"
local-zone: "140proof.com" redirect
local-data: "140proof.com A 0.0.0.0"
local-zone: "180hits.de" redirect
local-data: "180hits.de A 0.0.0.0"