user3495390
user3495390

Reputation: 1

extract a part of string from a file in shell scripting

I have a file as below

<SHORTCUT COMMENTS ="" DBDNAME ="xsxx_db2" FOLDERNAME ="XS_SHARED" NAME ="Shortcut_to_xx_CNTC_T" OBJECTSUBTYPE ="Source Definition" OBJECTTYPE ="SOURCE" REFERENCEDDBD ="xsxx_db2" REFERENCETYPE ="LOCAL" REFOBJECTNAME ="XSC_CLNT_CNTC_T" REPOSITORYNAME ="xxxx" VERSIONNUMBER ="2"/>
<SHORTCUT COMMENTS ="" FOLDERNAME ="MK_SHARED" NAME ="Shortcut_to_CIF_FF_MOB_NBR" OBJECTSUBTYPE ="Target Definition" OBJECTTYPE ="TARGET" REFERENCETYPE ="LOCAL" REFOBJECTNAME ="CIFXx_NBR" REPOSITORYNAME ="eim_xx" VERSIONNUMBER ="1"/>
<SHORTCUT COMMENTS ="" DBDNAME ="FlatFile" FOLDERNAME ="XS" NAME ="Shortcut_to_xxFF_TX" OBJECTSUBTYPE ="Source Definition" OBJECTTYPE ="SOURCE" REFERENCEDDBD ="FlatFile" REFERENCETYPE ="LOCAL" REFOBJECTNAME ="DXX_FF_TX" REPOSITORYNAME ="xxxx" VERSIONNUMBER ="1"/>

I need to extract the values in "" that follows the string FOLDERNAME =

Output needed is

XS_SHARED 
MK_SHARED
XS

Upvotes: 0

Views: 58

Answers (4)

Sina
Sina

Reputation: 154

Actually you can do it with awk using file separator option:

awk 'BEGIN { FS = "FOLDERNAME =" } ; { print $2 }' file | awk -F'"' '{print $2}'

Upvotes: 1

glenn jackman
glenn jackman

Reputation: 246764

grep -oP '(?<=FOLDERNAME =").+?(?=")' file

Upvotes: 1

clt60
clt60

Reputation: 63892

With sed:

sed 's/.*FOLDERNAME *= *"\([^"]*\)".*/\1/' file

Upvotes: 1

anubhava
anubhava

Reputation: 784998

You can use awk:

awk -F '^.*FOLDERNAME *= *"|"' '{print $2}' file
XS_SHARED
MK_SHARED
XS

It is usually better to use DOM parsers for XML rather than using awk/sed/grep though.

Upvotes: 1

Related Questions