【发布时间】:2020-11-18 01:02:43
【问题描述】:
我有一个文本文件,它由所有以数字代码开头的行组成,后跟一个或多个单词、一个逗号,然后是一个用逗号分隔的单词列表。除了第一个逗号之外,我需要删除每一行中的所有逗号。例如:
1.2.3 Example question, a, question, that, is, hopefully, not, too, rudimentary
which should be changed to
1.2.3 Example question, a question that is hopefully not too rudimentary
我尝试使用 sed 和 shell 脚本来解决这个问题,我可以弄清楚如何删除每行的第一个逗号 (1) 以及如何删除所有逗号 (2),但不知道如何只删除逗号每行第一个逗号后
(1)
while read -r line
do
echo "${line/,/}"
done <"filename.txt" > newfile.txt
mv newfile.txt filename.txt
(2)
sed 's/,//g' filename.txt > newfile.txt
【问题讨论】: