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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
01-30-2011, 04:11 AM
|
#1
|
|
LQ Newbie
Registered: Jan 2011
Posts: 3
Rep:
|
Sed - print line number along with the line ?
Hi,
I want to print the line number with the pattern of the line on a same line using multi-patterns in sed.
But i don't know how to do it.
For example, I have a file
abc
def
ghi
I want to print
1 abc
2 def
3 ghi
Does anyone know how to do it?
Thanks
|
|
|
|
01-30-2011, 04:20 AM
|
#2
|
|
Guru
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 18 with Awesome WM
Posts: 6,805
|
Hello,
You can use sed's = command to add line numbers if that's what you mean:
Code:
sed = yourfilename | sed 'N;s/\n/ /'
will print your file to screen with the line number in front of every line, followed by a space. If you want to redirect to another file, then use:
Code:
sed = yourfilename | sed 'N;s/\n/ /' > newfile
Kind regards,
Eric
|
|
|
|
01-30-2011, 04:46 AM
|
#3
|
|
LQ Newbie
Registered: Jan 2011
Posts: 3
Original Poster
Rep:
|
Hi,
Thank you very much for your reply.
But do you know how to write it in a sed script?
Thanks
|
|
|
|
01-30-2011, 05:28 AM
|
#4
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,381
|
Maybe you could explain why you need a script to perform a one line task?
Ultimately you have been given the solution, so should it not be up to you to work out how to put into a script?
|
|
|
|
01-30-2011, 05:44 AM
|
#5
|
|
Guru
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 18 with Awesome WM
Posts: 6,805
|
Quote:
Originally Posted by ntpntp
Hi,
Thank you very much for your reply.
But do you know how to write it in a sed script?
Thanks
|
Hi,
As indicated by grail, that should be up to you. You should put in the work and find out how to do it since we will not do all the work for you and give you a ready made solution. However if you already have put something together we'll gladly help you in fixing any errors or troubles you might encounter.
Kind regards,
Eric
|
|
|
|
01-30-2011, 05:52 AM
|
#6
|
|
LQ Newbie
Registered: Jan 2011
Posts: 3
Original Poster
Rep:
|
Hi,
Thank you very much for your reply!
|
|
|
|
01-30-2011, 05:58 AM
|
#7
|
|
Guru
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 18 with Awesome WM
Posts: 6,805
|
Hi,
You're welcome! If you consider this question/problem solved then please mark it as such using the Thread Tools. If you want to continue using this thread to post and query the script you want/need then of course you can do that, or you can open a new thread.
Kind regards,
Eric
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 05:33 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|