Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append output of a command to file without newline

I have the following line in a unix script:

head -1 $line | cut -c22-29  >> $file

I want to append this output with no newline, but rather separated with commas. Is there any way to feed the output of this command to printf? I have tried:

head -1 $line | cut -c22-29 | printf "%s, " >> $file

I have also tried:

printf "%s, " head -1 $line | cut -c22-29 >> $file

Neither of those has worked. Anyone have any ideas?

like image 633
KateMak Avatar asked Jan 21 '14 00:01

KateMak


People also ask

How do I append a message to a file without a newline character?

Assuming that the file does not already end in a newline and you simply want to append some more text without adding one, you can use the -n argument, e.g. However, some UNIX systems do not provide this option; if that is the case you can use printf , e.g. Do not print the trailing newline character.

How can you append the output of a command to a file?

Append to a File using the Redirection Operator ( >> ) Redirection allows you to capture the output from a command and send it as input to another command or file. The >> redirection operator appends the output to a given file.

Which command is used to append some text at the end of some file?

Append Text Using >> Operator For example, you can use the echo command to append the text to the end of the file as shown. Alternatively, you can use the printf command (do not forget to use \n character to add the next line).


1 Answers

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
like image 124
bdrx Avatar answered Oct 23 '22 22:10

bdrx