【问题标题】:Get string between two special characters获取两个特殊字符之间的字符串
【发布时间】:2013-03-16 03:29:05
【问题描述】:

我如何sedgrepawktrbash script 中的任何内容来获得每行之间第一次出现的字符

'      and      .    

来自这个文件。 (字符是单引号和句点)。这真的很难,我喜欢你甚至在尝试。

所以命令产生:

理想输出:

orinak
pchovi
orinak

xpt

moon

在以下文件中:

class GI_DnConstants {
        const BO_DOMAIN_NAME = 'orinak.backoffice.domain.com';
        const EX_DOMAIN_NAME = 'pchovi.extranet.domain.com';
        const WS_DOMAIN_NAME = 'orinak.www.domain.com';
        const PT_DOMAIN_NAME = '.partner.domain.com';
        const PTS_DOMAIN_NAME = 'xpt.partners.domain.com';
        const WS_SECURE_DOMAIN_NAME = '.secure.domain.com';
        const IMG_DOMAIN_NAME = 'moon.images.domain.com';
}

【问题讨论】:

  • 你也需要空行吗?
  • 是的,因为它会告诉我发生了错误配置。它现在适用于 Taoufix 的答案。

标签: linux bash scripting sed awk


【解决方案1】:

如果不需要输出中的空行,这个带有“环视”的 grep 将给出你想要的:

grep -Po "(?<=')[^.']*(?=\.)" file

刚刚看到你用 awk 标记了这个问题

然后试试这个 awk with 输出中的那些空行:

awk -F"['.]" 'NF>2{print $2}' file

(示例中的 awk 单行代码适用于您的输入)

【讨论】:

    【解决方案2】:

    试试这个:

    sed -n "s/.*'\([^\.]*\)\..*/\1/p" input_file.txt
    

    运行:

    $ sed -n "s/.*'\([^\.]*\)\..*/\1/p"  input_file.txt
    orinak
    pchovi
    orinak
    
    xpt
    
    moon
    
    
    $ sed --version
    GNU sed version 4.2.1
    Copyright (C) 2009 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
    to the extent permitted by law.
    
    GNU sed home page: <http://www.gnu.org/software/sed/>.
    General help using GNU software: <http://www.gnu.org/gethelp/>.
    E-mail bug reports to: <bug-gnu-utils@gnu.org>.
    Be sure to include the word ``sed'' somewhere in the ``Subject:'' field.
    

    【讨论】:

    • 我不知道您使用的是哪个版本的 sed,但该命令给出了您要求的确切结果,请参阅更新。
    • 这太好了,成功了!我唯一的问题是,我现在如何将这些行中的每一行放入一个可以循环遍历的数组中?
    【解决方案3】:

    又快又脏:

    egrep -o "'[^\.,;]+" file | cut -c2- 
    

    注意:这不会打印空行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-14
      • 1970-01-01
      • 1970-01-01
      • 2012-12-28
      • 1970-01-01
      相关资源
      最近更新 更多