0

I am trying to append to the first line the value of a variable within a loop so, in each iteration the line growth. I am trying

 sed -i '1s/.*/& $(echo "T$(echo "$j-3" | bc -l)_S$k")/' T_iSlicesExtraction_2.csv

so, I am trying to add the value of $(echo "T$(echo "$j-3" | bc -l)_S$k") to the first line. but I am having an issue "scaping" the different special characters, and I am a little bit lost with this. I tried to scape the $ with \ without luck.

  • 1
    Expansions do not happen in strings when they are single-quoted. – Kusalananda May 31 '21 at 08:40
  • face palm..... thanks a loot! – otaolafr May 31 '21 at 08:53
  • Relating: https://unix.stackexchange.com/q/69112/315749 (see also the Q/As the accepted answer links to). – fra-san May 31 '21 at 10:59
  • It's extremely unlikely that sed -i '1s/.*/& $(echo "T$(echo "$j-3" | bc -l)_S$k")/' T_iSlicesExtraction_2.csv is the best way to do what you want. For example awk -i inplace -v j="$j" -v k="$k" 'NR==1{$0="T" $0 (j-3) "_S" k} 1' T_iSlicesExtraction_2.csv would be better than calling 3 separate commands to do such a simple thing. Chances are this line is part of a shell script that'd be much simpler, clearer, more robust, more efficient, and more portable if the whole thing was only written as a single awk script though. – Ed Morton Jun 01 '21 at 13:02
  • it might be, but well I am in learning phase... and at least for the moment eventhought not clean at least I can follow my code that is working correctly (after the correction of Kusalananda). I can not add the answer as I have not enough reputation to do it. but for the moment I am going with that. I should have a look in awk ... – otaolafr Jun 02 '21 at 18:26
  • When in the learning phase is the best time to learn how to do things the right way rather than learning bad habits first and then having to unlearn them and learn the right way later. You would find an awk script clearer, simpler, more robust, more efficient, more portable, etc. than cobbling together a bunch of other commands to manipulate text. See https://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practice for good advice about manipulating text in a shell loop. – Ed Morton Jun 03 '21 at 08:50

0 Answers0