I am using sed to replace a content in a file with string "dba01upc\Fusion_test". After the replacement I see the '\' character is missing. The replaced string is dba01upcFusion_test . Looks like sed is ignoring '\' while replacing..
Can anyone let me know the sed command to include all characters?
My Sed Command:
sed -i "s%{"sara_ftp_username"}%"dba01upc\Fusion_test"%g" /home_ldap/user1/placeholder/Sara.xml
Before Replacement : Sara.xml
<?xml version="1.0" encoding="UTF-8"?>
<ser:service-account >
<ser:description/>
<ser:static-account>
<con:username>{sara_ftp_username}</con:username>
</ser:static-account>
</ser:service-account>
After Replacement : Sara.xml
<?xml version="1.0" encoding="UTF-8"?>
<ser:service-account>
<ser:description/>
<ser:static-account>
<con:username>dba01upcFusion_test</con:username>
</ser:static-account>
</ser:service-account>
Thanks
sed -i 's%{sara_ftp_username}%dba01upc[\]Fusion_test%g' /home_ldap/user1/placeholder/Sara.xml
# or
sed -i 's%{sara_ftp_username}%dba01upc\\Fusion_test%g' /home_ldap/user1/placeholder/Sara.xml
# or
sed -i "s%{sara_ftp_username}%dba01upc\\\Fusion_test%g" /home_ldap/user1/placeholder/Sara.xml
escape the \ (twice if double quote)
Try this:
sed -i "s%sara_ftp_username%dba01upc\\\Fusion_test%g" /home_ldap/user1/placeholder/Sara.xml
Related
I'm trying to add a line in a file afile.xyz using my script. This is what I've done so far using sed:
n="$(grep ".method" "$m" | grep "onCreate(Landroid/os/Bundle;)V")"
sed -i '' -e '/$n/ a\
"test", /Users/username/Documents/afile.xyz
I'm getting the error:
"onCreate\(\Landroid\/ ...": bad flag in substitute command: 'g'
How do I solve this? Please do help. Thanks.
Edit: Content of n
method protected onCreate(Landroid/os/Bundle;)V
2 problems:
because the sed body is in single quotes, the variable $n will not be expanded,
the regular expression in $n contains the / dilimiters.
Try this:
n=$(...)
nn=${n//\//\\/} # escape all slashes
sed -i '' '/'"${nn}"'/ a ...
The single-quoted sed body is interrupted to append the double quoted shell variable.
You can also use a different delimiter for the RE:
sed -i '' -e "\#$n# a\\
\"test\"" /Users/username/Documents/afile.xyz
I made an array of filenames of files in which match an pattern:
lista=($(grep -El "<LastVisitedURL>.+</LastVisitedURL>.*<FavoriteTopic>0</FavoriteTopic>" *))
Now I would delete in a file index.xml all tags enclosure which contains the filenames in the array.
for e in ${lista[*]}
do
sed '/\<TopicKey FileName=\"$e\"\>.*\<\/TopicKey\>/d' index.xml
done
The complete script is:
#! /bin/bash
#search xml files watched and no favorites.
lista=($(grep -El "<LastVisitedURL>.+</LastVisitedURL>.*<FavoriteTopic>0</FavoriteTopic>" *))
#declare -p lista
for e in ${lista[*]}
do
sed '/<TopicKey FileName=\"$e\">.*<\/TopicKey>/d' index.xml
done
Even though the regex pattern doesn't work, -i option in sed for edit in place index.xml, reload index file many times how filenames have the array, and this is bad.
Any suggestions?
Here an example using xmlstarlet in a shell :
% cat file.xml
<?xml version="1.0"?>
<root>
<foobar>aaa</foobar>
<LastVisitedURL>http://foo.bar/?a=1</LastVisitedURL>
<LastVisitedURL>http://foo.bar/?a=2</LastVisitedURL>
<LastVisitedURL>http://foo.bar/?a=3</LastVisitedURL>
</root>
Then, the command line :
% xmlstarlet edit --delete '//LastVisitedURL' file.xml
<?xml version="1.0"?>
<root>
<foobar>aaa</foobar>
</root>
I have this xml file
</license>
<parameters pca-dim="32"/>
<parameters resize_minpix="100000" npix="100000" ptch="24" step="4" nscale="5" maxscale="4"/>
<parameters notify-classes-removed="1"/>
<parameters grid-regions="1x1,1x3"/>
<feature_extractions>
<feature_extraction id="orh" params="8,4:0.7,0.5:0.4,0.6:0.01"/>
<feature_extraction id="col" params="4:mv:0.4,0.6:0.01"/>
</feature_extractions>
<vocabulary rebuild="IfDoesNotExist" gmm-iter="8" sig-norm-type="l2" sig-norm-pow="0.5"/>
<classifier type="sgd" lambda="1.0E-5" max-iterations="20"/>
<validation name="V1CrossValidation" folds="5" mode="fast" method="modulo" result-file="/opt/ADL_db/Users/mkhalil/CshellTest/ScriptTests/temp/V1CrossValidation-results.stats" score-flags="combine,normalize"/>
I would like to use sed command to change folds="5" to folds="6"
Try this :
sed -e 's/ folds="5"/ folds="6"/g' file.xml
You can omit the g modifier, it's for general replacement (all occurrences that match will be replaced).
If you want to write to the file rather than print to the standard output, use the -i (for in place) option :
sed -i 's/ folds="5"/ folds="6"/g' file.xml
If OS X, you can use :
sed -i '' 's/ folds="5"/ folds="6"/g' file.xml
How do I add ' (Apostrophe) character using sed command in between a line. I used following command on mac computer. But it just add a space.
sed -i '' 's/cur i s/cur i '' s/g' ./edit_char.cmd
Try doing this (see the double quotes) :
sed -i'' "s/cur i s/cur i '' s/g" ./edit_char.cmd
I'd do this:
sed -Ei '' 's/(cur i ) ( s)/\1'\''\2/g' ./edit_char.cmd
Based on the information at https://tex.stackexchange.com/questions/48933/which-symbols-need-to-be-escaped-in-context, I want to prepare a file for use with ConTeXt. I need to make several replacements:
Replace # with \#.
Replace % with \percent.
Replace | with \textbar.
Replace $ with \textdollar.
Replace _ with \textunderscore.
Replace ~ with \textasciitilde.
Replace { with \textbraceleft.
Replace } with \textbraceright.
I have tried using the information from Replacing "#", "$", "%", "&", and "_" with "\#", "\$", "\%", "\&", and "\_" to do these replacements:
sed -i 's/\&/\\\&/g' ./File.csv
sed -i 's/\#/\\\#/g' ./File.csv
sed -i 's/\%/\\\percent/g' ./File.csv
sed -i 's/\|/\\\textbar/g' ./File.csv
sed -i 's/\$/\\\textdollar/g' ./File.csv
sed -i 's/\_/\\\textunderscore/g' ./File.csv
sed -i 's/\~/\\\textasciitilde/g' ./File.csv
sed -i 's/\{/\\\textbraceleft/g' ./File.csv
sed -i 's/\}/\\\textbraceright/g' ./File.csv
Unfortunately, when I run these scripts, the entire file is changed to a bunch of strange letters, numbers, and the words "extbar" everywhere.
How can I make these replacements?
Why is "extbar" appearing in my file after running these commands?
when you do
sed -i 's/|/\\\textbar/g' ./File.csv
sed reads it as s/|/\\\textbar/g \\ becomes \ and \t becomes tab character.
Try
sed -i "s/|/\\\textbar/g"
or
sed -i 's/|/\\textbar/g'
Use four backslashes instead of the to escape. They are evaluated twice. Following, you have the character \tas replacement, followed by the string 'extbar'(from \textbar)
This might work for you:
cat <<\! >Village.sed
s/&/\\&/g
s/#/\\#/g
s/%/\\percent/g
s/|/\\textbar/g
s/\$/\\textdollar/g
s/_/\\textunderscore/g
s/~/\\textasciitilde/g
s/{/\\textbraceleft/g
s/}/\\textbraceright/g
!
sed -f Village.sed ./File.csv
Not sure why "extbar" is appearing in your file probably to do with the line s/\|/\\\textbar/g where \| means alternation.
See here:
echo foo | sed 's/\|/\\bar/'
\barfoo
echo foo | sed 's/|/\\bar/'
foo