bash 获取两个特殊字符之间的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15635321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get string between two special characters
提问by David Aghaian
How do I sed, grep, awk, tror whatever in a bash scriptto get the first occurrence per line of the characters between
I sed, grep, awk,tr或 a 中的任何内容bash script如何获取每行字符之间的第一次出现
' and .
from this file. (The character is a single quote and a period). This is really hard, I love that you're even trying.
从这个文件。(字符是单引号和句点)。这真的很难,我喜欢你甚至在尝试。
So that the command yields:
所以命令产生:
Ideal output:
理想输出:
orinak
pchovi
orinak
xpt
moon
on the following file:
在以下文件中:
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';
}
采纳答案by Kent
if the empty lines in output are not required, this grep with "look-around" will give what you want:
如果不需要输出中的空行,这个带有“环视”的 grep 将给出你想要的:
grep -Po "(?<=')[^.']*(?=\.)" file
just saw you tagged the question with awk
刚刚看到你用awk标记了问题
then try this awk withthose empty lines in output:
然后用输出中的那些空行试试这个 awk :
awk -F"['.]" 'NF>2{print }' file
(the awk one-liner works for your input in example)
(awk one-liner 适用于您在示例中的输入)
回答by Taoufix
Try this:
尝试这个:
sed -n "s/.*'\([^\.]*\)\..*//p" input_file.txt
Run:
跑:
$ sed -n "s/.*'\([^\.]*\)\..*//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: <[email protected]>.
Be sure to include the word ``sed'' somewhere in the ``Subject:'' field.
回答by Atropo
Quick and dirty:
又快又脏:
egrep -o "'[^\.,;]+" file | cut -c2-
Note: This don't print the empty lines.
注意:这不会打印空行。

