使用 sed 复制行并从重复项中删除字符

发布于 2024-12-03 14:47:14 字数 383 浏览 1 评论 0原文

我有一个如下所示的文件:

@"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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

洛阳烟雨空心柳 2024-12-10 14:47:14

您不必对正则表达式和替换字符串感到棘手:使用 sed 的 p 命令完整打印该行,然后修改该行并让它隐式打印

sed 'p; s/\.png//'

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 implicitly

sed 'p; s/\.png//'
黑寡妇 2024-12-10 14:47:14

Glenn jackman 的响应是好的,但它也将与表达式不匹配的行加倍。

相反,这个仅将与表达式匹配的行加倍:

sed -n 'p; s/\.png//p'

这里,-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:

sed -n 'p; s/\.png//p'

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

≈。彩虹 2024-12-10 14:47:14

使用 sed 可以很容易地做到这一点,您甚至不需要使用保留空间(sed 辅助缓冲区)。给定下面的input 文件:

$ cat input 
@"Afghanistan.png",
@"Albania.png",
@"Algeria.png",
@"American_Samoa.png",

您应该使用此命令:

sed 's/@"\([^.]*\)\.png",/&\
@"\1",/' input 

结果:

$ sed 's/@"\([^.]*\)\.png",/&\
@"\1",/' input 
@"Afghanistan.png",
@"Afghanistan",
@"Albania.png",
@"Albania",
@"Algeria.png",
@"Algeria",
@"American_Samoa.png",
@"American_Samoa",

此命令只是一个替换命令 (s///)。它匹配以 @" 开头、后跟非句点字符 ([^.]*) 和 .png", 的任何内容。此外,它使用组括号 \(\) 匹配 .png", 之前的所有非句点字符,因此我们可以得到什么因此,这是要替换的正则表达式:

@"\([^.]*\)\.png",

So 跟在命令的替换部分之后 & 命令仅插入与 匹配的所有内容。更改内容中的 @"\([^.]*\)\.png", 如果它是替换部分的唯一元素,则输出中不会发生任何更改。 & 有一个换行符 - 由反斜杠 \ 表示,后跟一个实际的换行符 - 在新行中我们添加 @" 字符串后跟第一组的内容(\1),然后是字符串 ",

这只是该命令的简要说明。希望这会有所帮助。另外,请注意,您可以使用 \n 字符串在某些版本的 sed(例如 GNU sed)中表示换行符,它会呈现更简洁和可读的命令:

sed 's/@"\([^.]*\)\.png",/&\n@"\1",/' input 

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:

$ cat input 
@"Afghanistan.png",
@"Albania.png",
@"Algeria.png",
@"American_Samoa.png",

you should use this command:

sed 's/@"\([^.]*\)\.png",/&\
@"\1",/' input 

The result:

$ sed 's/@"\([^.]*\)\.png",/&\
@"\1",/' input 
@"Afghanistan.png",
@"Afghanistan",
@"Albania.png",
@"Albania",
@"Algeria.png",
@"Algeria",
@"American_Samoa.png",
@"American_Samoa",

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:

@"\([^.]*\)\.png",

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:

sed 's/@"\([^.]*\)\.png",/&\n@"\1",/' input 
眼泪都笑了 2024-12-10 14:47:14

与卡尔斯·萨拉和格伦·杰克曼相比,我更喜欢这个:

sed '/.png/p;s/.png//'

只能说这是个人喜好。

I prefer this over Carles Sala and Glenn Jackman's:

sed '/.png/p;s/.png//'

Could just say it's personal preference.

瑶笙 2024-12-10 14:47:14

或者可以组合两个版本并仅在与所需模式匹配的行上应用复制

sed -e '/^@".*\.png",/{p;s/\.png//;}' input

or one can combine both versions and apply the duplication only on lines matching the required pattern

sed -e '/^@".*\.png",/{p;s/\.png//;}' input
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文