How to remove text between a string and a space using SED - sed

I have a file with repeating line in it like this;
<stack-block name="B" sub-type="SBL" type="ABM_BLOCK" level="2" parent-name="PBTYRD" geo-anchor-latitude="-34.96723069348281" geo-anchor-longitude="150.2157080161554" geo-anchor-orientation="72.35290364141252" z-index-min="1" />
<stack-block name="C" sub-type="SBL" type="ABM_BLOCK" level="2" parent-name="PBTYRD" geo-anchor-latitude="-34.967529872288864" geo-anchor-longitude="150.2145108805486" geo-anchor-orientation="72.35290364141252" z-index-min="1" />
...and so on...
I want to remove the geo-anchor-latitude="-34.96723069348281" section from the lines of a file including the geo-anchor-latitude phrase up to the second double quote.
I have tried sed -i 's/geo-anchor-latitude.*"//' filename with no luck as it strips everything from geo-anchor-latitude to the end of the line.
Any clues out there? Thanks.

Would you try the following:
sed -i 's/geo-anchor-latitude="[^"]*"//' filename
Output:
<stack-block name="B" sub-type="SBL" type="ABM_BLOCK" level="2" parent-name="PBTYRD" geo-anchor-longitude="150.2157080161554" geo-anchor-orientation="72.35290364141252" z-index-min="1" />
<stack-block name="C" sub-type="SBL" type="ABM_BLOCK" level="2" parent-name="PBTYRD" geo-anchor-longitude="150.2145108805486" geo-anchor-orientation="72.35290364141252" z-index-min="1" />
The regex geo-anchor-latitude="[^"]*" matches the substring such as:
A literal string geo-anchor-latitude="
Followed by a sequence of any characters except for "
Followed by a double quote "
Then the matched substring above is removed by the s command.

You can use extended regular expressions (-E) with sed to do this.
sed -Ei 's/geo-anchor-latitude="[-0-9]+[.][0-9]+"//' filename
This regex looks for the latitude attribute, followed by a decimal number with any number of digits.

Related

linux sed how to wrap a multiline search pattern with text

I have a html file which includes a section as follows:
<div id='webnews'>
... variable stuff ...
</div>
which I want to comment out as follows:
<!--
<div id='webnews'>
... variable stuff ...
</div>
-->
I can find & print the multiline text as follows:
sed '/<div id="webnews"/, /<\/div>/ { p }' filename.html
Experimenting with h, d, x and G, I have been unable work out how to either wrap the hold buffer or the pattern buffer with '<!--' and '-->'.
Would appreciate help with this challenge.
quick and dirty with sed (not the best idea on html unless you are sure of html content/structure)
sed "/<div id='webnews'/, /<\/div>/ {
/<div id='webnews'/ {
h
d
}
H
/<\/div>/ !d
x
s/^/<!--\\
/
s/$/\\
-->/
}" filename.html
This might work for you (GNU sed):
sed -e '/<div id='\''webnews'\''>/,/<\/dev>/!b;/<div id='\''webnews'\''>/i\<!--' -e '/<\/div>/a\-->' file
Or perhaps:
sed $'/<div id=.webnews.>/,/<\/dev>/{/<div id=.webnews.>/i\<!--\n;/<\/div>/a\-->\n}' file
Sed is not the right tool for the job.
Use sift:
sift -m '(.+)(<div id=.webnews.>.*</div>)(.+)' --replace '$1<!-- $2 -->$3'

Sed replacing Special Characters in a string

I am having difficulties replacing a string containing special characters using sed. My old and new string are shown below
oldStr = "# td=(nstates=20) cam-b3lyp/6-31g geom=connectivity"
newStr = "# opt b3lyp/6-31g geom=connectivity"
My sed command is the following
sed -i 's/\# td\=\(nstates\=20\) cam\-b3lyp\/6\-31g geom\=connectivity/\# opt b3lyp\/6\-31g geom\=connectivity/g' myfile.txt
I dont get any errors, however there is no match. Any ideas on how to fix my patterns.
Thanks
try s|# td=(nstates=20) cam-b3lyp/6-31g geom=connectivity|# opt b3lyp/6-31g geom=connectivity|g'
you can use next to anything after s instead of /, as your expression contains slashes I used | instead. -, = and # don't have to be escaped (minus only in character sets [...]), escaped parens indicate a group, nonescaped parens are literals.

sed replacement value between to matches

Hi I want to replace a string coming between to symbols by using sed
example: -amystring -bxyz
what to replace mystring with ****
value after -a can be anything like -amystring 123 -bxyz, -amystring 123<newline_char>, -a'mystring 123' -bxyz, -a'mystring 123'<newline_char>
I tried following regex but it does not work in all the cases
sed -re "s#(-w)([^\s\-]+)#\1**** #g"
can anybody help me to solve this issue ?
MyString="YourStringWithoutRegExSpecialCharNotEscaped"
sed "s/-a${MyString} -b/-a**** -b/g"
if you can escape your string for any regex key char like * + . \ / with something like
echo "${MyString}" | sed 's/\[*.\\/+?]/\\&/g' | read -r MyString
before us it in sed.
otherwise, you need to better define the edge pattern

how to conditionally replace string with sed

Suppose I have following string. I want to replace <b>2</b> to <b>20</b> if <a>2</a>
<start>
<a>1</a><b>1</b>
<a>2</a><b>2</b>
.
.
<a>10</a><b>10</b>
<a>2</a><b>2</b>
</start>
New string should look like this
<start>
<a>1</a><b>1</b>
<a>2</a><b>20</b>
.
.
<a>10</a><b>10</b>
<a>2</a><b>20</b>
</start>
can I do this using sed?
You can start with this:
sed '/<start>/,/<\/start>/s!\(<a>2</a><b>2\)</b>!\10</b>!' input
and relax the expression as required, for example allow spaces in tag a:
sed '/<start>/,/<\/start>/{/<a>[ ]*2[ ]*<\/a>/s!<b>2<!<b>20<!}' input
This will replace the first occurrence of <b>2</b> with <b>20</b> in all the lines with <a>2</a> invoke:
sed '/<a>2<\/a>/s/<b>2<\b>/<b>20<\/b>/' input

Remove double quotes from header in SED

i would like to remove double quotes from the header row only. The data below is in a txt file with tab-delimiter. Thanks!
"$sedol" "$cusip" "$rbss_id"
"2877365" "22122P101" "53301020"
"B0G72D1" " " "50102020"
The desired answer is:
$sedol $cusip $rbss_id
"2877365" "22122P101" "53301020"
"B0G72D1" " " "50102020"
Try this sed:
sed '1s/"//g' file.txt
1s will make sure to replace " on line # 1 only.