使用SED删除输出中的文本字符串。

huangapple go评论59阅读模式
英文:

Delete string of text of output using SED

问题

我已经访问了 https://sed.js.org/https://regex101.com,最后是 https://tecadmin.net/delete-a-line-containing-specific-string-using-sed/。我无法删除目录字符串中的前端和尾部的斜杠。这将被设置为bash中的一个变量。期望的命令是:dir=$(sed ...)

文件夹:/config/folder/donwloads/Video/Epoc/

我希望输出只包含 "Epoc" 这个精确的文本。我无法使用上述资源解决这个问题。

我已经使用 sed.js.org 网站和我的bash提示符测试了下面的字符串。

echo "/config/folder/downloads/Epoc/" | sed 's/.*[downloads]//'

但输出仍然包含 "c/"。

我尝试至少删除末尾的斜杠。

echo "/config/folder/downloads/Video/Epoc/" | sed 's/.*[Video]//' | sed '/\//d'

然后我什么输出都没有得到。

我是不是在使用错误的命令,还是使用了不正确的方式?
只希望输出 "Epoc" 而没有前后的斜杠。

英文:

I have went to https://sed.js.org/ and https://regex101.com and finally https://tecadmin.net/delete-a-line-containing-specific-string-using-sed/. I am unable to delete the front end and the trailing / in a directory string. This would be set to a variable in a bash. Expecting dir=$(sed ...)

Folder: /config/folder/donwloads/Video/Epoc/

I would like to only have the exact test of "Epoc" as the output. I am unable to figure this out using this above resources.

I have tested the string below using both the sed.js.org website and my bash prompt.

echo "/config/folder/downloads/Epoc/" | sed 's/.*[downloads]//'

but let with "c/" as the output.

I have tried to at least remove the trailing / at end .

echo "/config/folder/downloads/Video/Epoc/" | sed 's/.*[Video]//' | sed '/\//d'

Then I get nothing for output.

Am I using the wrong command to do this or just using the command in the incorrect way?

Output of Epoc only with no trailing or fronted "/"

答案1

得分: 2

  1. s/.*[downloads]//:这会贪婪地搜索在字母 "d"、"o"、"w"、"n"、"l"、"a" 和 "s" 之前的任何内容。它不会搜索单词 "download",正确的表达应该是:s/.*downloads//。方括号内的任何内容表示字符集,但代表单个字符。因此结果是 c/,因为 "Epoc" 中的字母 "o" 符合条件。

  2. 字符串 s/.*[Video]// 同样适用上述规则。

  3. 字符串 /\//d 意味着删除每一行中包含 "/" 的内容。

英文:

What goes wrong with your regex:

  1. s/.*[downloads]// : this performs a greedy search for anything before any of the letters "d","o","w","n","l","a" and "s". It does not search for the word "download", this would be: s/.*downloads//. Anything between square brackets means a set of characters, but represents a single character. Hence the result is c/ because of the letter o in "Epoc".
  2. The same applies to the string s/.*[Video]//
  3. The string /\//d means delete every line that has a / in it.

答案2

得分: 1

这可能适用于你(GNU sed):

sed -n 's/.*[/]\([^/]\+\)[/]\?$//p' file

使用贪婪模式 .* 来查找最后一个或倒数第二个 /,然后捕获除了 / 之外的任何字符,直到行尾的最后一个可选的 /

另一种方法:

sed -nE 's/.*\/([^/]+)\/?$/p' file
英文:

This might work for you (GNU sed):

sed -n 's/.*[/]\([^/]\+\)[/]\?$//p' file

Use greed .* to search for the last or last but one /, then capture anything which is not a / until the last optional / before the end of the line.

Alternative:

sed -nE 's/.*\/([^/]+)\/?$/p' file

huangapple
  • 本文由 发表于 2023年6月5日 10:32:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403213.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定