使用sed打印每行中与模式匹配后的剩余部分的方法是:

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

how to print remaining portion of each line after a pattern match using sed

问题

以下是翻译好的部分:

  1. 我在我的文件中有以下文本。
  2. log-2023-06-30-082821.txt:2023-06-30 08:30:32.648
  3. log-2023-06-30-082821.txt:2023-06-30 08:32:07.235
  4. log-2023-06-30-083220.txt:2023-06-30 08:32:55.831
  5. log-2023-06-30-083604.txt:2023-06-30 08:36:39.380
  6. 我想要在出现模式匹配 ':' 时打印每行的其余部分。
  7. 输出应该如下所示。
  8. 2023-06-30 08:30:32.648
  9. 2023-06-30 08:32:07.235
  10. 2023-06-30 08:32:55.831
  11. 2023-06-30 08:36:39.380
  12. 我该如何在 Windows 中使用 Cygwin 以及 sed 来实现这个目标?我尝试了一些 sed 模式,但它们没有起作用。
英文:

I have the following text in my file.

  1. log-2023-06-30-082821.txt:2023-06-30 08:30:32.648
  2. log-2023-06-30-082821.txt:2023-06-30 08:32:07.235
  3. log-2023-06-30-083220.txt:2023-06-30 08:32:55.831
  4. log-2023-06-30-083604.txt:2023-06-30 08:36:39.380

I want to print the rest of the portion of each line as soon as there is a pattern match ':'.
The output should look like this.

  1. 2023-06-30 08:30:32.648
  2. 2023-06-30 08:32:07.235
  3. 2023-06-30 08:32:55.831
  4. 2023-06-30 08:36:39.380

How can I do this using sed. I am using Cygwin in Windows. I tried a bunch of sed patterns but they haven't worked.

答案1

得分: 2

对于单个字符,您可以使用cut作为字段分隔符,并显示除第一个字段之外的内容。例如:

  1. $ echo 'log-2023-06-30-082821.txt:2023-06-30 08:30:32.648' | cut -d: -f2-
  2. 2023-06-30 08:30:32.648

您还可以使用sed 's/^[^:]*://'删除直到第一个:字符。

英文:

For a single character, you can use it as a field separator with cut and display except the first field. For example:

  1. $ echo 'log-2023-06-30-082821.txt:2023-06-30 08:30:32.648' | cut -d: -f2-
  2. 2023-06-30 08:30:32.648

You can also delete up to the first : character using sed 's/^[^:]*://'

答案2

得分: 0

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

  1. sed -n 's/^[^:]*://p' file

关闭隐式打印(-n)。

匹配从行的开头到 : 之间的任何内容,删除它并打印结果。

英文:

This might work for you (GNU sed):

  1. sed -n 's/^[^:]*://p' file

Turn off implicit printing (-n).

Match anything from the start of the line that is not a : until a :, remove it and print the result.

huangapple
  • 本文由 发表于 2023年7月4日 22:06:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613459.html
匿名

发表评论

匿名网友

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

确定