更新时间:2023-02-19 15:40:42
这应该可以完成工作(cat
和 echo
是不必要的):
This should do the job (cat
and echo
are unnecessary):
tr '\n' ':' < INPUT.TXT | sed 's/:$/\n/'
仅使用 sed
:
sed -n ':a; $ ! {N;ba}; s/\n/:/g;p' INPUT.TXT
没有任何外部的 Bash:
Bash without any externals:
string=($(<INPUT.TXT))
string=${string[@]/%/:}
string=${string//: /:}
string=${string%*:}
在sh
中使用循环:
colon=''
while read -r line
do
string=$string$colon$line
colon=':'
done < INPUT.TXT
使用 AWK:
awk '{a=a colon $0; colon=":"} END {print a}' INPUT.TXT
或者:
awk '{printf colon $0; colon=":"} END {printf "\n" }' INPUT.TXT
这是纯 Bash 的另一种方式:
Here's another way in pure Bash:
string=($(<INPUT.TXT))
saveIFS=$IFS
IFS=':'
newstring="${string[*]}"
IFS=$saveIFS
编辑 2:
这是确实使用echo
的另一种方式:
Here's yet another way which does use echo
:
echo "$(tr '\n' ':' < INPUT.TXT | head -c -1)"