How to add line numbers (multiples of 5: 0,5,10,15,20) to a text file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to add line numbers (multiples of 5: 0,5,10,15,20) to a text file?
# 8  
Old 01-24-2014
man nl ?
# 9  
Old 01-24-2014
Thanks RudiC, I have gone through the man page for nl
and able to understand the command now.

Code:
-v Number
            Sets the initial logical-page line number to the value specified by the Number variable. The default value of the Number variable is 1. The range of the
            Number variable is from 0 to 32767.
-i Number
            Increments logical-page line numbers by the number specified in the Number variable. The default value of the Number variable is 1. The range of the
            Number variable is from 1 to 250.
-n Format
            Uses the value of the Format variable as the line numbering format. Recognized formats are:
              ln
                   Left-justified, leading zeros suppressed


Thanks,
R. Singh
# 10  
Old 01-24-2014
Quote:
Originally Posted by RudiC
Code:
awk '$0=(NR-1)*5" " $0' file

Shedding a few more characters:
Code:
awk '$0=i++*5" "$0' file

There's a way to reduce it by at least one more character. Does anyone see it?

Regards,
Alister
# 11  
Old 01-25-2014
Code:
awk '$0=i+=5" "$0' file

(not tested, I'm on my phone)
# 12  
Old 01-27-2014
Quote:
Originally Posted by tukuyomi
Code:
$0=i+=5" "$0

That won't work.

In that expression, string concatenation has highest precedence, so, firstly, 5" "$0 is joined. Then, while converting the just-concatenated string to a numeric, += truncates it at the first space, discarding the entire record, $0.

Your suggestion is equivalent to $0=i+=5. Aside from the discarded data, this generates an arithmetic sequence which begins at 5, not 0.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Extracting lines from a text file based on another text file with line numbers

Hi, I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

2. Shell Programming and Scripting

how to add text into the last line of text file

I need help with insert text to the last line of text file with echo command I know can do something like echo "i4\n$logtext\n.\nwq" | ex -s $file can insert to first line, but how can i change this code in order to insert to the last line of text file? please help, thank you :( (2 Replies)
Discussion started by: gavin_L
2 Replies

3. Shell Programming and Scripting

sed add after line x new text from file

I've been playing with sed, trying to get it to insert the contents of somefile.txt after line 13 on anotherfile.txt. I tried searching for a line with regex and attempting to insert something on the next line with: find ./anotherfile.txt -type f -exec sed -i -e '/^dog/cat/' {} \; but it... (2 Replies)
Discussion started by: unclecameron
2 Replies

4. Shell Programming and Scripting

Add ; to every line in text file

Please help to add ; to every line in a text file i Have tired sed 's/$/ ; /g' > /tmp/drop_tables.sql but not working :( Thanks (2 Replies)
Discussion started by: bluebird5m
2 Replies

5. Shell Programming and Scripting

Add one text line in the head of the file

hello, how can I add one text line string at the line number one of a file. thankx, (5 Replies)
Discussion started by: Ahmed waheed
5 Replies

6. Shell Programming and Scripting

how to add line numbers in text file

Hi all How to add line numbers in text file.. ex abcd cdef result 1. abcd 2. cdef thx in advance (4 Replies)
Discussion started by: suryanarayana
4 Replies

7. Shell Programming and Scripting

find 2 line numbers, grab text in between

hi, i have a large text file that I just want to extract the important information from. It will be a random number of lines but between two specific line numbers/markers. I was thinking I could get the line number for the first marker: Tablespace Percent Total Free Then get the line... (11 Replies)
Discussion started by: Da_Duck
11 Replies

8. Shell Programming and Scripting

how to print out line numbers of a text file?

i have this text file name test.txt which contain : aaaaa bbb iiiiiiiiiiiii ccf ddaaa ddd and i need a script that can print out the line numbers using a while loop.. so when the script is run..it will have this: 1 2 3 any ideas? :) thanks guys (4 Replies)
Discussion started by: forevercalz
4 Replies

9. Shell Programming and Scripting

Need to add a comment line in a text file

Hi I need to add a comment line at the begining of a text file. The scenario is given below. 1. The number of servers that needs to be updated is around 80 2. The location of the text file in all the servers are the same including the file name. 3. The comment has to be added at the very... (2 Replies)
Discussion started by: orakhan
2 Replies

10. Shell Programming and Scripting

Add text to file at a certain line

I am trying to add a line of text just before the last line in a file. For example, if the last line of a file is "exit 0", I need to add a line of text just before that. Any ideas how I might do that? Thanks (5 Replies)
Discussion started by: TheCrunge
5 Replies
Login or Register to Ask a Question