Tell me more ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How can I append a line number and tab to the beginning of each line of a text file?

share|improve this question
What I'm wondering is how you'd do it on Windows... – itsadok Jul 21 '09 at 17:37
1  
Should this be on stackoverflow? For superuser the answer is Notepad & lots of patience. – kokos Jul 21 '09 at 22:10
So, do you want to prepend or append. Your title and body texts are different ;) – sirlancelot Jul 22 '09 at 23:52
I guess I want to append to the beginning, "prepend" being only a word to hackers. – Richard Hoskins Jul 23 '09 at 0:15

8 Answers

up vote 13 down vote accepted
awk '{printf "%d\t%s\n", NR, $0}' < filename
share|improve this answer
1  
Awksome. Thnx. – Richard Hoskins Jul 21 '09 at 17:29
2  
Or awk '{print NR, "\t", $0}'. – jtbandes Jul 23 '09 at 1:18
answer below, with nl command line, is a much simpler solution. – Daniel Ribeiro Oct 8 '12 at 20:38

The nl command should do this, but it adds space before the line number too. It's part of Linux coreutils.

nl lines.txt
 1  $bkWTN
 2  $cV8$.
share|improve this answer
Actually, you can tell nl to omit the space before the number. Just use the -w1 option to tell it the minimum width for a number is 1. – cjm Feb 26 at 23:09
sed = test.txt | sed 'N;s/\n/\t/'

The command "sed =" will print the line number followed by a carriage return and then the next line.

The expression "N;s/\n/\t/" will take each line, get the next line (ie line number and the line), and replace the carriage return with a tab.

share|improve this answer
Prints a "t" with no tab with my version of sed: 8t I saw the best minds of my generation destroyed by 9t madness, starving hysterical naked, – Richard Hoskins Jul 21 '09 at 16:49
cat -n <yourfile> | perl -pe "s/^\s*(\d+)\s+/\1\t/"

cat -n adds linenumbers as " 123 linecontents" and that regexp modifies it to "linenumberTABlinecontents"

share|improve this answer
perl -pe "s/^/$.\t$_/" file.txt

or

perl -ne "print qq($.\t$_)" file.txt
share|improve this answer

Ok, since we are collecting ways to do this,

 grep -n . file.txt | sed 's/\(^[0-9]*\):/\1    /g'
 # this is a tab with Ctrl-V + Tab  =====>  ----
share|improve this answer
Now I see this. OK, delete my own post. What? You say that SU doesn't let you delete your own post? – Kevin M Jul 23 '09 at 0:42
grep + sed == awk – Tadeusz A. KadÅ‚ubowski Jul 23 '09 at 12:50
sed file.txt -e 's/^/\t/' | cat -n | sed -e 's/^\t//'

or for some non-GNU seds:

cat file.txt | sed -e 's/^/\\t/' | cat -n | sed -e 's/^\\t//'
share|improve this answer
Prints a "t" with no tab with my version of sed. – Richard Hoskins Jul 21 '09 at 16:54
Edited to double-escape; try it like that. – chaos Jul 21 '09 at 17:18
If all else fails, just hit tab. :) (Or ^V tab if your shell gives you trouble with that.) – chaos Jul 21 '09 at 17:19
sed: 1: "test.txt": undefined label 'est.txt' – Richard Hoskins Jul 21 '09 at 17:24
'cat test.txt | sed -e 's/^/\\t/' | cat -n' prints (tab)number(space)\t(line) – Richard Hoskins Jul 21 '09 at 17:25
show 3 more comments

How about

cat -n somefile.txt

?

share|improve this answer
1  
No tab. (Comments needing 15 chars is a stupid requirement.) – Richard Hoskins Jul 21 '09 at 16:42
This produces tabs on my system. cat (GNU coreutils) 6.10. – innaM Jul 21 '09 at 16:50
On my system, it adds (tab)number(space)line. BSD Utils. – Richard Hoskins Jul 21 '09 at 16:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.