2

I have a large document with over 500000 lines like this

car
milk
dog

And I want to count every line and add the number in the end of line and the final document to look like this

car1
milk1
dog1
car2    
milk2
dog2
car3
milk3
dog3

or like this. It dooesn't matter the order of the output lines

car1
car2
car3
milk1
milk2
milk3
dog1
dog2
dog3

And I created a script for this

for (( i=0; i<=999; i++))
do
awk '{ print $1 '$i' }' inputfile >> outputfile
done

But the script it works just with small files and I don't know why.

  • 1
    "And I want to add in the end of every line a number" . This number, can you clarify what this number is meant to do , or, how you decide what line gets what number, it would help figure out what answer to give you. – clarity123 Oct 4 '15 at 14:35
  • Like in the example. I want to count every line from 1 to 999. – antiks Oct 4 '15 at 14:43
  • 1
    Your script only has one variable (i) but your examples show each distinct string being incremented separately. Which do you want? – steeldriver Oct 4 '15 at 14:51
  • 5
    awk '{print $0""++a[$0]}' <file will do the trick. – cuonglm Oct 4 '15 at 14:53
1

To repeat the whole file n times with a numerical suffix from 1 to n added at each repetition:

n=999
i=1
while [ "$i" -le "$n" ]; do
  sed "s/\$/$i/" <inputfile
  i=$((i+1))
done >outputfile

To repeat each line n times with a numerical suffix from 1 to n:

awk -v n=999 '{for (i=1; i<=n; i++) print $0 i}' <inputfile >outputfile
| improve this answer | |

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.