LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-29-2009, 09:04 AM   #1
SirTristan
Member
 
Registered: Feb 2006
Distribution: GNU/Linux
Posts: 54

Rep: Reputation: 15
Editing a specific group of lines by line number?


I have a 4GB database dump that I need to edit. Loading this file in an editor like pico would be agonizingly slow obviously - is there any way to edit set of a few lines (say 20, starting with line number 10000) by line number? Perhaps using the sed command.

Also how might one display a specified number of lines, starting with a given line number (again, say 20 lines starting at 10000). I tried
Code:
sed -l 20 -n '46810p' filename.sql
but that doesn't seem to be it.
 
Old 09-29-2009, 09:36 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
The "-l 20" syntax sets the line wrap length for the "l" command. How is that relevant to your goal?

Why not an address range? For example, to print 20 lines starting with #10000:

sed -n '10000,10019p'

OR--in Gnu sed:

sed -n '10000,+19p'

http://www.gnu.org/software/sed/manual/sed.html (Gnu sed--a few extra features)

http://www.grymoire.com/Unix/Sed.html (generic sed---the BEST tutorial ever)
 
Old 09-29-2009, 09:56 AM   #3
SirTristan
Member
 
Registered: Feb 2006
Distribution: GNU/Linux
Posts: 54

Original Poster
Rep: Reputation: 15
Thank you very much!
Quote:
Originally Posted by pixellany View Post
The "-l 20" syntax sets the line wrap length for the "l" command. How is that relevant to your goal?
It is not relevant, I just saw that option in the man page wondered if that was it

So would editing the selected lines in the file consist of something like this?
Code:
sed -n '10000,+19p s/orig/new' filename.sql
Also a couple other questions, if anyone knows:
1) I originally thought this was an editor where you could specify a set of lines and then edit them by hand, but I think that one has to use the substitute command. Yet, when I do something like
Code:
sed -n '10000,+19p' filename.sql
sed seems to hang after displaying select lines. I have to cntrl-c to break out of it. What is happening here?

2) Can one print line numbers before the output?
 
Old 09-29-2009, 03:38 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
You will find most of this in the links I gave you..

For example, this will not work:
Code:
sed -n '10000,+19p s/orig/new'
the fundamental sed syntax is:
[flags] [address] command
within this, some commands allow additional commands to be added "where they make sense".

These are legal:
Code:
sed -n '10000,+19s/orig/new/' 

sed -n '10000,+19p'

sed -n '10000,+19s/orig/new/p'
Beyond this, the best way to get your head around it is to try different things to see what happens.

Quote:
sed seems to hang after displaying select lines. I have to cntrl-c to break out of it. What is happening here?
If it's a long file, perhaps it just takes time to get thru it? You can quit after the selected lines like so:
Code:
sed -n '{2,6p;7q}'
prints lines 2 thru 6, then quits on line 7.

Quote:
2) Can one print line numbers before the output?
Yes
 
Old 09-29-2009, 06:47 PM   #5
SirTristan
Member
 
Registered: Feb 2006
Distribution: GNU/Linux
Posts: 54

Original Poster
Rep: Reputation: 15
Thanks again. Those links have a lot to read, very easy to miss stuff Even when I see what I need it's pretty confusing.

I'm having a lot of trouble getting line numbers to work. No matter where I place the '=' within the pattern, I get an "extra characters after command" error. With something like:
Code:
sed -n '{2,6p;7q}' dump.sql
Where would the '=' go?
 
Old 09-29-2009, 08:30 PM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
try these:

sed = filename
sed -n = filename

Note that combining commands usually requires { ; } (as in one of my examples)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Substitute specific lines with lines from another file rahmathullakm Programming 4 01-10-2009 06:47 AM
how to print a specific line number with cat new_2_unix Linux - Newbie 2 10-14-2007 11:47 AM
print a text file with long lines and line number added powah Linux - General 2 05-26-2006 03:02 PM
Shell Scripting: How to pick lines out of files by line number. Louie55 Programming 3 03-22-2005 07:18 PM
cat: output specific number of lines mikeshn Linux - Software 3 12-31-2003 01:15 PM


All times are GMT -5. The time now is 06:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration