7beggars_nnnnm
7beggars_nnnnm

Reputation: 757

Repeat line range n times

I would like to repeat more than once all lines (from line 1 to line 13) from the 0.txt file and print the output in a 1.txt file.

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()"
   ]
  },

The only correlated (but not enough)command_01 and command_02 below that I know is

command_01:

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

(where n > 1)

command_02:

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

I tried to find some solution to adapt the command_01 in order to get several repeated segments from line 1 to line 13 of the 0.txt file, but I did not find anything for now.

Note: I posted the real text that represents my need, so contextually it is necessary that I can run the solution to the contents of 0.txt.

For all purposes, considering that I want to repeat n times (n > 1, always) all the rows of 0.txt comprised between line 1 and line 13, my 1.txt output file should be something below:

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....

I wish I could have a solution using AWK, SED, Perl or some regex, not necessarily in this order. If you can not use these tools then it is something I can run in Bash.

About the chosen solution:

Running the solution @JRFerguson,

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

to 13 th line was being printed (at least for my tests) as },{

instead of

  },
  {

see here my full output 1.txt.

But with this command awk '{gsub(/},{/,"},\n {");print}' 1.txt > 2.txt I have added a further step and then corrected the output at 13 th line in relation to @JRFerguson. Such that 2.txt is as shown below:

{
   "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()"
   ]
  },

Combining this additional command I consider the @jrferguson response as sufficient.

Upvotes: 0

Views: 152

Answers (1)

JRFerguson
JRFerguson

Reputation: 7526

One way is:

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

This reads the first 13 lines of your 0.txt file into an array. Then, the contents of the array is written to 3 times (for example) to 1.txt.

Note that the 0.txt file is only read once.

Upvotes: 1

Related Questions