I have a file called applications.txt
which contains the following data :
MOB-x01gmobdb2a-PRD-IBG TDST-AB-x01gtdsdb1a-UAT-CST ABC-x01gabcdb2a-PRD-CBG PQR-x01gpqrdb1a-UAT-IBG XYZA-GZ-x01gxyzdb2a-PRD-TS UVW-x01guvwdb1a-UAT-IBG
The strings in this file contain hostnames which I would like to display as follows :
x01gmobdb2a x01gtdsdb1a x01gabcdb2a x01gpqrdb1a x01gxyzdb2a x01guvwdb1a
Few of the above servers are UAT and few are PROD servers. As you see the host-names are sandwiched between two patterns:
- First pattern is
-x
(lowercase x only), - Second pattern is either
-UAT
or-PRD
. Whatever the string available in between first pattern and second pattern, we need to display.
note: From the first pattern x
also should display in output. Example host name is: x01gmobdb2a
Advertisement
Answer
One using awk:
$ awk 'match($0,/-x[^-]*/){print substr($0,RSTART+1,RLENGTH-1)}' file x01gmobdb2a x01gtdsdb1a x01gabcdb2a x01gpqrdb1a x01gxyzdb2a x01guvwdb1a