获取两个特殊字符之间的字符串

时间:2013-03-26 10:58:09

标签: linux bash scripting sed awk

我如何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';
}

3 个答案:

答案 0 :(得分:4)

如果输出中的空行不是必需的,那么带有“look-around”的grep会给出你想要的东西:

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

刚看到你用 awk

标记了这个问题

然后在输出中使用这些空行尝试这个awk

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

(例如,awk单行程适用于您的输入)

答案 1 :(得分:1)

试试这个:

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.

答案 2 :(得分:0)

又快又脏:

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

注意:这不会打印空行。