Loading...

Bash Collection – Script for cutting parts from large file

The following small script was written by me in order to be able to simply cut parts from very large text files.
It is very useful for database administrators to for example cut the SQL statements for creating and restoring one single table from a really large SQL dump.

For this you would need to determine the line numbers from the large file. For the example with restoring a single table from a SQL dump, you would do something like this:

This would give you this example output:

So, if you now want all the statements for restoring “table 4”, you would need the SQL statements from lines 3245 – 3643.
Since mostly there also is a “DROP TABLE…” one line before the “CREATE TABLE” statement, we would need to substrate at least one line from the last line number.
The line numbers we would need then would be: 3245 – 3642

So, with this information, we then can use the script below. Just type in the command line:

This command would cut everything from lines 3245 until 3642 (including the given line numbers!) from the original, large dump and put them into a new file “./table-4.sql”.

And here is the script. Just save it as /usr/local/bin/cutter and make it executable (chmod 755 /usr/local/bin/cutter)

Find the script here…

Hope it helps.


If you also have some useful scripts, please don’t hesitate to share them with us!
Just contact info@linuxcounter.net and attach your script with a small description what it does.
Thanks!

.

Share

About Christin Löhner (Linux Counter)


   6 Comments


  1. Chris F.A. Johnson
      2015-09-08

    #!/bin/bash

    if (( $# “$4”

  2. Simon Deziel
      2015-09-09

    This functionality is easily achieved with sed as well:

    sed -n ‘3245,3642 p’ ./large.sql.dump > ./table-4.sql

    • Alexander Löhner
        2015-09-10

      Cool thing! I knew that you can do that with sed as well, but for many people, sed still is a “book with sevven signets” as we would say in germany. 😛

      • rjn0
          2015-09-10

        If you prefer awk over sed, you may end up with
        awk 'NR>=3245&&NR ./table-4.sql
        🙂

  3. Rolando
      2015-09-10

    Thanks for sharing!…

  4. Голиков Александр Юрьевич
      2015-09-14

    lines 29-34 of your script may be replased with:
    head -n ${2} ${3} | tail -n $(( ${2} - $(( ${1} - 1 )) )) > ${4}

Leave a Reply