LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 01-30-2011, 04:11 AM   #1
ntpntp
LQ Newbie
 
Registered: Jan 2011
Posts: 3

Rep: Reputation: 0
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
 
Old 01-30-2011, 04:20 AM   #2
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
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
 
Old 01-30-2011, 04:46 AM   #3
ntpntp
LQ Newbie
 
Registered: Jan 2011
Posts: 3

Original Poster
Rep: Reputation: 0
Hi,

Thank you very much for your reply.

But do you know how to write it in a sed script?

Thanks
 
Old 01-30-2011, 05:28 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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?
 
Old 01-30-2011, 05:44 AM   #5
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Quote:
Originally Posted by ntpntp View Post
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
 
Old 01-30-2011, 05:52 AM   #6
ntpntp
LQ Newbie
 
Registered: Jan 2011
Posts: 3

Original Poster
Rep: Reputation: 0
Hi,
Thank you very much for your reply!
 
Old 01-30-2011, 05:58 AM   #7
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
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
 
  


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
Sed - Replace all spaces at beginning of line with the number 1. felix001 Programming 12 06-21-2010 12:22 PM
Specifying line number with sed teiben Linux - Newbie 8 06-27-2008 05:54 AM
Print from a line number hammertime1983 Linux - Newbie 3 08-20-2007 07:06 AM
Pass a variable as a line number in sed dx0r515t Programming 7 03-30-2007 04:55 PM
sed / awk command to print line number as column? johnpaulodonnell Linux - Newbie 2 01-22-2007 07:07 AM

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

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