-1

I am trying to place a different sting at the end of each line using. The below works but not when the file is large. x=line # in the file. The data goes to the end with $.

Let data=0
Let x=0
while [ $data -lt 10 ]
do
data=$(($data+1))

x=$(($x+11))

sed -i ''"$x"'s/$/ '"$data"' /' file
. done

What I am trying to do.
Before:file_name

abc abc abc
abc abc abc
abc abc abc

After:file_name
abc abc abc 1
abc abc abc 2
abc abc abc 3
etc...

0

You just need this:

sed -i 's/$/ '"$data"'/' filename

The regular expression $ matches at the end of the line.

Note, to substitute the value of a variable, you don't need to echo it.

  • Each line will have a different value, How do I reference each line?. Thank you. – joseph rob Aug 31 '15 at 20:32
  • This seems to work. sed -i ''"$x"'s/$/ '"$data"' /' test_data.. . Just need a push in the right Thanks Glen . – joseph rob Aug 31 '15 at 20:34
  • Trying using sed -i ''"$x"'s/$/ '"$data"' /' filename getting weird out put \r – joseph rob Aug 31 '15 at 21:01
  • Are you saying that $data is different for each line? – Andy Dalton Aug 31 '15 at 21:34
  • 1
    Sorry, you have not increased the clarity of your question in any way. – glenn jackman Sep 1 '15 at 21:02

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.