The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Adding a columnfrom a specifit line number to a specific line number Ezy Shell Programming and Scripting 2 05-12-2008 05:29 AM
Appending line number to each line and getting total number of lines chiru_h Shell Programming and Scripting 2 03-25-2008 06:19 AM
Appending the line number and a seperator to each line of a file ? pjcwhite Shell Programming and Scripting 4 03-20-2007 09:29 PM
input a line at the beginning of every file in a directory? Terrible Shell Programming and Scripting 6 08-16-2006 03:42 AM
identifying duplicates line & reporting their line number stresslog UNIX for Dummies Questions & Answers 5 04-23-2006 09:43 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-16-2006
Registered User
 

Join Date: Jun 2006
Posts: 3
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.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 06-16-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,002
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 '$='
Reply With Quote
  #3 (permalink)  
Old 06-18-2006
Registered User
 

Join Date: Jun 2006
Posts: 3
Question

How do I run this on the Unix Terminal?
# count lines (emulates "wc -l")
sed -n '$='
Reply With Quote
  #4 (permalink)  
Old 06-19-2006
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,211
You can also do :
Code:
cat -n /path/to/file

Jean-Pierre.
Reply With Quote
  #5 (permalink)  
Old 06-19-2006
Registered User
 

Join Date: Jun 2006
Posts: 3
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.
Reply With Quote
  #6 (permalink)  
Old 06-19-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,859
/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>
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:37 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0