I need to insert text to a file with quotes using sed - 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.

Related

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

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 replacing special string quota

sed is still giving me headaches, so a little help is extremely appreciated.
In a file I have a string like:
SOME_TEXT="variables"
What I want to accomplish is to add a piece of text (variable) to either the end or the begging of the string for that text.
I tried to use variations of:
sed -i '/^SOME_TEXT="/ s/$/ SOME_TEXT="new text'/' filename
but that is failing, so clearly the quota for the string I want to add to is messing up the syntax.
LE:
A variation further is that I have a variable that I want to use as the replace in that syntax, so I have this:
sed -i "s/^SOME_TEXT="/SOME_TEXT=" $variable/" file
This actually produces this output, as it picks up incorrectly the opening/closing quotas:
SOME_TEXT = text_variable" initial text continuation
So how can I properly close the trailing quota so that I can use the variable after it?
I used
sed 's/^SOME_TEXT="/SOME_TEXT="new text/' filename
and it showed:
SOME_TEXT="new textvariables"
Is that what you want?
Escape the '"' characters with a '\' so that they don't terminate your regex string.
sed -i "s/^TEXT=\"/TEXT=\" $variable/"

Using sed to comment out lines that contain a specific string of text

Please bear with me as I'm new to the forums and tried to do my research before posting this. What I'm trying to do is to use sed to look through multiple lines of a file and any line that contains the words 'CPU Usage" I want it to comment out that line and also 19 lines immediately after that.
Example file.txt
This is some random text CPU USAGE more random text
Line2
Line3
Line4
Line5
etc.
I want sed to find the string of text CPU usage and comment out the line and the 19 lines following
#This is some random text CPU USAGE more random text
#Line2
#Line3
#Line4
#Line5
#etc.
This is what I've been trying but obviously it is not working since I'm posting on here asking for help
sed '/\/(CPU Usage)s/^/#/+18 > File_name
sed: -e expression #1, char 17: unknown command: `^'
I'd like to be able to use this on multiple files. Any help you can provide is much appreciated!
GNU sed has a non-standard extension (okay, it has many non-standard extensions, but there's one that's relevant here) of permitting /pattern/,+N to mean from the line matching pattern to that line plus N.
I'm not quite sure what you expected your sed command to do with the \/ part of the pattern, and you're missing a single quote in what you show, but this does the trick:
sed '/CPU Usage/,+19 s/^/#/'
If you want to overwrite the original files, add -i .bak (or just -i if you don't mind losing your originals).
If you don't have GNU sed, now might be a good time to install it.
This can easily be done with awk
awk '/CPU Usage/ {f=20} f && f-- {$0="#"$0}1' file
When CPU Usage is found, set flag f=20
If flag f is true, decrements until 0 and for every time, add # in front of the line and print it.
Think this should work, cant test it, if anyone finds something wrong just let me know :)
awk '/CPU Usage/{t=1}t{x++;$0="#"$0}x==19{t=0;x=0}1' file

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!