使用 awk 替换 `'` 字符
我想删除其中包含单个 :
和 '
的行。我想使用 awk 来实现此目的。我尝试过使用:
awk '{gsub ( "[:\\']","" ) ; print $0 }'
并且
awk '{gsub ( "[:\']","" ) ; print $0 }'
它们
awk '{gsub ( "[:']","" ) ; print $0 }'
都不起作用,但返回错误Unmatched“。
。当我放置
awk '{gsub ( "[:_]","" ) ; print $0 }'
然后它起作用并删除所有:
和_
字符。如何去掉 '
字符?
I have lines with a single :
and a'
in them that I want to get rid of. I want to use awk
for this. I've tried using:
awk '{gsub ( "[:\\']","" ) ; print $0 }'
and
awk '{gsub ( "[:\']","" ) ; print $0 }'
and
awk '{gsub ( "[:']","" ) ; print $0 }'
non of them worked, but return the error Unmatched ".
. when I put
awk '{gsub ( "[:_]","" ) ; print $0 }'
then It works and removes all :
and _
chars. How can I get rid of the '
char?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
tr
就是为此目的而制作的tr
is made for this purpose这有效:
This works:
您可以使用:
单引号的八进制代码:
<前><代码>[:\47]
双引号内的单引号,但在这种情况下特殊
字符将被 shell 扩展:
使用动态正则表达式,但存在相关的性能影响
对于这种方法:
You could use:
Octal code for the single quote:
The single quote inside double quotes, but in that case special
characters will be expanded by the shell:
Use a dynamic regexp, but there are performance implications related
to this approach:
使用 bash,您无法在用单引号括起来的文字内插入单引号。例如使用
'"'"'
。首先
'
关闭当前文字,然后"'"
将其与仅包含单引号的文字连接起来,然后'
重新打开字符串文字,这也将被串联起来。你想要的是:
ssapkota 的替代方案也不错(
'\''
)。With bash you cannot insert a single quote inside a literal surrounded with single quotes. Use
'"'"'
for example.First
'
closes the current literal, then"'"
concatenates it with a literal containing only a single quote, and'
reopens a string literal, which will be also concatenated.What you want is:
ssapkota's alternative is also good (
'\''
).我不知道你为什么限制自己使用 awk,无论如何你已经从其他用户那里得到了很多答案。您还可以使用 sed 去掉“:'”,
这也将达到您的目的。简单且不太复杂。
I don't know why you are restricting yourself to using awk, anyways you've got many answers from other users. You can also use sed to get rid of " :' "
This will also serve your purpose. Simple and less complex.
这也有效:
awk '{gsub("\x27","");打印}'
This also works:
awk '{gsub("\x27",""); print}'
最简单的
awk '{gsub(/\047|:/,"")};1'
simplest
awk '{gsub(/\047|:/,"")};1'