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 ...
- 10-10-2008 #1Just 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.
- 10-10-2008 #2
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
- 10-10-2008 #3
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:
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.Code:head -n 10 file.sql > fileTop wc -l file.sql tail -n (number of lines - 10) > fileBottom
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
- 10-10-2008 #4
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
- 10-10-2008 #5Registered 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
- 10-10-2008 #6Just 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
- 10-10-2008 #7
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
- 10-10-2008 #8Just 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


Reply With Quote

