LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 08: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, 08:36 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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, 08: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, 02:38 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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, 05: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, 07:30 PM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
try these:

sed = filename
sed -n = filename

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


Reply



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
cat: output specific number of lines mikeshn Linux - Software 8 01-24-2017 11:08 AM
Substitute specific lines with lines from another file rahmathullakm Programming 4 01-10-2009 05:47 AM
how to print a specific line number with cat new_2_unix Linux - Newbie 2 10-14-2007 10:47 AM
print a text file with long lines and line number added powah Linux - General 2 05-26-2006 02:02 PM
Shell Scripting: How to pick lines out of files by line number. Louie55 Programming 3 03-22-2005 06:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:24 AM.

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
Open Source Consulting | Domain Registration