AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1289859
Accepted
Mario Palumbo
Mario Palumbo
Asked: 2020-11-06 06:04:15 +0800 CST2020-11-06 06:04:15 +0800 CST 2020-11-06 06:04:15 +0800 CST

bash --> perl 命令:只打印替换的文本

  • 772

我有文件“test.txt”,其中包含:

Val1 = '59'
Val2 = '76'
Val3 = '42'
Val4 = '53'

我用这个命令:

perl -pe "s/^Val2 = '(.*)'/\1/" test.txt

我想要:

76

但我得到:

Val1 = '59'
76
Val3 = '42'
Val4 = '53'
bash regex perl
  • 2 2 个回答
  • 590 Views

2 个回答

  • Voted
  1. Best Answer
    steeldriver
    2020-11-06T06:15:32+08:002020-11-06T06:15:32+08:00

    这个-p参数就像 sed 的默认打印 - 也像 sed,如果你想禁止默认打印,你可以使用-n。

    所以你可以做

    perl -ne "print if s/^Val2 = '(.*)'/\1/" test.txt
    

    您还可以使用正则表达式匹配而不是正则表达式替代:

    perl -lne "print \$1 if /^Val2 = '(.*)'/" test.txt
    

    或者

    perl -nE "say \$1 if /^Val2 = '(.*)'/" test.txt
    

    (反斜杠是为了防止$1被 shell 扩展,因为表达式是在双引号中以允许在匹配中使用 Lieral 单引号)。

    • 7
  2. Timur Shtatland
    2020-11-06T06:34:16+08:002020-11-06T06:34:16+08:00

    使用这个 Perl 单行:

    perl -lne "print for /^Val2\s+=\s+'(.*)'/" test.txt
    

    It is slightly shorter and there is not need to escape any variables, since the for loop passes the captured group (.*) to print implicitly as $_, which is the default argument to print.

    It also uses "\s+" (1 or more whitespace characters) instead of " " (1 blank) to be less strict about the input it accepts. While optional, I prefer to follow the rule about being less strict on the input, and more strict on the output (not sure about the source of this rule, though).

    The Perl one-liner uses these command line flags:
    -e : Tells Perl to look for code in-line, instead of in a file.
    -n : Loop over the input one line at a time, assigning it to $_ by default.
    -l : Strip the input line separator ("\n" on *NIX by default) before executing the code in-line, and append it when printing.

    SEE ALSO:
    perldoc perlrun: how to execute the Perl interpreter: command line switches
    perldoc perlre: Perl regular expressions (regexes)
    perldoc perlre: Perl regular expressions (regexes): Quantifiers; Character Classes and other Special Escapes; Assertions; Capture groups
    perldoc perlrequick: Perl regular expressions quick start

    • 2

相关问题

  • 同时复制到两个位置

  • 如何在 shell 脚本中创建选择菜单?

  • 从 bash 迁移到 zsh [关闭]

  • bashrc 还是 bash_profile?

  • 备份 bash 脚本未压缩其 tarball

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve