sed - replace jira macro in confluence space - sed

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

Related

I need to insert text to a file with quotes using sed

Hi Stackoverflow users,
Normally I can use sed pretty wel (often need a few tries). However I am having issues with adding the text below at the end of the file:
<linebreak/empty line here>
[extensions]
blacklist = "google-authenticator"
Into a file called: /usr/local/psa/admin/conf/panel.ini
To make things "more" complicated, I also use the command in OpenVZ automation. Like this:
vzctl exec $VEID 'sed XX "VALUE FROM ABOVE" /usr/local/psa/admin/conf/panel.ini'
Can someone provide some help and/or direct me in the right direction?
Obviously I tried a few things, but I think I am having issues with the extra quotes in the text I am trying to add.
Errors are always something like this:
expression #1, char 89: unterminated address regex
expression #1, char 91: unterminated address regex
expression #1, char 53: unterminated address regex
So I am doing something wrong and I have no clue how to correctly add the above. Preferably with a line-break in front.
I hope I explained the issue correctly. Thanks in advance for your assistance.
//Edit
All I need is a solution to add the following text:
<linebreak/empty line here>
[extensions]
blacklist = "google-authenticator"
by using sed.
So I can apply it to my script which creates an OpenVZ container (automated) like: vzctl exec $VEID 'possible sed solution here' /file-name
Sorry if I wasn't clear before.
Try using this GNU sed:
'sed -i '\''$a \\n[extensions]\nblacklist = "google-authenticator"'\'' /usr/local/psa/admin/conf/panel.ini'
-i: edit files in place
$: address matching the last line
a text: append text after a line
Just like #Kent recommended in his deleted answer, it would be more readable and simpler to save the text you want to append into a file and use the r file command to sed.

The perl -pe command

So I've done a research about the perl -pe command and I know that it takes records from a file and creates an output out of it in a form of another file. Now I'm a bit confused as to how this line of command works since it's a little modified so I can't really figure out what exactly is the role of perl pe in it. Here's the command:
cd /usr/kplushome/entities/Standalone/config/webaccess/WebaccessServer/etc
(PATH=/usr/ucb:$PATH; ./checkall.sh;) | perl -pe "s,^, ,g;"
Any idea how it works here?
What's even more confusing in the above statement is this part : "s,^, ,g;"
Any help would be much appreciated. Let me know if you guys need more info. Thank you!
It simply takes an expression given by the -e flag (in this case, s,^, ,g) and performs it on every line of the input, printing the modified line (i.e. the result of the expression) to the output.
The expression itself is something called a regular expression (or "regexp" or "regex") and is a field of learning in and of itself. Quick googles for "regular expression tutorial" and "getting started with regular expressions" turn up tons of results, so that might be a good place to start.
This expression, s,^, ,g, adds ten spaces to the start of the line, and as I said earlier, perl -p applies it to every line.
"s,^, ,g;"
s is use for substitution. syntax is s/somestring/replacement/.
In your command , is the delimiter instead of /.
g is for work globally, means replace all occurrence.
For example:
perl -p -i -e "s/oldstring/newstring/g" file.txt;
In file.txt all oldstring will replace with newstring.
i is for inplace file editing.
See these doc for information:
perlre
perlretut
perlop

Sed replace error

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!

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

sed in perl script

I am using following kind of script in my perl script and expecting entry at line 1. I am getting some error as below; any help?
plz ignore perl variable....
Error Messaage -
sed: -e expression #1, char 22: extra characters after command
# Empty file will not work for Sed line number 1
`"Security Concerns Report" > $outputFile`;
`sed '1i\
\
DATE :- $CDate \
Utility accounts with super access:- $LNumOfSupUserUtil \
Users not found in LDAP with super access: - $LNumOfSupUserNonLdap\
' $outputFile > $$`;
`mv $$ $outputFile`;
}
Your immediate problem is that the backslash character is interpreted by Perl inside the backtick operator, as is the dollar character. So your backslash-newline sequence turns into a newline in the shell command that is executed. If you replace these backslashes by \\, you'll go over this hurdle, but you'll still have a very brittle program.
Perl is calling a shell which calls sed. This requires an extra level of quoting for the shell which you are not performing. If your file names and data contain no special characters, you may be able to get away with this, until someone uses a date format containing a ' (among many things that would break your code).
Rather than fix this, it is a lot simpler to do everything in Perl. Everything sed and shells can do, Perl can do almost as easily or easier. It's not very clear from your question what you're trying to do. I'll focus on the sed call, but this may not be the best way to write your program.
If you really need to prepend some text to an existing file, there's a widely-used module on CPAN that already does this well. Use existing libraries in preference to reinventing the wheel. File::Slurp has a prepend_file method just for that. In the code below I use a here-document operator for the multiline string.
use File::Slurp; # at the top of the script with the other use directives
File::Slurp->prepend_file($outputFile, <<EOF);
DATE :- $CDate
Utility accounts with super access:- $LNumOfSupUserUtil
Users not found in LDAP with super access: - $LNumOfSupUserNonLdap
EOF