0

I am trying to replace folder name value in an xml file.I am searching with required pattern using grep and replace the folder name value which is DF_GCSS with ABC_DEF which comes from a variable.Below mentioned statement i have used to replace only the folder name value but the output is not as expected.It is appending the variable data to the existing folder value

FLDR_NM=ABC_DEF grep -i "<FOLDER NAME" file_name.xml | sed 's/<FOLDER NAME="[Aa0-Zz9]"*/<FOLDER NAME="'$FLDR_NM'"/1g' 

Output:

<FOLDER NAME="ABC_DEF"F_GCSS" GROUP="" OWNER="Administrator" SHARED="NOTSHARED" DESCRIPTION="" PERMISSIONS="rwx---r--" UUID="5ff15b2a"> 

Expected output:

<FOLDER NAME="ABC_DEF" GROUP="" OWNER="Administrator" SHARED="NOTSHARED" DESCRIPTION="" PERMISSIONS="rwx---r--" UUID="5ff15b2a"> 
2
  • is the folder name to begin with just F_GCSS ?CommentedDec 4, 2018 at 15:58
  • Any reason you have to use grep/sed, and not tools that deal properly with XML files (including possible line breaks)?
    – dirkt
    CommentedDec 5, 2018 at 8:35

1 Answer 1

1

Provided the current folder name can only be alphanumeric with underscores (_), this will work:

my_folder="ABC_DEF"; cat tmp.xml | sed -e "s/FOLDER NAME=\"[[:alnum:]_]*\"/FOLDER NAME\=\"$my_folder\"/g" 

Folder name in tmp.xml is set to F_GCSS

output:

<FOLDER NAME="ABC_DEF" GROUP="" OWNER="Administrator" SHARED="NOTSHARED" DESCRIPTION="" PERMISSIONS="rwx---r--" UUID="5ff15b2a"> 

    You must log in to answer this question.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.