Skip to content

Instantly share code, notes, and snippets.

@Chunlin-Li
Created February 24, 2016 09:28
Show Gist options
  • Save Chunlin-Li/ce47f9adaf8b676899fd to your computer and use it in GitHub Desktop.
Save Chunlin-Li/ce47f9adaf8b676899fd to your computer and use it in GitHub Desktop.
sed

使用 sed 将一行中的特定部分切出来. extract substring by pattern. regex

cat xxxxx |grep xxxx | sed -n 's/REGEX_FULLLINE/\1/p'

例:

echo "regular expression - extract part of string using sed" |sed -n 's/.*- extract \(.*\) using.*/\1/p'

regular expression - extract part of string using sed 中截取 - extract 和 using 中间的部分
-n 表示关闭 sed 的自动打印 pattern space 的功能
最后的 p 表示对于执行了替换的行进行打印.

注意 sed 中正则表达式需完全匹配整行内容, 因此首尾的 .* 用来使 sed 能够正确匹配到目标行, 不能省略!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment