Unix Script with line number at beginning of each line. | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Unix Script with line number at beginning of each line.

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-16-2006
mascorro mascorro is offline
Registered User
 
Join Date: Jun 2006
Last Activity: 19 June 2006, 7:07 PM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Unix Script with line number at beginning of each line.

Could anybody help me.
I need to create a script that reads a text file from STDIN and prints out the file to STDOUT with line numbers at the beginning of each line.

Thanks.
Sponsored Links
    #2  
Old 06-16-2006
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 22 September 2014, 9:42 AM EDT
Location: Foxborough, MA
Posts: 7,650
Thanks: 150
Thanked 580 Times in 545 Posts
Quote:
Originally Posted by mascorro
Could anybody help me.
I need to create a script that reads a text file from STDIN and prints out the file to STDOUT with line numbers at the beginning of each line.

Thanks.
'man nl'

you could also check sed1liners

here's the relative snippet:
Quote:
Originally Posted by sed1liners
NUMBERING:

# number each line of a file (simple left alignment). Using a tab (see
# note on '\t' at end of file) instead of space will preserve margins.
sed = filename | sed 'N;s/\n/\t/'

# number each line of a file (number on left, right-aligned)
sed = filename | sed 'N; s/^/ /; s/ *\(.\{6,\}\)\n/\1 /'

# number each line of file, but only print numbers if line is not blank
sed '/./=' filename | sed '/./N; s/\n/ /'

# count lines (emulates "wc -l")
sed -n '$='
Sponsored Links
    #3  
Old 06-19-2006
mascorro mascorro is offline
Registered User
 
Join Date: Jun 2006
Last Activity: 19 June 2006, 7:07 PM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Question

How do I run this on the Unix Terminal?
# count lines (emulates "wc -l")
sed -n '$='
    #4  
Old 06-19-2006
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
 
Join Date: Apr 2004
Last Activity: 20 January 2014, 5:51 AM EST
Location: Bordeaux, France
Posts: 1,711
Thanks: 2
Thanked 61 Times in 57 Posts
You can also do :

Code:
cat -n /path/to/file


Jean-Pierre.
Sponsored Links
    #5  
Old 06-19-2006
mascorro mascorro is offline
Registered User
 
Join Date: Jun 2006
Last Activity: 19 June 2006, 7:07 PM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
In The terminal. It shows that I do not have a file or directory with that name.
cat -n /path/to/file

Thanks for any help you could give me.
Sponsored Links
    #6  
Old 06-19-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 22 September 2014, 7:30 AM EDT
Location: NM
Posts: 10,211
Thanks: 278
Thanked 796 Times in 743 Posts
/path/to/file is an example file name not a real file name

A complete filename (example, not real) is /home/mascorro/text.lis
The path is /home/mascorro
the file is named text.lis

Plus vgersh99 gave you the answer


Code:
nl <filename of your choice goes here>

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
add a number to the beginning of every line boaz733 Shell Programming and Scripting 5 12-15-2011 09:39 AM
sed script - print the line number along with the line ntpntp Shell Programming and Scripting 11 01-31-2011 12:20 AM
Shell script to count number of ~ from each line and compare with next line Ganesh Khandare Shell Programming and Scripting 3 09-29-2010 04:06 PM
How to read contents of a file from a given line number upto line number again specified by user grc UNIX for Dummies Questions & Answers 5 07-13-2010 07:14 AM
Adding a columnfrom a specifit line number to a specific line number Ezy Shell Programming and Scripting 2 05-12-2008 08:29 AM



All times are GMT -4. The time now is 10:13 AM.