简体   繁体   中英

Find and replace strings in bash

I have XML file, this file has to be changed each time i'm running my scripts, i have to find a specific line there

<data key='user.CC_MVFSModuleKernelPath,com.ibm.rational.clearcase.linux_x86' value='/lib/modules/2.6.32-279.el6.i686/build'/>

I can't be sure that this line will be at the same line number, is there any way to find this line, and replcae the value which is '/lib/modules/2.6.32-279.el6.i686/build' .

The input file that i have is: <data key='user.Common_AdminDir' value='/var/adm/rational'/> <data key='user.Common_TempDir' value='/tmp'/> <data key='user.CC_LICENSE_MANAGER,com.ibm.rational.clearcase.linux_x86' value='IBMRCL'/> <data key='user.ATRIA_HOST_IS_REMOTE,com.ibm.rational.clearcase.linux_x86' value=''/> <data key='user.ATRIA_REMOTE_HOSTNAME,com.ibm.rational.clearcase.linux_x86' value=''/> <data key='user.ATRIA_LOCAL_KEY,com.ibm.rational.clearcase.linux_x86' value=''/> <data key='user.CC_IS_ATRIA_LICENSE,com.ibm.rational.clearcase.linux_x86' value='false'/> <data key='user.CC_MVFSModuleKernelPath,com.ibm.rational.clearcase.linux_x86' value='/lib/modules/2.6.32-279.el6.i686/build'/> <data key='user.CC_MVFSModuleRebuild,com.ibm.rational.clearcase.linux_x86' value='1'/> I need to replace the value that is in the line which holds the next string - MVFSModuleKernelPath,com.ibm.rational.clearcase I don't know what is the current in this line so the search pattern can't include the t he string in the value - and i need to replace it with a new value for example echo /lib/modules/ uname -r /build this value will be in some variable valueToInsert I want to be able to print just value in the virst place and put in some variable in order to compare with my existing kernel version and then replace it

You can use sed :

# printing current value
sed -n "\#<data key='user\.CC_MVFSModuleKernelPath,com\.ibm\.rational\.clearcase\.linux_x86'#s~^.*value='\([^']*\)'.*$~\1~p" file.xml
/lib/modules/2.6.32-279.el6.i686/build

# replacement of the value by $valueToInsert
sed -i.bak "\#<data key='user\.CC_MVFSModuleKernelPath,com\.ibm\.rational\.clearcase\.linux_x86'#s~^\(.*value='\)[^']*\('.*\)$~\1$valueToInsert\2~" file.xml

Open file and use substitution replace value with "MY NEW VALUE".

WHere my "MY NEW VALUE" in the command line is your new value also using # as delimiter in sed instead of using "/" will avoid having to escape "/" chars.

 xmllint my_xml_file|sed 's#key='user.CC_MVFSModuleKernelPath,com.ibm.rational.clearcase.linux_x86' value='/lib/modules/2.6.32-279.el6.i686/build'/>#key='user.CC_MVFSModuleKernelPath,com.ibm.rational.clearcase.linux_x86' value= MY NEW VALUE/>#g'

To find line, highlight and print it and afterward modify and print the new line, you can try the following

 xmllint xml_file| grep -o -E --color  'key='user.CC_MVFSModuleKernelPath,com.ibm.rational.clearcase.linux_x86' value='/lib/modules/2.6.32-279.el6.i686/build'/>' &&  xmllint my_xml_file|sed -n 's#key='user.CC_MVFSModuleKernelPath,com.ibm.rational.clearcase.linux_x86' value='/lib/modules/2.6.32-279.el6.i686/build'/>#key='user.CC_MVFSModuleKernelPath,com.ibm.rational.clearcase.linux_x86' value= MY NEW VALUE/>#p'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM