LinuxQuestions.org
Review your favorite Linux distribution.
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-05-2013, 10:09 PM   #1
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Rep: Reputation: 30
print with line number


Hi ,

I use vi to edit the file , I know :set number can show the line number on vi , can advise if I would like to print out the content of the file with the line number , what can I do ? thanks
 
Old 09-05-2013, 10:25 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

you can use the nl program.

Cheers,

Evo2.
 
Old 09-05-2013, 10:28 PM   #3
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by evo2 View Post
Hi,

you can use the nl program.

Cheers,

Evo2.
sorry , I tried to google it , but still not found what is nl program , would you please advise how to do it ? thanks
 
Old 09-05-2013, 10:30 PM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

it should be installed on your system (as part of coreutils). It has a perfectly good man page.

Evo2.
 
Old 09-05-2013, 10:31 PM   #5
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
for output without tabs

Code:
awk '{print NR" "$0}' Infile
or, read the nl man

don't know why I offered the awk,, seems nl is better at it

Last edited by Firerat; 09-05-2013 at 10:33 PM.
 
1 members found this post helpful.
Old 09-06-2013, 09:32 AM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,948

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by evo2 View Post
Hi,
it should be installed on your system (as part of coreutils). It has a perfectly good man page.
Evo2.
Yes, but given the OP's posting history, using Google or man pages is NOT something they do. After TEN YEARS, you'd think it would be.
 
2 members found this post helpful.
Old 09-06-2013, 09:42 PM   #7
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (Chicago area)
Distribution: Red Hat (8.0, RHEL5,6), CentOS, SuSE (10.x, 11.x, 12.2, 13.2), Solaris (8-10), Tru64, MacOS, Raspian
Posts: 1,108

Rep: Reputation: 64
Quote:
Originally Posted by ust View Post
I would like to print out the content of the file with the line number , what can I do ? thanks
I'm wondering why everyone is over complicating this. Does your "cat" command not support the "-n" switch?
 
Old 09-07-2013, 07:51 PM   #8
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
You can also use pr, which will allow you a number of options on how handle the numbers, title lines, page sizes...
 
Old 09-08-2013, 10:43 PM   #9
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Quote:
Originally Posted by rnturn View Post
I'm wondering why everyone is over complicating this. Does your "cat" command not support the "-n" switch?
Over complicating? Is "nl <file>" more complicated than "cat -n <file>"?

Evo2.
 
Old 09-09-2013, 12:50 AM   #10
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (Chicago area)
Distribution: Red Hat (8.0, RHEL5,6), CentOS, SuSE (10.x, 11.x, 12.2, 13.2), Solaris (8-10), Tru64, MacOS, Raspian
Posts: 1,108

Rep: Reputation: 64
Quote:
Originally Posted by evo2 View Post
Is "nl <file>" more complicated than "cat -n <file>"?
No though given the number of ways one could skin this particular cat (Oooh... bad pun! Bad pun!) we could make a solution as complicated as we like.

Perhaps "overcomplicating" wasn't the right word. I do tend to use 'cat' a lot more than 'pr' and, frankly, I wouldn't have even known about 'nl' unless I'd read this thread. (Well, I'm sure I've heard of it before but after reading it's manpage probably thought "Oh how nice... 'cat -n' on steroids. I'm sure someone thought that was useful.") I can't recall when I picked up on 'cat -n' but it was a long time ago; could have been something I started using back in my Coherent days. Some of these other commands like 'nl' seem to be relative newcomers to 'IX.
 
Old 09-09-2013, 01:44 AM   #11
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,129

Rep: Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271
Smile

grep -n ^ <filename>
sed = <filename>

Last edited by pan64; 09-09-2013 at 01:48 AM.
 
Old 09-09-2013, 01:59 AM   #12
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

Quote:
Originally Posted by rnturn View Post
No though given the number of ways one could skin this particular cat (Oooh... bad pun! Bad pun!) we could make a solution as complicated as we like.
Please, anything but puns!

Quote:
Perhaps "overcomplicating" wasn't the right word. I do tend to use 'cat' a lot more than 'pr' and, frankly, I wouldn't have even known about 'nl' unless I'd read this thread. (Well, I'm sure I've heard of it before but after reading it's manpage probably thought "Oh how nice... 'cat -n' on steroids. I'm sure someone thought that was useful.")
We sometimes learn one way to do something and and since we are happy with that solution, we might not ever stumble upon other equally valid ways. I don't think I've ever used pr since I've been happy with a2ps and since I've always used nl I don't think I've ever used the -n option for cat.

Quote:
I can't recall when I picked up on 'cat -n' but it was a long time ago; could have been something I started using back in my Coherent days. Some of these other commands like 'nl' seem to be relative newcomers to 'IX.
I think nl was one of the first commands I learnt. It was probably on one of those list of a dozen or so commands that newbs are given when they first get an account on a *nix system. Out of curiosity I checked GNU coreutils nl.c and can see from the copyright that it has been there at least since 1989 (which predates my use of *nix or GNU). Not sure if it was standard on commercial unicies.

Cheers,

Evo2.
 
Old 09-09-2013, 02:39 AM   #13
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (Chicago area)
Distribution: Red Hat (8.0, RHEL5,6), CentOS, SuSE (10.x, 11.x, 12.2, 13.2), Solaris (8-10), Tru64, MacOS, Raspian
Posts: 1,108

Rep: Reputation: 64
Quote:
Originally Posted by pan64 View Post
sed = <filename>
Close but not quite the same as the line numbers are on a separate line.

We could come up with as many ways to number lines in a file as there are "Hello, World" programs.

 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Sed - print line number along with the line ? ntpntp Linux - Newbie 6 01-30-2011 06:58 AM
how to print a specific line number with cat new_2_unix Linux - Newbie 2 10-14-2007 11:47 AM
Print from a line number hammertime1983 Linux - Newbie 3 08-20-2007 08:06 AM
Print line number X of a file (in shell) rheza Programming 4 01-04-2005 06:55 PM
Quick Q: Command to print line number X? KneeLess Linux - Software 1 05-27-2004 08:49 PM


All times are GMT -5. The time now is 10:28 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration