Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > Your Distro > SuSE Linux Help > How to use vi to edit only the top 10 lines file

Forgot Password?
 SuSE Linux Help   For help and discussions related to SuSE Linux

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 10-10-2008   #1 (permalink)
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.
alexsqlforums is offline  


Reply With Quote
Old 10-10-2008   #2 (permalink)
Linux Guru
 
smolloy's Avatar
 
Join Date: Apr 2005
Location: CA, but from N.Ireland
Posts: 2,407
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
smolloy is offline   Reply With Quote
Old 10-10-2008   #3 (permalink)
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
danielsmw is offline   Reply With Quote
Old 10-10-2008   #4 (permalink)
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
danielsmw is offline   Reply With Quote
Old 10-10-2008   #5 (permalink)
Linux Guru
 
smolloy's Avatar
 
Join Date: Apr 2005
Location: CA, but from N.Ireland
Posts: 2,407
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
smolloy is offline   Reply With Quote
Old 10-10-2008   #6 (permalink)
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
alexsqlforums is offline   Reply With Quote
Old 10-10-2008   #7 (permalink)
Linux Guru
 
smolloy's Avatar
 
Join Date: Apr 2005
Location: CA, but from N.Ireland
Posts: 2,407
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
smolloy is offline   Reply With Quote
Old 10-10-2008   #8 (permalink)
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
alexsqlforums is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 10:33 AM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2