How do I replace regex
with $var
in this command?
如何在此命令中用$ var替换正则表达式?
echo "$many_lines" | sed -n '/regex/{g;1!p;};h'
$var
could look like fs2@auto-17
.
$ var看起来像fs2 @ auto-17。
The sed
command will output the line immediately before a regex, but not the line containing the regex.
sed命令将在正则表达式之前输出行,但不包含包含正则表达式的行。
If all this can be done easier with a Perl one-liner, then it is fine with me.
如果使用Perl单线程可以更轻松地完成所有这些操作,那么我就可以了。
5 个解决方案
#1
1
It is not beautiful, but this gives me the previous line to $var
which is want I wanted.
它并不漂亮,但这给了我前一行$ var,这是我想要的。
echo "$many_lines" | grep -B1 "$var" | grep -v "$var"
#2
1
In Perl regexes, you can interpolate variable contents into regexes like /$foo/
. However, the contents will be interpreted as a pattern. If you want to match the literal content of $foo
, you have to escape the metacharacters: $safe = quotemeta $foo; /$safe/
. This can be shortended to /\Q$foo\E/
, which is what you usually want. A trailing \E
is optional.
在Perl正则表达式中,您可以将变量内容插入到/ $ foo /等正则表达式中。但是,内容将被解释为模式。如果你想匹配$ foo的文字内容,你必须转义元字符:$ safe = quotemeta $ foo; / $安全/。这可以缩短到/ \ Q $ foo \ E /,这是你通常想要的。尾随\ E是可选的。
I don't know if the sed regex engine has a similar feature.
我不知道sed正则表达式引擎是否有类似的功能。
A Perl one-liner: perl -ne'$var = "..."; print $p if /\Q$var/; $p=$_'
Perl one-liner:perl -ne'$ var =“...”;打印$ p if / \ Q $ var /; $ P = $ _”
#3
0
Use double quotes instead of single quotes to allow variable expansion:
使用双引号而不是单引号来允许变量扩展:
echo $many_lines | sed -n "/$var/"'{g;1!p;};h'
#4
0
Since you are looking for a line before the regex, with a single one liner it will not be that trivial and beautiful, but here is how I will do it (Using Perl only):
因为你正在寻找正则表达式之前的一行,只用一个单一的衬里就不会那么琐碎和漂亮,但这是我将如何做到的(仅使用Perl):
echo "$many_lines" | perl -nle 'print $. if /\Q$var/' | while read line_no; do export line_no
echo $many_lines | perl -nle 'print if $. - 1 == $ENV{line_no}'
done
echo“$ many_lines”| perl -nle'print $。 if / \ Q $ var /'|而读行line_no; do export line_no echo $ many_lines | perl -nle'打印如果$。 - 1 == $ ENV {line_no}'完成
or if you want to do in one line
或者如果你想在一行中做
echo "$many_lines" | perl -nle 'BEGIN {my $content = ""; } $content .= $_; END { while ($content =~ m#([^\n]+)\n[^\n]+\Q$var#gosm) { print $1 }}'
echo“$ many_lines”| perl -nle'BEGIN {my $ content =“”; } $ content。= $ _; END {while($ content = ~m#([^ \ n] +)\ n [^ \ n] + \ Q $ var#gosm){print $ 1}}'
Or this one, should definitely match:
或者这个,绝对应该匹配:
echo "$many_lines" | perl -nle 'BEGIN {my @contents; } push @contents, $_; if ($contents[-1] =~ m#\Q$var#o)') { print $contents[-2] if defined $contents[-2]; }
echo“$ many_lines”| perl -nle'BEGIN {my @contents; } push @contents,$ _; if($ contents [-1] = ~m#\ Q $ var#o)'){print $ contents [-2] if defined defined $ contents [-2]; }
#5
-1
Or you can use here-documents too, if you don't want to escape the double quotes!
或者你也可以使用here-documents,如果你不想逃避双引号!
In Perl it looks like this:
在Perl中它看起来像这样:
$heredoc = <<HEREDOC;
here is your text and $var is your parameter
HEREDOC
Its important to end the heredoc with the same string you began, in my example its "HEREDOC" in a new line!
用你开始的同一个字符串来结束heredoc非常重要,在我的例子中它是一个新的“HEREDOC”!
#1
1
It is not beautiful, but this gives me the previous line to $var
which is want I wanted.
它并不漂亮,但这给了我前一行$ var,这是我想要的。
echo "$many_lines" | grep -B1 "$var" | grep -v "$var"
#2
1
In Perl regexes, you can interpolate variable contents into regexes like /$foo/
. However, the contents will be interpreted as a pattern. If you want to match the literal content of $foo
, you have to escape the metacharacters: $safe = quotemeta $foo; /$safe/
. This can be shortended to /\Q$foo\E/
, which is what you usually want. A trailing \E
is optional.
在Perl正则表达式中,您可以将变量内容插入到/ $ foo /等正则表达式中。但是,内容将被解释为模式。如果你想匹配$ foo的文字内容,你必须转义元字符:$ safe = quotemeta $ foo; / $安全/。这可以缩短到/ \ Q $ foo \ E /,这是你通常想要的。尾随\ E是可选的。
I don't know if the sed regex engine has a similar feature.
我不知道sed正则表达式引擎是否有类似的功能。
A Perl one-liner: perl -ne'$var = "..."; print $p if /\Q$var/; $p=$_'
Perl one-liner:perl -ne'$ var =“...”;打印$ p if / \ Q $ var /; $ P = $ _”
#3
0
Use double quotes instead of single quotes to allow variable expansion:
使用双引号而不是单引号来允许变量扩展:
echo $many_lines | sed -n "/$var/"'{g;1!p;};h'
#4
0
Since you are looking for a line before the regex, with a single one liner it will not be that trivial and beautiful, but here is how I will do it (Using Perl only):
因为你正在寻找正则表达式之前的一行,只用一个单一的衬里就不会那么琐碎和漂亮,但这是我将如何做到的(仅使用Perl):
echo "$many_lines" | perl -nle 'print $. if /\Q$var/' | while read line_no; do export line_no
echo $many_lines | perl -nle 'print if $. - 1 == $ENV{line_no}'
done
echo“$ many_lines”| perl -nle'print $。 if / \ Q $ var /'|而读行line_no; do export line_no echo $ many_lines | perl -nle'打印如果$。 - 1 == $ ENV {line_no}'完成
or if you want to do in one line
或者如果你想在一行中做
echo "$many_lines" | perl -nle 'BEGIN {my $content = ""; } $content .= $_; END { while ($content =~ m#([^\n]+)\n[^\n]+\Q$var#gosm) { print $1 }}'
echo“$ many_lines”| perl -nle'BEGIN {my $ content =“”; } $ content。= $ _; END {while($ content = ~m#([^ \ n] +)\ n [^ \ n] + \ Q $ var#gosm){print $ 1}}'
Or this one, should definitely match:
或者这个,绝对应该匹配:
echo "$many_lines" | perl -nle 'BEGIN {my @contents; } push @contents, $_; if ($contents[-1] =~ m#\Q$var#o)') { print $contents[-2] if defined $contents[-2]; }
echo“$ many_lines”| perl -nle'BEGIN {my @contents; } push @contents,$ _; if($ contents [-1] = ~m#\ Q $ var#o)'){print $ contents [-2] if defined defined $ contents [-2]; }
#5
-1
Or you can use here-documents too, if you don't want to escape the double quotes!
或者你也可以使用here-documents,如果你不想逃避双引号!
In Perl it looks like this:
在Perl中它看起来像这样:
$heredoc = <<HEREDOC;
here is your text and $var is your parameter
HEREDOC
Its important to end the heredoc with the same string you began, in my example its "HEREDOC" in a new line!
用你开始的同一个字符串来结束heredoc非常重要,在我的例子中它是一个新的“HEREDOC”!