I'm using a piped command to migrate a big production DB from one host to another using this command:

mysqldump <someparams> | pv | mysql <someparams>

And I need to extract the line 23 (or let's say the first X lines) (saved as file or simply in bash output) from the SQL passing from one server to another.

What I've tried:

  • Concatenate in output less, at least to see the output scrolling, but no luck

    mysqldump <someparams> | pv | mysql <someparams> | less

  • Read about sed, but it's not useful to me

  • Using head to write to a file, but it is empty

mysqldump <someparams> | pv | mysql <someparams> | head -n 25 > somefile.txt

The only requirement I have is that I cannot save this .sql file.

Any idea?


1 Answer 1


With zsh

mysqldump <someparams> |
  pv > >(sed '22,24!d' > saved-lines-22-to-24.txt) |
  mysql <someparams>

With bash (or zsh):

mysqldump <someparams> |
  pv |
  tee >(sed '22,24!d' > saved-lines-22-to-24.txt) |
  mysql <someparams>

(though beware that as bash doesn't wait for that sed process, it's not guaranteed that saved-lines-22-to-24.txt will be complete by the time you run the next command in the script).

Or you could have sed to the writing:

mysqldump <someparams> |
  pv |
  sed '22,24 w saved-lines-22-to-24.txt' |
  mysql <someparams>

To have it as output, with zsh:

{mysqldump <someparams> |
  pv > >(sed '22,24!d' >&3) |
  mysql <someparams>} 3>&1

or bash/zsh:

{ mysqldump <someparams> |
  pv |
  tee >(sed '22,24!d' >&3)
  mysql <someparams>
} 3>&1
  • Worked perfeclty (I used the third solution you proposed me). Thank you sir!
    – Ghesio
    Nov 21, 2017 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.