LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Editing a specific group of lines by line number? (https://www.linuxquestions.org/questions/linux-newbie-8/editing-a-specific-group-of-lines-by-line-number-758446/)

SirTristan 09-29-2009 08:04 AM

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.

pixellany 09-29-2009 08:36 AM

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)

SirTristan 09-29-2009 08:56 AM

Thank you very much!
Quote:

Originally Posted by pixellany (Post 3700563)
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?

pixellany 09-29-2009 02:38 PM

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

SirTristan 09-29-2009 05:47 PM

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?

pixellany 09-29-2009 07:30 PM

try these:

sed = filename
sed -n = filename

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


All times are GMT -5. The time now is 09:33 PM.