bashshellscriptingsedawk

replace new lines with commas in shell


I want to replace new lines in text with coma or space but do not change the last new line.

I know of this question: How to replace newlines with tab characters? - but it does produce an tab on end instead of new line.

So far I have come with:

awk 'NR>1{printf","} {printf $1} END{printf"\n"}'

Is there an easier way to do this? This is not an assignment, I am just curious want to level up my scripting.


Solution

  • This might work for you:

    paste -s -d, file
    

    or

    paste -s -d" " file