[英]Remove a “*” from a line using sed?
I want to obtain the first field of each line, but it happens one of them will be a '*' and I want it to be skipped. 我想获得每一行的第一个字段,但它发生其中一个将是'*',我希望它被跳过。
git branch -vv
returns 回报
master 34a8e20 [origin/master: behind 14] renamed yml's
* ss_doc 3ebc755 [origin/ss_doc: gone] PRD configuration
ss_fix d0f4a4c [origin/ss_fix: gone] Merge branch 'ss_fix' into 'master'
ss_v c3b4635 [origin/ss_v: gone] remove composes
When I apply the following sed command, the result is the following 当我应用以下sed命令时,结果如下
git branch -vv | sed -r 's|\*?(\w+).+|\1|'
the result is 结果是
master
* ss_doc
ss_fix
ss_v
I cannot understand why it catches the "*" within the matching group. 我无法理解为什么它会在匹配组中捕获“*”。 I've tried other workarounds but this is the closest to the goal.
我尝试了其他解决方法,但这是最接近目标的。 How not to catch the "*"?
怎么不赶上“*”?
Note that sed
replacement command replaces only what is matched. 请注意,
sed
replacement命令仅替换匹配的内容。 What is not matched is not replaced. 什么不匹配不会被替换。
Your pattern tries to match a *
, but if it does not find *
at the current position, it tries to match (\\w+).+
pattern (since \\*?
matches one or zero asterisks). 您的模式尝试匹配
*
,但如果它在当前位置找不到*
,它会尝试匹配(\\w+).+
pattern(因为\\*?
匹配一个或零个星号)。 Since it is not matched, the sed
replacement command keeps the unmatched *
in the result. 由于它不匹配,
sed
replacement命令在结果中保持不匹配的*
。
You need to make sure the asterisk is matched. 您需要确保星号匹配。 As there is whitespace between the
*
and word chars, you may match it with \\s*
or [[:space:]]*
: 由于
*
和单词字符之间有空格,您可以将它与\\s*
或[[:space:]]*
匹配:
sed -r 's|\*?\s*(\w+).+|\1|'
Another way is to match any whitespace and *
before the word chars: 另一种方法是在单词chars之前匹配任何空格和
*
:
sed -r 's|[*[:space:]]*([[:alnum:]_]+).*|\1|'
^^^^^^^^^^^^^
Or, use a PCRE pattern with grep
to only match what you need: 或者,使用带有
grep
的PCRE模式仅匹配您需要的模式:
grep -oP '^\W*\K\w+'
Or, remove any non-word chars at the start and awk
out the first field: 或者,删除任何非单词字符开始和
awk
出的第一个字段:
sed 's/^[^[:alnum:]_]*//' | awk '{print $1}'
See the online demo . 请参阅在线演示 。
Just tell awk to print the first field after the leading blanks and asterisk: 只需告诉awk在前导空白和星号后打印第一个字段:
$ awk -F'[ *]+' '{print $2}' file
master
ss_doc
ss_fix
ss_v
That will work using any awk in any shell on any UNIX system. 这将适用于任何UNIX系统上任何shell中的任何awk。 If you prefer sed, this will work with any sed:
如果您更喜欢sed,这适用于任何sed:
$ sed 's/^[ *]*\([^ ]*\).*/\1/' file
master
ss_doc
ss_fix
ss_v
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.