Add numbers to the beginning of every line in a file
Asked Answered
H

7

66

How can I add numbers to the beginning of every line in a file?

E.g.:

This is
the text
from the file.

Becomes:

000000001 This is
000000002 the text
000000003 from the file.
Herschel answered 21/11, 2011 at 1:34 Comment(0)
S
39

AWK's printf, NR and $0 make it easy to have precise and flexible control over the formatting:

~ $ awk '{printf("%010d %s\n", NR, $0)}' example.txt
0000000001 This is
0000000002 the text
0000000003 from the file.
Stanfill answered 21/11, 2011 at 1:38 Comment(1)
Wonderful! I needed this to populate a huge array of static int declarations, so I had something like public static final int CONSTANTX; and I wanted something like public static final int CONSTANTX;=X where X is the number of the line in the source file. I rearranged your answer as awk '{printf("%s = %d;\n", $0, NR-4)}' Constants.java and it worked like a charm!Subside
D
125

Don't use cat or any other tool which is not designed to do that. Use the program:

nl - number lines of files

Example:

$ nl --number-format=rz --number-width=9 foobar
$ nl -n rz -w 9 foobar # short-hand

Because nl is made for it ;-)

Dissonance answered 21/11, 2011 at 1:38 Comment(4)
ls | nl #nl for dummiesFormalism
nl doesn't really help in all scenarios. see below: $ cat test123 123 sd $ nl test123 1 123 2 sd $ awk '{print NR,$0}' test123 1 123 2 3 sdWoodbury
It doesn't output the lines until EOF is read. So you can't use it to number output of a long-running program on-line.Vibraharp
@Woodbury Use -ba switch.Fibrovascular
S
39

AWK's printf, NR and $0 make it easy to have precise and flexible control over the formatting:

~ $ awk '{printf("%010d %s\n", NR, $0)}' example.txt
0000000001 This is
0000000002 the text
0000000003 from the file.
Stanfill answered 21/11, 2011 at 1:38 Comment(1)
Wonderful! I needed this to populate a huge array of static int declarations, so I had something like public static final int CONSTANTX; and I wanted something like public static final int CONSTANTX;=X where X is the number of the line in the source file. I rearranged your answer as awk '{printf("%s = %d;\n", $0, NR-4)}' Constants.java and it worked like a charm!Subside
E
32

You're looking for the nl(1) command:

$ nl -nrz -w9  /etc/passwd
000000001   root:x:0:0:root:/root:/bin/bash
000000002   daemon:x:1:1:daemon:/usr/sbin:/bin/sh
000000003   bin:x:2:2:bin:/bin:/bin/sh
...

-w9 asks for numbers nine digits long; -nrz asks for the numbers to be formatted right-justified with zero padding.

Ethnomusicology answered 21/11, 2011 at 1:38 Comment(6)
That's the exact answer and a good one, but for a homework style question it may have been better to offer a hint, a link, or a more general purpose solution.Stanfill
@Raymond, true enough, but I've not seen any indication that this is homework. Simple? Sure. But there's a giant pile of text-processing utilities on these things that even experienced developers never learn about.Ethnomusicology
@Raymond: I suggest undeleting your answer, I like it. Different tools might excel for different reasons, and awk(1) certainly excels at tasks like this.Ethnomusicology
I only knew because I fell for the a previous question a few minutes ago: #8206780 Also, the unusual number of leading zeros in the example was another hint :-)Stanfill
This is not for homework. I just thought, it could take me 2 hours to figure out how to do that in awk, using some for loop, which would take another 30 minutes for the script to run through the file; or I could ask here. I've never heard of nl before. Am I posting the wrong kind of questions?Herschel
Village: not at all -- Stack Overflow is intended to answer questions of all levels. @Raymond just noticed that you've got three questions asked in a short time frame asking for solutions to problems similar to homework a professor might assign in a "Learning Unix" course. One alone sounds like someone looking for a solution to a problem, but all three taken together makes us wonder if we're doing your homework for you. :)Ethnomusicology
T
17

cat -n thefile will do the job, albeit with the numbers in a slightly different format.

Terrorism answered 21/11, 2011 at 1:36 Comment(0)
W
8

Easiest, simplest option is

awk '{print NR,$0}' file

See comment above on why nl isn't really the best option.

Woodbury answered 21/3, 2016 at 15:14 Comment(0)
L
6

Here's a bash script that will do this also:

#!/bin/bash
counter=0
filename=$1
while read -r line
do
  printf "%010d %s" $counter $line
  let counter=$counter+1
done < "$filename"
Landlubber answered 21/1, 2014 at 15:43 Comment(0)
C
4
perl -pe 'printf "%09u ", $.' -- example.txt
Caulfield answered 21/11, 2011 at 15:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.