Linux command to add a word after a specific line - linux

we need to add the line <property name="assumeHungTime" value="600000" />
after the line <bean id="DataUnpacker" class="com-packer" singleton="false">
in one xml file . Please let me know the command to add it - using sed?
Thanks,
Ravikanth

The follwing sed command will do what you want.
sed -e '/<bean id="DataUnpacker" class="com-packer" singleton="false">/a<property name="assumeHungTime" value="600000" />'

Related

Why ^M character is getting appended at end of each line in linux?

I am doing find-replace operations using "sed" in Linux. I have a XML file in Linux named config.xml. The file contains data as followed-
<CATALOG>
<SERVER>
<URL value="http://ip-172-44-0-92.compute.internal:440/" />
</SERVER>
</CATALOG>
I want to find a line in the config.xml file that contains <URL value= and replaces the entire line with <URL value="http://ip-181-40-10-72.compute.internal:440/" />
I tried it by executing the command-
sed -i '/<URL value=/c\<URL value=\"http://ip-181-40-10-72.compute.internal:440/\" />' config.xml
The command executes correctly and does find replace operation but, when I open the file using vi config.xml I see ^M character at the end of each lines that were not replaced. Why did this happened and, how to fix it?
EDIT-
By referring to #Atalajaka's answer...
My original file contains CRLF line endings at the end of each line. And, sed replaces the line with LF line ending. As a result, all other unreplaced lines will still have CRLF ending and 'vi' editor will now show ^M at the end of these lines.
So the solution is to replace CRLF endings with LF endings by running the command-
sed -i $'s/\r$//' config.xml
The issue could rely on the original XML file, in case it has CRLF endings for each line. If that is the case, vim will recognize and hide them, so that they remain unimportant to you. Assuming this, all new lines added with vim will contain those same CRLF line-endings.
The sed command uses LF line-endings when adding any new lines, so when that happens, vim sees the two different line-endings, and will assume the LF line-endings as the regular ones. This means that all CR line-endings will be displayed as ^M.
If you have access to the original XML file before being edited, you can open it in vim and check if you see [dos] at the footer, right next to the file name, something as:
$ vim original_xml.xml
...
"original_xml" [dos] ...
Source.

grep specific part out of a line of text

this is my first question here so please bear with me.
I have a large text file from which I need only one specific part of one line. I can grep the line but I do not know how I can get that specific part out of that line.
here is my text line (stored in output.txt)
><source src="https://download.foobar.com/content/mp4/web01/2017/05/08/24599/mp4_web01.mp4" type="video/mp4" data-label="Laag - 360p" /><source src="https://download.foobar.com/content/mp4/web02/2017/05/08/24599/mp4_web02.mp4" type="video/mp4" data-label="Hoog - 720p" /><source src="https://download.foobar.com/content/mp4/web03/2017/05/08/24599/mp4_web03.mp4" type="video/mp4" data-label="Normaal - 480p" /></video></div></div>
the part I need to extract from this line is:
https://download.foobar.com/content/mp4/web02/2017/05/08/24599/mp4_web02.mp4
Now I can do a grep like this but that gives me back three lines:
grep -Po '><source src="\K[^"]+' output.txt
gives me:
https://download.omroep.nl/nos/content/mp4/web01/2017/05/08/24599/mp4_web01.mp4
https://download.omroep.nl/nos/content/mp4/web02/2017/05/08/24599/mp4_web02.mp4
https://download.omroep.nl/nos/content/mp4/web03/2017/05/08/24599/mp4_web03.mp4
I would like to get only the line I am looking for without making the extra sed command to remove the first and third line of the results.
How can I grep the input line and only get back the intended line. I only need the link to the mp4_web02.mp4 file.
Can anyone help me get this into one grep command?

shell script sed replace

I have this file config.xml
<widget id="com.example.hello" version="0.0.1">
<name>HelloWorld</name>
<description>
A sample Apache Cordova application that responds to the deviceready event.
</description>
<author email="dev#callback.apache.org" href="http://cordova.io">
Apache Cordova Team
</author>
<enter>PASSWORD</enter>
<content src="index.html" />
<access origin="*" />
I tried to do it with sed without success.
I need to do this:
$./script.sh config.xml NEWPASSWORD
to get:
<widget id="com.example.hello" version="0.0.1">
<name>HelloWorld</name>
<description>
A sample Apache Cordova application that responds to the deviceready event.
</description>
<author email="dev#callback.apache.org" href="http://cordova.io">
Apache Cordova Team
</author>
<enter>NEWPASSWORD</enter>
<content src="index.html" />
<access origin="*" />
Using backreference:
sed "s/^\( *<enter>\)\([^>]*\)</\1$2</" "$1"
^\( *<enter>\): search for lines starting with any number of spaces followed by <enter>. Matching characters are captured with escaped parentheses.
\([^>]*\)<: following characters up top next < are captured in a second group.
\1$2<: in the substitution string, characters from first group are output(\1) followed by the second parameter value passed to the script, ($2, the new password value)
The command is applied to $1, the file passed as first parameter to the script (the file name).
To edit the file in place, use the -i flag:
sed -i "s/^\( *<enter>\)\([^>]*\)</\1$2</" "$1"
The good result is:
$cat script.sh
#!/bin/sh
file=$1
sed -i "s/^\( *<enter>\)\([^>]*\)</\1$2</" "$1"
Then:
$./script.sh config.xml NEWPASSWORD
Many thanks to everyone, especially to Kenavoz.

How to insert a string path into the middle of a file line immediately after a matching pattern?

I am trying to insert a string path stored in a variable into an xmlfile as the first part of an attribute value.
The xmlfile is a context.xml file for Tomcat using the form
<Context reloadable="true" docBase="application.war">
...
</Context>
A path, which is the absolute location of a war file to deploy is
$WKSP_APATH="/location/on/server
$WKSP PATH should be inserted as follows
<Context reloadable="true" docBase="/location/on/server/application.war">
...
</Context>
I've tried to find information, but most sources focus on full line insertion.
This is the last line to add for my script.
For context the executed line location is given below as an extract from the script it is embedded in
find /$SEARCH_PATH -name "*.xml" -type f|while read fname; do
CONTEXT_FILE=${fname##*/}
cp -rf "/$SEARCH_PATH/$CONTEXT_FILE"
"/$COPY_LOCATION/${PROJECT_CODENAME}-$CONTEXT_FILE"
echo "Copied $CONTEXT_FILE to:
$COPY_LOCATION/$PROJECT_CODENAME-$CONTEXT_FILE for deployment"
#do insert new path on local server here?
done
Many thanks
Assuming that:
You take the risk to parse xml files using regex instead of proper tools.
The FILE variable contains the xml filename to process.
The WKSP_PATH variable contains the absolute location you want to insert as a prefix in the value of the docBase attribute.
Try:
#!/bin/bash
WKSP_PATH="/location/on/server"
sed -i "s#docBase=\"#docBase=\"$WKSP_PATH/#" "$FILE"
To check the result:
cat "$FILE"
<Context reloadable="true" docBase="/location/on/server/application.war">
...
</Context>

Linux command to find a specific line in file and add new lines below it

I am trying to build a Linux command to find a line in file and add more lines below that line. For example search for "Application" in a file, then get the line where this word is and add three more lines below it.
I am currently using the absolute line number as in "sed -i '51icolumn1, column2, column3' test.txt" but if something change in this file I might be adding the text in a wrong place.
Would
sed -i '/Application/a column1, column2, column3\ncolumn1, column2, column3' test.txt
solve your issue?

Resources