从shell脚本中的文件中提取字符串的一部分

时间:2014-04-03 19:47:42

标签: shell unix extract

我有一个文件如下

<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"/>

我需要提取字符串""

后面的FOLDERNAME =中的值

所需的输出是

XS_SHARED 
MK_SHARED
XS

4 个答案:

答案 0 :(得分:1)

您可以使用awk:

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

通常最好将DOM解析器用于XML,而不是使用awk / sed / grep。

答案 1 :(得分:1)

使用sed:

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

答案 2 :(得分:1)

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

答案 3 :(得分:1)

实际上你可以使用awk使用文件分隔符选项:

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