numbering lines in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers numbering lines in a file
# 1  
Old 02-19-2012
numbering lines in a file

if we execute
Code:
:set nu

in vi mode, it displays the line numbers. so how to make this permanently in a file.
Whenever i execute cat , the line numbers should be there. please help me.
thanks
# 2  
Old 02-19-2012
1. i=1; while read x; do echo "$i $x"; i=$(($i+1));done < inputfile

2. perl -i.bak -pe 's/^/$. /' inputfile --> Original file would be retained as inputfile.bak
# 3  
Old 02-19-2012
Quote:
Originally Posted by pandeesh
if we execute
Code:
:set nu

in vi mode, it displays the line numbers. so how to make this permanently in a file.
Whenever i execute cat , the line numbers should be there. please help me.
thanks
Assuming you don't want the line numbers actually in the file, just shown when you cat the file, define an alias 'ncat' (probably not a good idea to alias cat):

Code:
alias ncat="awk '{ printf( \"%4d %s\n\", NR, \$0 ); }'"

Each time you execute something like ncat /tmp/foo the output will be prefixed with line numbers.
# 4  
Old 02-19-2012
Code:
man cat | grep -i number

# 5  
Old 02-19-2012
There is UNIX utility which job is just that:
Code:
nl file > new.file

This User Gave Thanks to bartus11 For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Numbering file's lines

hey a file called test : Code: hey1 hey2 hey3 ........ how to : Code: 1.hey1 2.hey2 3.hey3 .......... (3 Replies)
Discussion started by: eawedat
3 Replies

2. Shell Programming and Scripting

help with numbering a file

Hi, All I need to do is number a file. The file looks like this > JJJJJJJJJJJJJJJJJJJJJ > JKJKJKKKKKKJJJ > MMMMYKKKJKKK what I want to do is number it so that theres a numerical value beside the >. >1 JJJJJJJJJJJJJJJJJJJJJ >2 JKJKJKKKKKKJJJ (2 Replies)
Discussion started by: kylle345
2 Replies

3. UNIX for Dummies Questions & Answers

Ghostscript output file numbering?

I am using ghostscript to convert a multi-page pdf file to individual jpg files. I am wondering if there is a way to get ghostscript to start numbering the output jpg files from zero? What i am trying to convey is that it starts naming my files from page_001.jpg, page_002.jpg, etc., and would like... (0 Replies)
Discussion started by: RacerX
0 Replies

4. Shell Programming and Scripting

numbering each line in a text file

I have a simple text file. I want to number each line in that file . for example: My text file is unix my file test My output should be 1 unix 2 my file 3 test (5 Replies)
Discussion started by: pitagi
5 Replies

5. Shell Programming and Scripting

numbering lines according to a condition

hello again guys, I tried to make a script but due to array's limitations I didn't succeed...so I'm asking you :) I need numbering the lines according to date (everyday I need to restart the counter) for example: ABCBD 20080101 XXX 1 FSDFD 20080101 BBB 2 FSDFD 20080102 HHH 1 and so... (3 Replies)
Discussion started by: elionba82
3 Replies

6. Shell Programming and Scripting

Numbering a Text File

Running into a little problem with blank lines. My file is of this format: To number each line of the file i would use: n=1 echo "$FILE" | while read line do echo "$n) $line" n=`expr $n + 1` But really, i dont want to number the blank lines. What i've tried is to use sed... (13 Replies)
Discussion started by: omgsomuchppl
13 Replies

7. UNIX for Dummies Questions & Answers

Numbering lines grep

Is there a way to number lines and use something other than ":" to separate the number from the line? Or can anyone recommend a way to replace the ":" with a tab? Here's what I'm working with: 16859:52.67 16860:46 16861:39.63 16862:33.88 16863:29.64 16864:26.27 16865:22.09... (1 Reply)
Discussion started by: jdolny
1 Replies

8. Shell Programming and Scripting

Numbering Lines

Hello everyone, I want get numbered lines from a file. and i can do it with: sed = file.txt | sed "/./N; s/\n/ /" | sed -n "5,7p" but the output that i get is something similar to: 5 line5 6 line6 7 line7 and i want something like this (with 2points after the number): 5:... (6 Replies)
Discussion started by: vibra
6 Replies

9. Windows & DOS: Issues & Discussions

Numbering lines in a file

Hi all, I need to number the lines in a file. I tried using "set nu" in the vi editor, but it is only temporary. Can anyone help me please. Thanx in advance. MK (1 Reply)
Discussion started by: minazk
1 Replies

10. Shell Programming and Scripting

Numbering lines in a file

Hi all, I need to number the lines in a file. I tried using "set nu" in the vi editor, but it is only temporary. Can anyone help me please. Thanx in advance. MK (4 Replies)
Discussion started by: minazk
4 Replies
Login or Register to Ask a Question