替换csv中的双引号
我几乎遇到了以下问题,但没有找到解决方案。这可能是我的 CSV 文件结构:
1223;"B630521 ("L" fixed bracket)";"2" width";"length: 5"";2;alternate A
1224;"B630522 ("L" fixed bracket)";"3" width";"length: 6"";2;alternate B
如您所见,在封闭的 "
中有一些为英寸编写的 "
和 "L"
。
现在我正在寻找一个 UNIX shell 脚本来用 2 个单引号替换 "
(英寸)和 "L"
双引号,如下例所示:
sed "s/$OLD/$NEW/g" $QFILE > $TFILE && mv $TFILE $QFILE
任何人都可以帮忙吗我?
I've got nearly the following problem and didn't find the solution. This could be my CSV file structure:
1223;"B630521 ("L" fixed bracket)";"2" width";"length: 5"";2;alternate A
1224;"B630522 ("L" fixed bracket)";"3" width";"length: 6"";2;alternate B
As you can see there are some "
written for inch and "L"
in the enclosing "
.
Now I'm looking for a UNIX shell script to replace the "
(inch) and "L"
double quotes with 2 single quotes, like the following example:
sed "s/$OLD/$NEW/g" $QFILE > $TFILE && mv $TFILE $QFILE
Can anyone help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于“L”,请尝试以下操作:
对于英寸,您可以尝试:
我不确定这是最好的选择,但我已经尝试过并且有效。我希望这有帮助。
For the "L" try this:
For inches you can try:
I am not sure it is the best option, but I have tried and it works. I hope this is helpful.
更新(使用 perl 很容易,因为你可以获得完整的前瞻功能)
输出
仅使用 sed、grep 仅
使用 grep、sed(而不是 perl、php、python 等)不是那么优雅的解决方案 可以是:
输出 - 对于您的输入文件,它给出:
grep -o
基本上是按分割输入;
"
替换为单引号'
"
Update (Using perl it easy since you get full lookahead features)
Output
Using sed, grep only
Just by using grep, sed (and not perl, php, python etc) a not so elegant solution can be:
Output - for your input file it gives:
grep -o
is basically splitting the input by;
"
by single quite'
"
at the start and end也许这就是您想要的:
即:在数字(
[0-9]
)后面查找双引号("
),但后面不跟分号([ ^;]
)并将其替换为两个单引号编辑: 。
我可以扩展我的命令(现在它变得很长):
当您使用 SunOS 时,我猜您不能使用扩展正则表达式(
sed -r
)?因此我这样做了:第一个s
命令将所有英寸"
替换为''
,第二个和第三个s< /code> 是相同的。它们将所有不是
;
直接邻居的"
替换为单个'
。我必须执行两次才能替换例如"L"
的第二个"
因为"
和此之间只有一个字符字符已与\([^;]\)
匹配。这样您也可以将""
替换为''
。如果您有"""
或""""
等,则必须再添加一个(但只能再添加一个)s
。Maybe this is what you want:
I.e.: Find double quotes (
"
) following a number ([0-9]
) but not followed by a semicolon ([^;]
) and replace it with two single quotes.Edit:
I can extend my command (it's becoming quite long now):
As you are using SunOS I guess you cannot use extended regular expressions (
sed -r
)? Therefore I did it that way: The firsts
command replaces all inch"
with''
, the second and the thirds
are the same. They substitute all"
that are not a direct neighbor of a;
with a single'
. I have to do it twice to be able to substitute the second"
of e.g."L"
because there's only one character between both"
and this character is already matched by\([^;]\)
. This way you would also substitute""
with''
. If you have"""
or""""
etc. you have to put one more (but only one more)s
.