Append output of a command to file without newline Append output of a command to file without newline unix unix

Append output of a command to file without newline


You just want tr in your case

tr '\n' ','

will replace all the newlines ('\n') with commas

head -1 $line | cut -c22-29 | tr '\n' ',' >> $file


An very old topic, but even now i have been needed to do this (on limited command resources) and that one (replied) command havent been working for me due to its length.

Appending to a file can be done also by using file-descriptors:

  • touch file.txt (create new blank file),

  • exec 100<> file.txt (new fd with id 100),

  • echo -n test >&100 (echo test to new fd)

  • exec 100>&- (close new fd)

Appending starting from specyfic character can be done by reading file from certain point eg.

  • exec 100 <> file.txt - new descriptor

  • read -n 4 < &100 - read 4 characters

  • echo -n test > &100 - append echo test to a file starting from forth character.

  • exec 100>&- - (close new fd)