Find the answer to your Linux question:
Results 1 to 8 of 8
I have a 6.4 GB .sql file and i need to modify only the top 10 line portion of that file. How can i only open the top 10 lines ...
  1. #1
    Just Joined!
    Join Date
    Oct 2007
    Posts
    16

    How to use vi to edit only the top 10 lines file



    I have a 6.4 GB .sql file and i need to modify only the top 10 line portion of that file.
    How can i only open the top 10 lines of this file without fully opening it?
    OR
    How can i search and replace a certain string like "Create Database mydb1" and comment it out without fully opening it?

    Thank you
    Any help appreciated.

  2. #2
    Linux Guru smolloy's Avatar
    Join Date
    Apr 2005
    Location
    CA, but from N.Ireland
    Posts
    2,413
    You could use a combination of awk and sed. Use awk to print out the top 10 lines to another file for editing (you could even use awk to edit them in transit to the new file). Then use sed to delete those lines in the original file, and then, finally, concatenate the edited lines onto the top of the old file.
    Registered Linux user #388328 || Registered LFS user #15880
    AMD 64 X2 4600+ :: 2X1GB DDR2 800 :: GeForce 9400 GT 512MB :: ASUS M2N32 Deluxe :: 4X250GB SATAII
    Need instant help? Try us on IRC -- #linuxforums on freenode

  3. #3
    Linux Newbie danielsmw's Avatar
    Join Date
    Nov 2006
    Location
    Clemson, SC / Charleston, SC
    Posts
    110
    I'm not an expert with sed, but you may want to look into it a bit. This sounds like something you could use it for. Another possibility is to actually split it into two files:
    Code:
    head -n 10 file.sql > fileTop
    wc -l file.sql
    tail -n (number of lines - 10) > fileBottom
    That wc line is just there to figure out the number of lines in the file. Then you could edit fileTop and when you're done, just put the files back together. This is by no means an elegant solution, and I'm sure there are better answers, but that's just one way of approaching it.

    Another way could be to write a quick Perl script. Just open the the file, use your substitution for the first few lines ($line =~ s/replacingthis/withthis/ if $linenumber < 10, and then just print the rest of the lines as well.

    But hopefully, someone has a better solution, because I want to see it too!
    Registered Linux User: #479567
    Asking a question? Read this page first.
    Now... sudo make me a sandwich.
    ratiocinativeroot.blogspot.com

  4. #4
    Linux Newbie danielsmw's Avatar
    Join Date
    Nov 2006
    Location
    Clemson, SC / Charleston, SC
    Posts
    110
    oops, and smolloy posts a better solution as I type. =)
    Registered Linux User: #479567
    Asking a question? Read this page first.
    Now... sudo make me a sandwich.
    ratiocinativeroot.blogspot.com

  5. #5
    Linux Guru smolloy's Avatar
    Join Date
    Apr 2005
    Location
    CA, but from N.Ireland
    Posts
    2,413
    Quote Originally Posted by danielsmw View Post
    oops, and smolloy posts a better solution as I type. =)


    I'm not sure my solution is better. Your idea of using "head" to extract the top 10 lines is more elegant than awk (although you'd have to pipe it to sed if you wanted to edit the lines in transit as I suggested).

    Maybe a combination of our ideas is the best
    Registered Linux user #388328 || Registered LFS user #15880
    AMD 64 X2 4600+ :: 2X1GB DDR2 800 :: GeForce 9400 GT 512MB :: ASUS M2N32 Deluxe :: 4X250GB SATAII
    Need instant help? Try us on IRC -- #linuxforums on freenode

  6. #6
    Just Joined!
    Join Date
    Oct 2007
    Posts
    16
    Thanks guys for pointing out to awk and sed.
    After a little research i was able to replace the string by using this command.

    sed 's/CREATE DATABASE/#CREATE DATABASE/' abc.sql | tee abc1.sql

  7. #7
    Linux Guru smolloy's Avatar
    Join Date
    Apr 2005
    Location
    CA, but from N.Ireland
    Posts
    2,413
    I was going to suggest that, but then I noticed that it was a very large file. Didn't it take a long time to pass that monster through sed?
    Registered Linux user #388328 || Registered LFS user #15880
    AMD 64 X2 4600+ :: 2X1GB DDR2 800 :: GeForce 9400 GT 512MB :: ASUS M2N32 Deluxe :: 4X250GB SATAII
    Need instant help? Try us on IRC -- #linuxforums on freenode

  8. #8
    Just Joined!
    Join Date
    Oct 2007
    Posts
    16
    it did
    Now i'm trying to implement awk and cut/edit/paste back to top of file.

    I'll figure it out soon

    thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •