David Aghaian
David Aghaian

Reputation: 43

Get string between two special characters

How do I sed, grep, awk, tr or whatever in a bash script to get the first occurrence per line of the characters between

'      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';
}

Upvotes: 4

Views: 9206

Answers (3)

Kent
Kent

Reputation: 195209

if the empty lines in output are not required, this grep with "look-around" will give what you want:

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

just saw you tagged the question with awk

then try this awk with those empty lines in output:

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

(the awk one-liner works for your input in example)

Upvotes: 4

Atropo
Atropo

Reputation: 12541

Quick and dirty:

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

Note: This don't print the empty lines.

Upvotes: 0

Taoufix
Taoufix

Reputation: 372

Try this:

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

Run:

$ 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: <[email protected]>.
Be sure to include the word ``sed'' somewhere in the ``Subject:'' field.

Upvotes: 1

Related Questions