Skip to content

Skipping N lines of a large file, then piping #6570

Closed

Description

@hut8

Question

I want to translate this from bash to nushell:

zcat filename.sql.gz |
    tail -n +46 |
    mysql dbname

That file is huge, so I can't load it all into memory -- but there should be no need for that, unless pipelines are entirely sequential and store the intermediate result. Although nushell supports an impressive array of inputs, including SQLite, it doesn't seem to support any compressed formats natively (right?). So first I extracted my .gz into its own file. Then, I have tried:

> open --raw filename.sql | lines | skip 46 | to text | mysql dbname

This appears to load everything into memory.

If I do:

open --raw pagelinks.sql  | lines | skip 47 | take 1 | to text | mysql dbname

it seems to work, but that of course only gets one line. Can nushell do streaming like I need? I am sorry I was unable to find the docs but I read the entire book and cookbook. I'm really impressed with nushell generally but couldn't figure out this simple script. Thanks for your help in advance!

Additional context and details

No response

Activity

  1. added
    deprecated:question
    on Sep 16, 2022
  2. fdncred commented on Sep 16, 2022

    @fdncred
    Contributor

    I'm trying to understand what you're doing and may not be getting it all.
    If I do this:

    open Cargo.lock | lines | skip 47
    

    it seems to stream the rest of the data.

    to text absolutely does a collect so it will not stream.

    You could try print - i can't remember if it collects or not (I don't think it does), like:

    open Cargo.lock | lines | skip 47 | each {|i| print $i }
    
  3. NotTheDr01ds commented on Sep 16, 2022

    @NotTheDr01ds
    Contributor

    Repeating my Super User answer to your question there.

    As @fdncred said, the to text is the main problem that prevents streaming, since you are essentially telling it to collect the entire list of lines/strings into a single string.

    zcat filename.sql.gz | lines | skip 46 | each { mysql dbname }

    ... should work. It definitely works for me in a 20 million line test file using the form:

    zcat filename.sql.gz  | lines | skip 46 | each { bash -c "cat; echo" }

    Alternatively, using explicit output and an explicit each variable:

    zcat filename.sql.gz  | 
        lines |
        skip 46 |
        each {|mysql_statement|
            echo $mysql_statement | mysql dbname
        }
  4. NotTheDr01ds commented on Sep 16, 2022

    @NotTheDr01ds
    Contributor

    Dang Cool -- I didn't realize we have nu as a language specifier on code-blocks here!

    ```nu
    codeblock
    ```
    
  5. hut8 commented on Sep 16, 2022

    @hut8
    Author

    Perfect. Thanks for the help!

  6. fdncred commented on Sep 16, 2022

    @fdncred
    Contributor

    @NotTheDr01ds nu is a language recognized by GitHub that isn't nushell, unfortunately.

  7. NotTheDr01ds commented on Sep 16, 2022

    @NotTheDr01ds
    Contributor

    @NotTheDr01ds nu is a language recognized by GitHub that isn't nushell, unfortunately.

    Ah, that makes sense. Thanks. I got excited when it did a passable job at formatting ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    deprecated:question

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions