Numbering Lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Numbering Lines
# 1  
Old 08-22-2008
Network 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: line5
6: line6
7: line7


but without using loops to do it, like
for i++
then
echo "i: sed -n "{i}p" file.txt"
end


I really want avoid loops. i hate them in bash scripts! Smilie
# 2  
Old 08-22-2008
simple write this..
Quote:
sed = file.txt | sed "/./N; s/\n/: /;5,7p"
# 3  
Old 08-22-2008
awk '{print NR": "$0}' filename
but one condition it won't check for empty lines..
# 4  
Old 08-22-2008
simply you can use this also... :-)
Quote:
nl -b t -s ": " filename
# 5  
Old 08-22-2008
whow man!!!

thanks for the fast replay...

this sed = file.txt | sed "/./N; s/\n/: /;5,7p" solved my problem!

-- EDIT --

well, i used a little different approach: sed = file.txt | sed "/./N; s/\n/: /" | sed -n "5,7p". I couldn't use the above commands set...

Last edited by vibra; 08-22-2008 at 08:42 AM.. Reason: wrong command
# 6  
Old 08-22-2008
cat -n filename (won't put ':' but will do the job)
# 7  
Old 08-22-2008
IMO, nl seems to be the easiest, most lightweight choice. cat is up there as well.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

numbering lines in a file

if we execute :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 (4 Replies)
Discussion started by: pandeesh
4 Replies

2. 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

3. Shell Programming and Scripting

Numbering duplicates

Hi, I have this large file and sometimes there are duplicates and I want to basically find them and figure how many there are. So I have a file with multiple columns and the last column (9) has the duplicates. eg. yan tar tar man ban tan tub tub tub Basically what I want to... (6 Replies)
Discussion started by: kylle345
6 Replies

4. 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

5. 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

6. 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

7. 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

8. Shell Programming and Scripting

Numbering

I'm trying to do a script that will look for a log file if it is already there change the name to another name. I.E if log.0 is there rename to log.1 rename log.1 to log.2 rename log.2 to log.3 and so on. Only thing is I got no idea where or what is the best command to use for this? ... (3 Replies)
Discussion started by: merlin
3 Replies

9. UNIX for Dummies Questions & Answers

Numbering!

Just a shot question... how to make 1,2,3,...999 into the form of 001,002,003....999 (3 digits) Thanks.... (9 Replies)
Discussion started by: biglemon
9 Replies

10. UNIX for Dummies Questions & Answers

numbering of process

:confused: How does UNIX handle the numbering of processes? (2 Replies)
Discussion started by: tweety111
2 Replies
Login or Register to Ask a Question

Featured Tech Videos