Apr 23, 2003 #1 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
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
Apr 23, 2003 1 #2 vgersh99 Programmer Joined Jul 27, 2000 Messages 2,146 Location US man nl vlad +----------------------------+ | #include<disclaimer.h> | +----------------------------+ Upvote 0 Downvote
Apr 23, 2003 1 #3 CaKiwi Programmer Joined Apr 8, 2001 Messages 1,294 Location US Or cat -n CaKiwi "I love mankind, it's people I can't stand" - Linus Van Pelt Upvote 0 Downvote
Apr 23, 2003 1 #4 olded Programmer Joined Oct 27, 1998 Messages 1,065 Location US How about the print files command: pr -n Ed Upvote 0 Downvote
Apr 23, 2003 1 #5 tdatgod Programmer Joined Jul 21, 2001 Messages 601 Location US All the good answers have been taken. awk '{printf("%8d %s\n", NR,$0)}' file --- Upvote 0 Downvote
Apr 23, 2003 #6 bigoldbulldog Programmer Joined Feb 26, 2002 Messages 286 Location US And also, sed = file | sed 'N;s/\n/ /' Has this been done in pure ksh yet? Cheers, ND bigoldbulldog@hotmail.com Upvote 0 Downvote
And also, sed = file | sed 'N;s/\n/ /' Has this been done in pure ksh yet? Cheers, ND bigoldbulldog@hotmail.com
Apr 23, 2003 #7 vgersh99 Programmer Joined Jul 27, 2000 Messages 2,146 Location US not yet: sed = file.txt | sed 'N;s/\n/ /' Upvote 0 Downvote
Apr 23, 2003 #8 olded Programmer Joined Oct 27, 1998 Messages 1,065 Location US With a nod towards tdatgod's solution: #!/bin/ksh lineno=0 OIFS=IFS while IFS="&" read -r txtline; do IFS=OIFS lineno=$((lineno+1)) printf "%8d %s\n" $lineno $txtline OIFS=IFS done < d.file You probably want to use something other than "&" as a field separator. What a hastle! Regards, Ed Upvote 0 Downvote
With a nod towards tdatgod's solution: #!/bin/ksh lineno=0 OIFS=IFS while IFS="&" read -r txtline; do IFS=OIFS lineno=$((lineno+1)) printf "%8d %s\n" $lineno $txtline OIFS=IFS done < d.file You probably want to use something other than "&" as a field separator. What a hastle! Regards, Ed
Apr 23, 2003 #9 vgersh99 Programmer Joined Jul 27, 2000 Messages 2,146 Location US modifying a bit olded idea: #!/bin/ksh lineno=0 while txtline=$(line); do lineno=$((lineno+1)) printf "%8d %s\n" "$lineno" "$txtline" done < $1 vlad +----------------------------+ | #include<disclaimer.h> | +----------------------------+ Upvote 0 Downvote
modifying a bit olded idea: #!/bin/ksh lineno=0 while txtline=$(line); do lineno=$((lineno+1)) printf "%8d %s\n" "$lineno" "$txtline" done < $1 vlad +----------------------------+ | #include<disclaimer.h> | +----------------------------+
Apr 23, 2003 #10 olded Programmer Joined Oct 27, 1998 Messages 1,065 Location US Vlad: Good mod! Ed Upvote 0 Downvote
Apr 23, 2003 #11 SamBones Programmer Joined Aug 8, 2002 Messages 3,186 Location US Borrowing from vlad and going for the Brevety Award... Code: #!/bin/ksh lineno=0 while txtline=$(line); do print "$((lineno += 1)) $txtline"; done < $1 Upvote 0 Downvote
Borrowing from vlad and going for the Brevety Award... Code: #!/bin/ksh lineno=0 while txtline=$(line); do print "$((lineno += 1)) $txtline"; done < $1