使用 sed 复制行并从重复项中删除字符
我有一个如下所示的文件:
@"Afghanistan.png",
@"Albania.png",
@"Algeria.png",
@"American_Samoa.png",
我希望它看起来像这样
@"Afghanistan.png",
@"Afghanistan",
@"Albania.png",
@"Albania",
@"Algeria.png",
@"Algeria",
@"American_Samoa.png",
@"American_Samoa",
我想我可以使用 sed 来执行此操作,但我不知道如何在缓冲区中存储某些内容然后修改它。
我是否使用了正确的工具?
谢谢
I have a file that looks like this:
@"Afghanistan.png",
@"Albania.png",
@"Algeria.png",
@"American_Samoa.png",
I want it to look like this
@"Afghanistan.png",
@"Afghanistan",
@"Albania.png",
@"Albania",
@"Algeria.png",
@"Algeria",
@"American_Samoa.png",
@"American_Samoa",
I thought I could use sed to do this but I can't figure out how to store something in a buffer and then modify it.
Am I even using the right tool?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您不必对正则表达式和替换字符串感到棘手:使用 sed 的
p
命令完整打印该行,然后修改该行并让它隐式打印You don't have to get tricky with regular expressions and replacement strings: use sed's
p
command to print the line intact, then modify the line and let it print implicitlyGlenn jackman 的响应是好的,但它也将与表达式不匹配的行加倍。
相反,这个仅将与表达式匹配的行加倍:
这里,-n 代表“除非明确打印,否则不打印任何内容”,
s/\.png//p
中的 p 强制打印如果替换已完成,但不强制替换Glenn jackman's response is OK, but it also doubles the rows which do not match the expression.
This one, instead, doubles only the rows which matched the expression:
Here, -n stands for "print nothing unless explicitely printed", and the p in
s/\.png//p
forces the print if substitution was done, but does not force it otherwise使用 sed 可以很容易地做到这一点,您甚至不需要使用保留空间(sed 辅助缓冲区)。给定下面的
input
文件:您应该使用此命令:
结果:
此命令只是一个替换命令 (
s///
)。它匹配以@"
开头、后跟非句点字符 ([^.]*
) 和.png",
的任何内容。此外,它使用组括号\(
和\)
匹配.png",
之前的所有非句点字符,因此我们可以得到什么因此,这是要替换的正则表达式:So 跟在命令的替换部分之后
&
命令仅插入与 匹配的所有内容。更改内容中的@"\([^.]*\)\.png",
如果它是替换部分的唯一元素,则输出中不会发生任何更改。&
有一个换行符 - 由反斜杠\
表示,后跟一个实际的换行符 - 在新行中我们添加@"
字符串后跟第一组的内容(\1
),然后是字符串",
。这只是该命令的简要说明。希望这会有所帮助。另外,请注意,您可以使用
\n
字符串在某些版本的 sed(例如 GNU sed)中表示换行符,它会呈现更简洁和可读的命令:That is pretty easy to do with sed and you not even need to use the hold space (the sed auxiliary buffer). Given the
input
file below:you should use this command:
The result:
This commands is just a replacement command (
s///
). It matches anything starting with@"
followed by non-period chars ([^.]*
) and then by.png",
. Also, it matches all non-period chars before.png",
using the group brackets\(
and\)
, so we can get what was matched by this group. So, this is the to-be-replaced regular expression:So follows the replacement part of the command. The
&
command just inserts everything that was matched by@"\([^.]*\)\.png",
in the changed content. If it was the only element of the replacement part, nothing would be changed in the output. However, following the&
there is a newline character - represented by the backslash\
followed by an actual newline - and in the new line we add the@"
string followed by the content of the first group (\1
) and then the string",
.This is just a brief explanation of the command. Hope this helps. Also, note that you can use the
\n
string to represent newlines in some versions of sed (such as GNU sed). It would render a more concise and readable command:与卡尔斯·萨拉和格伦·杰克曼相比,我更喜欢这个:
只能说这是个人喜好。
I prefer this over Carles Sala and Glenn Jackman's:
Could just say it's personal preference.
或者可以组合两个版本并仅在与所需模式匹配的行上应用复制
or one can combine both versions and apply the duplication only on lines matching the required pattern