Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to add line numbers to a file or print a file with line numbers? 4

Status
Not open for further replies.

6656

Programmer
Joined
Nov 5, 2002
Messages
104
Location
US
Hi, anyone can give me an example in ksh that How to add line numbers to a file or print a file with line numbers?
Thanks,
Mike
 
man nl

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 

Or cat -n

CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
How about the print files command:

pr -n

Ed
 

All the good answers have been taken.

awk '{printf(&quot;%8d %s\n&quot;, NR,$0)}' file

---
 
And also,

sed = file | sed 'N;s/\n/ /'

Has this been done in pure ksh yet?

Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
not yet:

sed = file.txt | sed 'N;s/\n/ /'
 
With a nod towards tdatgod's solution:

#!/bin/ksh

lineno=0
OIFS=IFS
while IFS=&quot;&&quot; read -r txtline; do
IFS=OIFS
lineno=$((lineno+1))
printf &quot;%8d %s\n&quot; $lineno $txtline
OIFS=IFS
done < d.file

You probably want to use something other than &quot;&&quot; as a field separator. What a hastle!

Regards,


Ed

 
modifying a bit olded idea:

#!/bin/ksh

lineno=0
while txtline=$(line); do
lineno=$((lineno+1))
printf &quot;%8d %s\n&quot; &quot;$lineno&quot; &quot;$txtline&quot;
done < $1


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Borrowing from vlad and going for the Brevety Award...
Code:
#!/bin/ksh

lineno=0

while txtline=$(line); do print &quot;$((lineno += 1)) $txtline&quot;; done < $1

[thumbsup2]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top