LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-09-2009, 05:29 AM   #1
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

Rep: Reputation: 30
sed help needed


Hi all,

Am on the way of learning sed using Bash-Beginners-Guide.pdf. Suddenly i got questions myself of how to print the lines , say 5-10 in a file using sed.

1.How to print a line ,say 28 only in a file using sed?
2.How to print from line 5-10 in a file ?
3.How can list all my files in my home directory using sed?

Can someone suggest me a dedicated document(pdf pref) for "sed"

Thanks

Last edited by ZAMO; 01-09-2009 at 05:34 AM.
 
Old 01-09-2009, 05:33 AM   #2
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
Here's a really good sed tutorial for you

http://www.ibm.com/developerworks/li...ry/l-sed1.html

Why would you use sed to list files in your home directory?
 
Old 01-09-2009, 05:37 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986
Here is another really good sed tutorial:

http://www.grymoire.com/Unix/Sed.html
 
Old 01-09-2009, 05:38 AM   #4
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

Original Poster
Rep: Reputation: 30
Code:
Why would you use sed to list files in your home directory?

It is there in the Exercise part of the pdf and it is interesting to know , sed to list the files in a directory.
 
Old 01-09-2009, 06:17 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 683Reputation: 683Reputation: 683Reputation: 683Reputation: 683Reputation: 683
You might use sed to reformat the list of files in your home directory, but the list will be supplied either by file globbing or ls or find.

If you move on to learning awk as well, I would recommend downloading the source package for 'gawk' and using "make pdf" to produce the "GAWK: Effective Awk Programming".

The "Sed & Awk" O'Reilly book is very good. The first edition might be available as a pdf on the web.
 
Old 01-09-2009, 06:34 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986
Quote:
Originally Posted by jschiwal View Post
If you move on to learning awk as well, I would recommend downloading the source package for 'gawk' and using "make pdf" to produce the "GAWK: Effective Awk Programming"
The pdf is also available online, here.
 
Old 01-09-2009, 06:44 AM   #7
ZAMO
Member
 
Registered: Mar 2007
Distribution: Redhat &CentOS
Posts: 598

Original Poster
Rep: Reputation: 30
Thank you colucix and jschiwal,

I am having this PDF for gawk for a very long time and used to go through it. It will be nice if I get a similar kind of one for "sed".
 
Old 01-09-2009, 06:58 AM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
The grymoire site linked earlier also has a good section on AWK.

Back to the questions:
Quote:
1.How to print a line ,say 28 only in a file using sed?
2.How to print from line 5-10 in a file ?
What you want is called "addressing". The general form is:

sed '<address1>,<address2> action'

either <address1> or <address2> can be a line number or a Regex. <address2> can also be a numerical increment or interval.

Examples:

Code:
sed -n '3p' filename     ##prints only line 3

sed -n '3,6p' filename    ##prints lines 3-6

sed -n '/the/p' filename    ##prints all lines containing "the"

sed -n '/the/,/end/p' filename    ##prints lines beginning with one containing "the" and ending with one containing "end"

finally, note that Gnu SED has many extensions that do not appear in the Unix tutorials. For this, look at "info sed" in a terminal, or Google for the official Gnu SED manual.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
basic sed help needed davimint Programming 6 02-11-2008 09:06 AM
sed help needed avijitp Programming 8 11-19-2007 08:33 AM
awk or sed help needed cmontr Programming 42 11-02-2007 11:43 PM
SED help needed DriveMeCrazy Linux - General 2 11-20-2006 10:04 PM
Help on sed and perl needed. cash_05 Programming 7 09-03-2005 04:33 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:55 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