shell replace cr\lf by comma

2020-02-26 02:59发布

I have input.txt

1
2
3
4
5

I need to get such output.txt

1,2,3,4,5

How to do it?

8条回答
唯我独甜
2楼-- · 2020-02-26 03:26

tr and sed used be very good but when it comes to file parsing and regex you can't beat perl (Not sure why people think that sed and tr are closer to shell than perl... )

perl -pe 's/\n/$1,/' your_file

if you want pure shell to do it then look at string matching

${string/#substring/replacement}
查看更多
叛逆
3楼-- · 2020-02-26 03:31

Try this:

tr '\n' ',' < input.txt > output.txt
查看更多
登录 后发表回答