重复行范围 n 次

时间:2021-03-21 20:42:49

标签: text-processing

我想多次重复 0.txt 文件中的所有行(从第 1 行到第 13 行),并在 1.txt 中打印输出文件。

0.txt:

{
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "file = open('out1.txt', 'a')\n",
    "sys.stdout = file\n",
    "print(foo.bar_bar((x,y), (x1,y1)))\n",
    "file.close()"
   ]
  },

我所知道的唯一相关(但还不够)command_01command_02

command_01:

awk '{while(++i<n)print;i=0}' 0.txt > 1.txt 

(其中 n > 1)

command_02:

sed -n '1,13p' source.txt > target.txt

我试图找到一些解决方案来调整 command_01 以获得从 0.txt 文件的第 1 行到第 13 行的几个重复段,但我没有找到任何内容暂时。

注意:我发布了代表我需要的真实文本,因此在上下文中,我有必要对 0.txt 的内容运行解决方案。

出于所有目的,考虑到我想重复 n 次(n > 1,总是)0.txt 的所有行,包含在第 1 行和第 13 行之间,我的 1.txt。 txt 输出文件应该是下面的内容:

1.txt:

    {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "file = open('out1.txt', 'a')\n",
    "sys.stdout = file\n",
    "print(foo.bar_bar((x,y), (x1,y1)))\n",
    "file.close()"
   ]
  },
      {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "file = open('out1.txt', 'a')\n",
    "sys.stdout = file\n",
    "print(foo.bar_bar((x,y), (x1,y1)))\n",
    "file.close()"
   ]
  },
      {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "file = open('out1.txt', 'a')\n",
    "sys.stdout = file\n",
    "print(foo.bar_bar((x,y), (x1,y1)))\n",
    "file.close()"
   ]
  },
    .
    .
    .
    .
    ... etc....

我希望我能有一个使用 AWK、SED、Perl 或一些正则表达式的解决方案,不一定按此顺序。如果你不能使用这些工具,那么我可以在 Bash 中运行。

关于选择的解决方案:

运行解决方案@JRFerguson,

perl -ne 'if (1..13) {push @data,$_};END{print @data for 1..3}' 0.txt > 1.txt,

打印到第 13 行(至少对于我的测试而言)为 },{

代替

  },
  {

在此处查看我的完整输出 1.txt

但是使用此命令 awk '{gsub(/},{/,"},\n {");print}' 1.txt > 2.txt 我添加了进一步的步骤,然后更正了与 @JRFerguson 相关的第 13 行的输出。这样2.txt如下图:

{
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "file = open('out1.txt', 'a')\n",
    "sys.stdout = file\n",
    "print(subsystem.cause_info((1,2), (4,3)))\n",
    "file.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "file = open('out1.txt', 'a')\n",
    "sys.stdout = file\n",
    "print(subsystem.cause_info((1,2), (4,3)))\n",
    "file.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "file = open('out1.txt', 'a')\n",
    "sys.stdout = file\n",
    "print(subsystem.cause_info((1,2), (4,3)))\n",
    "file.close()"
   ]
  },

结合这个额外的命令,我认为@jrferguson 的响应就足够了。

1 个答案:

答案 0 :(得分:1)

一种方法是:

perl -ne 'if (1..13) {push @data,$_};END{print @data for 1..3}' 0.txt > 1.txt

这会将 0.txt 文件的前 13 行读入一个数组。然后,将数组的内容写入 3 次(例如)到 1.txt

请注意,0.txt 文件只能读取一次。