0

I Have a huge file (50000 rows) looks like :

input.txt:

123315334262625363534
265343426272282827262
224343536625242536363
635352627273635373732
363353533637383838327
635342422325337474524

I want to add row numbers as the first column at the beginning of the file while each number repeated two times:

1 123315334262625363534
1 265343426272282827262
2 224343536625242536363
2 635352627273635373732
3 363353533637383838327
3 635342422325337474524

any suggestion please?

1

With awk:

awk '{getline l; printf "%d %s\n%d %s\n", ++i, $0, i, l}' <in >out
  • thanks, if I want the row number do not be repeated then how should I change this? I mean if I want it to be like 1 2 3 ... – zara Oct 5 '15 at 19:02
  • @zara: Just awk '{print FNR, $0}' <in >out. – cuonglm Oct 5 '15 at 19:03
0

Other awk

awk '{print ++count, $0}NR%2{count--}' <in >out

sed + nl

sed 'N;s/\n/@/' <in |
nl |
sed 's/\(\(^\s*[0-9]*\s*\).*\)@/\1\n\2/' >out
0
perl -pe 'printf "%d ", .5+$./2'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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