Sed replace error - sed

I have a pattern I am trying to match:
<x>anything</x>
I am trying to replace 'anything' (which can be any text, not the text anything - (.*)) with 'something' so any occurrences would become:
<x>something</x>
I am trying to use the following sed command:
sed "s/<x>.*</x>/<x>something</x>/g" file
I am getting the following error:
sed: -e expression #1, char 19: unknown option to `s'
Can someone point me in the right direction?

This might work for you (GNU sed):
sed -r 's/(<x>)[^<]*/\1something/g' file
This looks to replace <x> and something which is not a < by <x>something repeatedly on the same line.
N.B. .* is greedy and may well swallow up further tags on the same line.

The slashes in the closing XML tags are confusing it. Try escaping them like this:
sed "s/<x>.*<\/x>/<x>something<\/x>/g" file
You can apparently also use an equals sign which I'd never seen before. I'll be changing a bunch of scripts when I get to work!

Related

Delete line from file if matches regular expression

I am attempting to delete a line from a text file if it matches a regular expression. To accomplish this I was using sed in an Ubuntu environment combined with regular expressions. I have tried/referenced the following solutions: Sol1, Sol2, Sol3.
My current command is: sed '/[^"]+},/d' test.json with this command I am attempting to match and remove lines like:
{"hello},
{"go penguins!},
{"someone help1),
I am NOT trying to match or remove lines like: "should not match regex"}, Any line that ends with "}, should not be deleted.
I am not tied to using sed so any acceptable answer would work so long as my text file would look something like:
...
{"omg this is amazing"},
{"thanks for your help"},
{"no problem"},
...
How about sed '/\"},/!d' test.json?
It should by sed '/\"},/d' test.json (without !)

sed - replace jira macro in confluence space

Think i'm very close to my solution but i don't see what's wrong with this expression. I checked this expression within an editor, which works fine. But same should work with sed, so that i can run it with a shell script.
What i did.
I exported a Confluence Space and like to import to another Confluence. This confluence does not know the JIRA Server as an Application Link and it will not get.
So that's why i want to replace the macro with a link.
<ac:structured-macro ac:name="jira"><ac:parameter ac:name="columns">key,summary,type,created,updated,due,assignee,reporter,priority,status,resolution</ac:parameter><ac:parameter ac:name="server">JIRA</ac:parameter><ac:parameter ac:name="serverId">797a864e-7adf-3e88-ae1f-f35e5aade3f4</ac:parameter><ac:parameter ac:name="key">IT-1234</ac:parameter></ac:structured-macro>
I tried the following to replace the macro. But didn't work yet. Can somebody help me with this expression, and explain what i'm doing wrong?
sed -i -E 's/<ac:structured-macro ac:name="jira">.*?((?:IT|BI)-[0-9]+).*?<\/ac:structured-macro>/http:\/\/www.myconfluence.com\/browse\/\1/gI' "confluence-space/entities.xml"
I get the result:
sed: -e expression #1, char 130: Invalid preceding regular expression.
Thanks in advance.
You can't use (?:x)(non-capturing groups) syntax with sed.
(? will search for zero or one occurrence of... nothing because ( is not interpreted as a litteral character but as an opening capturing group.
Try this:
sed -i -E 's/<ac:structured-macro ac:name="jira">.*?((IT|BI)-[0-9]*).*<\/ac:structured-macro>/http:\/\/www.myconfluence.com\/browse\/\1/g' file

sed command over multiple lines not working

I am using sed to replace 14 different abbreviations like CA_23456, CB_scaffold34532,... with 'proper' names in a file and it works putting it all on one line.
acc=$1
sed -e 's/CA_[A-Z]*[a-z]*[0-9]*/Hesperocyparis_arizonica/;s/CB_[A-Z]*[a-z]*[0-9]*/Hesperocyparis_bakeri/;s/CM_[A-Z]*[a-z]*[0-9]*/Hesperocyparis_macrocarpa/;s/CS_[A-Z]*[a-z]*[0-9]*/Cupressus_sempervirens/;s/CT_[A-Z]*[a-z]*[0-9]*/Cupressus_torulosa/;s/JD_[A-Z]*[a-z]*[0-9]*/Juniperus_drupacea/;s/JF_[A-Z]*[a-z]*[0-9]*/Juniperus_flaccida/;s/JI_[A-Z]*[a-z]*[0-9]*/Juniperus_indica/;s/JP_[A-Z]*[a-z]*[0-9]*/Juniperus_phoenicea/;s/JX_[A-Z]*[a-z]*[0-9]*/Juniperus_procera/;s/JS_[A-Z]*[a-z]*[0-9]*/Juniperus_scopulorum/;s/MD_[A-Z]*[a-z]*[0-9]*/Microbiota_decussata/;s/XN_[A-Z]*[a-z]*[0-9]*/Xanthocyparis_nootkatensis/;s/XV_[A-Z]*[a-z]*[0-9]*/Xanthocyparis_vietnamensis/' ${acc}.nex > ${acc}_replaced.nex
To make it more readable I'd like to have the command split over multiple lines using '\' (not all the replacements are shown for brevity)
acc=$1
sed -e 's/CA_[A-Z]*[a-z]*[0-9]*/Hesperocyparis_arizonica/;\
s/CB_[A-Z]*[a-z]*[0-9]*/Hesperocyparis_bakeri/;\
s/CM_[A-Z]*[a-z]*[0-9]*/Hesperocyparis_macrocarpa/'\
${acc}.nex > ${acc}_replaced.nex
However, I get an error message: sed: -e expression #1, char 168: unterminated address regex. I have looked at the answers to similar problems on various webforums and tried various things (using 's/.../.../' on every line, leaving ';' out,....) but I can't get it to work. What am I doing wrong?
Drop the \ that escapes the newlines. (They are not actually doing it!, they are interpreted as wrong syntax by sed). However I would suggest to put it into a file and run it like this:
sed -f script.sed input
where script.sed looks like this:
s/CA_[A-Z]*[a-z]*[0-9]*/Hesperocyparis_arizonica/
s/CB_[A-Z]*[a-z]*[0-9]*/Hesperocyparis_bakeri/
s/CM_[A-Z]*[a-z]*[0-9]*/Hesperocyparis_macrocarpa/
Remove the backslashes from the sed code.
Inside singly-quoted shell strings, backslashes are not needed to escape newlines and are not removed because they are not parsed as escape characters. This has the effect that sed sees them as part of its code, and it then expects to find an address regex with a different delimiter than / before the command ends at the next newline (similar to \,/home/, !d). This address regex does not appear (nor an associated command), and so sed complains about invalid code.
Apart from that: The semicolons in the sed code are no longer necessary when you terminate commands with newlines, and anything involving shell variables should be quoted to avoid splitting in case of whitespace.
In sum:
sed -e 's/CA_[A-Z]*[a-z]*[0-9]*/Hesperocyparis_arizonica/
s/CB_[A-Z]*[a-z]*[0-9]*/Hesperocyparis_bakeri/
s/CM_[A-Z]*[a-z]*[0-9]*/Hesperocyparis_macrocarpa/' \
"${acc}.nex" > "${acc}_replaced.nex"

Matching strings even if they start with white spaces in SED

I'm having issues matching strings even if they start with any number of white spaces. It's been very little time since I started using regular expressions, so I need some help
Here is an example. I have a file (file.txt) that contains two lines
#String1='Test One'
String1='Test Two'
Im trying to change the value for the second line, without affecting line 1 so I used this
sed -i "s|String1=.*$|String1='Test Three'|g"
This changes the values for both lines. How can I make sed change only the value of the second string?
Thank you
With gnu sed, you match spaces using \s, while other sed implementations usually work with the [[:space:]] character class. So, pick one of these:
sed 's/^\s*AWord/AnotherWord/'
sed 's/^[[:space:]]*AWord/AnotherWord/'
Since you're using -i, I assume GNU sed. Either way, you probably shouldn't retype your word, as that introduces the chance of a typo. I'd go with:
sed -i "s/^\(\s*String1=\).*/\1'New Value'/" file
Move the \s* outside of the parens if you don't want to preserve the leading whitespace.
There are a couple of solutions you could use to go about your problem
If you want to ignore lines that begin with a comment character such as '#' you could use something like this:
sed -i "/^\s*#/! s|String1=.*$|String1='Test Three'|g" file.txt
which will only operate on lines that do not match the regular expression /.../! that begins ^ with optional whiltespace\s* followed by an octothorp #
The other option is to include the characters before 'String' as part of the substitution. Doing it this way means you'll need to capture \(...\) the group to include it in the output with \1
sed -i "s|^\(\s*\)String1=.*$|\1String1='Test Four'|g" file.txt
With GNU sed, try:
sed -i "s|^\s*String1=.*$|String1='Test Three'|" file
or
sed -i "/^\s*String1=/s/=.*/='Test Three'/" file
Using awk you could do:
awk '/String1/ && f++ {$2="Test Three"}1' FS=\' OFS=\' file
#String1='Test One'
String1='Test Three'
It will ignore first hits of string1 since f is not true.

Delete a matching pattern with sed

I try to delete all occcurence of a word from my xml file. The pattern I would like to delete is something like below:
& lt;foo_bar>300</foo_bar& gt;
I am not familiar with sed, but I know it's feasible using it. I tried something like :
sed 's^&lt[foo_bar]>$g' myfile.xml
or
sed 's/^&lt[foo_bar]>$//' myfile.xml
both failed with an error message. So could you please help me how to figure out this? OS is Solaris 10 so most likely standart version is sed installed not GNU one. Please ignore space after & sign in the expression. There is no space in actual expression.
Thanks
At least, the way you are using character classes [foo_bar] is wrong. [foo_bar] can match one of f,o,b,a,r,_ only once. And you seem to have no attempt at matching /. The first expression you have lacks regex delimiters. sed will assume you are using ^ as the delimiter but then it lacks the corresponding delimiters as in s^find^replace^g.
This seems to work:
sed 's!<foo_bar>[^&]*</foo_bar>!!g' input
This might work for you (GNU sed):
sed -r 's/&\s*lt;(foo_bar&)gt;[0-9]+<\/\1\s*gt;//g' file