sed command help for forward slash - sed

I have a html line like this
<name> abc </name>
<name> test </name>
Basically any string can be between <name> and </name> and I have to replace it with word file : XYZ
So i tried
sed -i 's/<name>.*<\//name>/<name>file:XYZ<\//name>/g' filename
but it throws an error unexpected char: '\' on the first back slash. What is the right way to do this?

If you replace '/' with '#', there's no need to escape '/'. Modify your command as followed,
sed 's#<name>.*</name>#<name>file:XYZ</name>#g' filename
And I would suggest you to try the command below,
sed 's#\(<name>\).*\(</name>\)#\1file:XYZ\2#g' filename
\1 and \2 refer to the corresponding matching \(<name>\) and \(</name>\). Make sure the output is what you want before -i is added.

Related

Sed: unescaped newline inside substitute pattern

I want to replace '-Djava.library.path=""' with '-Djava.library.path="path"' in file named "file" using sed
sed -i 's/-Djava.library.path=""/-Djava.library.path="path"/g' -f file
returns
sed: 2: file: unescaped newline inside substitute pattern
What's wrong? Can someone help?
Changed to
sed -i 's/Djava.library.path=""/Djava.library.path="path"/' file
Now it works.

Replace string and next one using sed

I know how to replace sting via sed:
sed -i "s|.*#app-${BRANCH}-log.*| Path ${LOG_PATH} #app-${BRANCH}-log|" /etc/td-agent-bit/td-agent-bit.conf
and this is work for file like
[INPUT]
Name tail
Path /var/lib/docker/containers/f774c1a3689dfffb2528833ac2ded629c3b1873fd3af96fe0cf1f041f22f88d8/f774c1a3689dfffb2528833ac2ded629c3b1873fd3af96fe0cf1f041f22f88d8-json.log #app-develop-log
Tag app.develop
Interval_Sec 1
but how to replace next line? for example:
#This line just for triggering sed
And this one must be replaced
Any idea? Sorry for my horrible English.
With GNU sed:
sed '/^#This line just for triggering sed$/{n;s/.*/foo/}' file
Output:
#This line just for triggering sed
foo
See: man sed

search a string which contains "/" and replace using sed

How to search a pattern and remove the line using sed which contains special characters like "ranasnfs2:/SA_kits/prod"
I tried using a variable to hold the complete string and then recall the variable in sed command but it is not working.
echo $a
ranasnfs2:/SA_kits/prod
sed -i '/"$a"/d' test.txt
cat test.txt | grep -i SA
/SA_kits -rw,suid,soft,retry=4 ranasnfs2:/SA_kits/prod
You need to escape the slash character.
Use this for deleting lines which contain a /:
sed '/\//d' file

replace string between 2 dot with sed

my file contain lines like that:
level0.1.level1.1.level1
level0.1.level1.2.level2
I want to replace ".x." with "{i}"
so the desired lines are like follow
level0.{i}.level1.{i}.level1
level0.{i}.level1.{i}.level2
When we look for ".x.", meaning "." followed by any character
followed by ".", the search pattern for sed will be
"\..\.", as we need to escape the "." with "\".
Once the match is found replace it with ".{i}."
The "/g" at the end implies multiple such replacements.
cat file | sed 's/\..\./.{i}./g'
using sed
sed -r 's/[.][0-9]+[.]/.{i}./g'
crash test
echo 'level0.1.level1.2.level2'|sed -r 's/[.][0-9]+[.]/.{i}./g'
output
level0.{i}.level1.{i}.level2

How to extract URL from html source with sed/awk or cut?

I am writing a script that will download an html page source as a file and then read the file and extract a specific URL that is located after a specific code. (it only has 1 occurrence)
Here is a sample that I need matched:
<img id="sample-image" class="photo" src="http://xxxx.com/some/ic/pic_1asda963_16x9.jpg"
The code preceding the URL will always be the same so I need to extract the part between:
<img id="sample-image" class="photo" src="
and the " after the URL.
I tried something with sed like this:
sed -n '\<img\ id=\"sample-image\"\ class=\"photo\"\ src=\",\"/p' test.txt
But it does not work. I would appreciate your suggestions, thanks a lot !
You can use grep like this :
grep -oP '<img\s+id="sample-image"\s+class="photo"\s+src="\K[^"]+' test.txt
or with sed :
sed -r 's/<img\s+id="sample-image"\s+class="photo"\s+src="([^"]+)"/\1/' test.txt
or with awk :
awk -F'src="' -F'"' '/<img\s+id="sample-image"/{print $6}' test.txt
If you have GNU grep then you can do something like:
grep -oP "(?<=src=\")[^\"]+(?=\")" test.txt
If you wish to use awk then the following would work:
awk -F\" '{print $(NF-1)}' test.txt
With sed as
echo $string | sed 's/\<img.*src="\(.*\)".*/\1/'
A few things about the sed command you are using:
sed -n '\<img\ id=\"sample-image\"\ class=\"photo\"\ src=\",\"/p' test.txt
You don't need to escape the <, " or space. The single quotes prevents the shell from doing word splitting and other stuff on your sed expression.
You are essentially doing this sed -n '/pattern/p' test.txt (except you seemed to be missing the opening backslash) which says "match this pattern, then print the line which contain the match", you are not really extracting the URL.
This is minor, but you don't need to match class="photo" since the id already makes the HTML element unique (no two elements share the same id w/in the same HTML).
Here's what I would do
sed -n 's/.*<img id="sample-image".*src="\([^"]+\)".*/\1/p' test.txt
The p flag tells sed to print the line where substitution (s) was performed.
\(pattern\) captures a subexpression which can be accessed via \1, \2, etc. on the right side of s///
The .* at the start of regex is in case there is something else preceding the <img> element on the line (you did mention you are parsing a HTML file)