【问题标题】:Generating consecutive numbered urls生成连续编号的 url
【发布时间】:2021-03-28 05:38:18
【问题描述】:

我想生成一个包含以下行的文本文件:

http://example.com/file1.pdf
http://example.com/file2.pdf
http://example.com/file3.pdf
.
.
http://example.com/file1000.pdf

谁能告诉我如何使用unix命令行?

谢谢

【问题讨论】:

  • 你试过什么?也许您可以提出一些解决方案,并告诉我们哪些具体不起作用。

标签: unix url text-files generate numbered


【解决方案1】:

带有一个 for 循环

for (( i=1;i<=1000;i++ ));
do 
    echo "http://example.com/file$i.pdf";
done > newfile

带序列:

while read i;
do 
   echo "http://example.com/file$i.pdf";
done <<< $(seq 1000) > newfile

【讨论】:

  • 谢谢大家!在我的 Mac 上,我使用这个:print -l 'http://example.com/file'{1..1000}.pdf &gt; newfile 在我的 Unix 机器上,我使用这个(由 Raman Sailopal 编写),这也适用于 Mac:for (( i=1;i&lt;=1000;i++ )); do echo "http://example.com/file$i.pdf"; done &gt; newfile;
  • 太棒了。如果建议符合您的要求,请您投票并接受解决方案。
【解决方案2】:

可以创建/运行 python 脚本文件来生成它。使用 vim、nano 或任何其他终端编辑器,创建一个 python 文件,如下所示:

def genFile(fn, start, end):
  with open(fn, "w+") as f:
    f.writelines([f"http://example.com/file{str(i)}.pdf\n" for i in range(start, end+1)])

try:
  fn = input("File Path: ") # can be relative
  start = int(input("Start: ")) # inclusive
  end = int(input("End: ")) # inclusive
  genFile(fn, start, end)
except:
  print("Invalid Input")

一旦将其写入文件,我们将其命名为 script.py。我们可以运行以下命令来执行脚本:

python script.py

然后,填写文件路径、开始和结束的提示。这应该会导致所有这些行都打印在由 '\n' 分隔的指定文件中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-11
    • 2021-05-08
    • 1970-01-01
    • 1970-01-01
    • 2020-12-27
    • 2011-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多