我有一个简单的要求,我有输入likeL

4,23139,,"XYZ"

在一个文本文件中,我需要如下输出
"4","23139","","ARCMsgEnum".

我所写的命令在某些情况下运行良好:
sed 's/[0-9]*[0-9]/"&"/g' inputTestData.txt | sed s/,,/,\"\",/g

但有些行的输入是时间戳:
4,23139,,"XYZ","2008-09-04 08:11:33.51 AM"

所以期望的输出是:
"4","23139","","XYZ","2008-09-04 08:11:33.51 AM"

但是上面的命令给了我如下输出:
"4","23139","","XYZ",""2008"-"09"-"04" "08":"11":"33"."46" AM"

最佳答案

以下可能对您有用:

sed -e 's/"//g' -e 's/[^,]*/"&"/g' inputfile

这将首先去掉引号,然后在逗号内的部分插入引号。
例如:
$ echo 4,23139,,\"XYZ\",\"2008-09-04 08:11:33.51 AM\" | sed -e 's/"//g' -e 's/[^,]*/"&"/g'
"4","23139","","XYZ","2008-09-04 08:11:33.51 AM"

10-06 09:12