UNIX: Replace Newline w/ Colon, Preserving Newline Before EOF UNIX: Replace Newline w/ Colon, Preserving Newline Before EOF unix unix

UNIX: Replace Newline w/ Colon, Preserving Newline Before EOF


This should do the job (cat and echo are unnecessary):

tr '\n' ':' < INPUT.TXT | sed 's/:$/\n/'

Using only sed:

sed -n ':a; $ ! {N;ba}; s/\n/:/g;p' INPUT.TXT

Bash without any externals:

string=($(<INPUT.TXT))string=${string[@]/%/:}string=${string//: /:}string=${string%*:}

Using a loop in sh:

colon=''while read -r linedo    string=$string$colon$line    colon=':'done < INPUT.TXT

Using AWK:

awk '{a=a colon $0; colon=":"} END {print a}' INPUT.TXT

Or:

awk '{printf colon $0; colon=":"} END {printf "\n" }' INPUT.TXT

Edit:

Here's another way in pure Bash:

string=($(<INPUT.TXT))saveIFS=$IFSIFS=':'newstring="${string[*]}"IFS=$saveIFS

Edit 2:

Here's yet another way which does use echo:

echo "$(tr '\n' ':' < INPUT.TXT | head -c -1)"


Old question, but

paste -sd: INPUT.txt


Here's yet another solution: (assumes a character set where ':' isoctal 72, eg ascii)

perl -l72 -pe '$\="\n" if eof' INPUT.TXT