user2402291
user2402291

Reputation: 1

Cutting Substring from a file

I have the 2 tnsentries of a file with entries like , each entry is in a line.

AAA_PRD = (DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=mmmm01.dimpotiso.org)(PORT=1521)))(CONNECT_DATA=(SID=ORCL)))

BBB_PRD = (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=irkosdap01.nonprod.digrestsiso.org)(PORT=1626))(CONNECT_DATA=(SID=BBBDFS)))^

The HOST entry does not always start at the same column length. I want to strip out the entries from the HOST to the end bracket of the host, or better just the HOST entries.

I would like to get the results as

mmmm01.dimpotiso.org

irkosdap01.nonprod.digrestsiso.org

Thanks

I was partially able to get the information out of my tnsnames.ora file with the below-

cat  tnsnamesMaster_March12_2013.ora | grep 'HOST' | cut -f2 -d 'HOST=' | cut -f2 -d'=' | cut -f1 -d ')'

Upvotes: 0

Views: 75

Answers (1)

Kevin
Kevin

Reputation: 56069

grep -o 'HOST=[^)]*' tnsnamesMaster_March12_2013.ora | cut -c6-

-o tells grep to print only the matching string, which will be HOST= up to but not including the first ). Then cut that HOST= out.

If your grep supports perl regexes, you can do it at once by putting the HOST= in a lookbehind:

grep -oP '(?<=HOST=)[^)]+' tnsnamesMaster_March12_2013.ora

Apparently -o is not POSIX, so the above may not work on them. The following are, as far as I can tell, strictly POSIX compliant and so should work anywhere sane:

grep HOST tnsnamesMaster_March12_2013.ora | sed 's/.*HOST=\([^)]*\)).*/\1/'

sed -n '/HOST/s/.*HOST=\([^)]*\)).*/\1/p' tnsnamesMaster_March12_2013.ora

Upvotes: 1

Related Questions