sed - how can I cover optional space? - unix

Probably this is a dumb question but here we go! I am using sed to remove a content from file and sometimes it doesnt have a space character:
myfile
resource "love" {
...
}
myfile2
resource "love"{
...
}
My command:
sed -i '' -e '/resource "love" {/,/}/d;/resource "love"{/,/}/d'
Do you have another option to consider that space as optional character and cover both situations instead of copy/paste sed instruction?

Match zero or more using sed
$ sed -i "" '/resource "love" *{/,/}/d' input_file

With perl:
perl -i -0pe 's/ressouce "love"\s*.*?\}//s' file

Related

Replace Text using variables in Sed not working

I need to modify an xml file using Sed to replace the line
url="jdbc:oracle:thin:#//ttpdbscan.axel.net:1521/axel.telco.net"
with
url="jdbc:oracle:thin:#//ttpdbscan.axeltelecom.net:1598/axelPRD.telco.net"
I have stored the lines like this
ACTUAL_DB=$(sed -n 's#^.*url="\(.*\).*"#\1#p' $FILE.xml)
and
NEW_DB="jdbc:oracle:thin:#//ttpdbscan.axeltelecom.net:1598/axelPRD.telco.net"
And the replacing method is this one
sed -i "s#$ACTUAL_DB#$NEW_DB#g" $File.xml
The problem is that when I run the script the file stays the same.
I have echoed the variables and all of them return the correct values.
Assuming the file you have is File.xml (if it is not a variable), you may use
sed -i "s#${ACTUAL_DB}#${NEW_DB}#g" File.xml
Try also with other delimiters:
sed -i "s~${ACTUAL_DB}~${NEW_DB}~g" File.xml
If your sed does not support -i use
sed "s~${ACTUAL_DB}~${NEW_DB}~g" File.xml 1<> File.xml
See sed edit file in place
So I saved the output into another file and found out that the string had an extra space
so it looked like this
ACTUAL_DB= "jdbc:oracle:thin:#//ttpdbscan.axel.net:1521/axel.telco.net "
I removed the extra space with
"$(echo -e "${ACTUAL_DB}" | tr -d '[:space:]')"
And now the sed is working as intended

Replacing text in sed with escape characters

I have a very large file where I need to replace the characters \x01\n with the character \n. How would this be done with sed? So far I have:
$ sed -i 's/\x01\n/\n' file
extra characters at the end of d command
But perhaps I'm missing a few escape characters.
Don't write \n in the line oriented sed:
sed -i 's/\x01$//' file
I think you are just missing the closing '/':
$ sed -i 's/\x01\n/\n/' file
This might work for you (GNU sed):
sed -zi 's/\x01\n/\n/g' file
This slurps in the whole (or parts delimited by \x00) file and replaces \x01\n by \n globally.

Remove last character from a file in unix

Have
08-01-12|07-30-13|08-09-32|12-43-56|
Want
08-01-12|07-30-13|08-09-32|12-43-56
I want to remove just the last |.
quick and dirty
sed 's/.$//' YourFile
a bit secure
sed 's/[|]$//' YourFile
allowing space
sed 's/[|][[:space:]]*$//' YourFile
same for only last char of last line (thansk #amelie for this comment) :
add a $in front so on quick and dirty it gives sed '$ s/.$//' YourFile
Since you tagged with awk, find here two approaches, one for every interpretation of your question:
If you want to remove | if it is the last character:
awk '{sub(/\|$/,"")}1' file
Equivalent to sed s'/|$//' file, only that escaping | because it has a special meaning in regex content ("or").
If you want to remove the last character, no matter what it is:
awk '{sub(/.$/,"")}1' file
Equivalent to sed s'/.$//' file, since . matches any character.
Test
$ cat a
08-01-12|07-30-13|08-09-32|12-43-56|
rrr.
$ awk '{sub(/\|$/,"")}1' a
08-01-12|07-30-13|08-09-32|12-43-56
rrr. # . is kept
$ awk '{sub(/.$/,"")}1' a
08-01-12|07-30-13|08-09-32|12-43-56
rrr # . is also removed

Sed command matching pattern UNIX

I want to delete lines from the file using sed command in linux but problem is that
if , I want to match "abcd" it matches "abcd.efgh" also . I just want to match not "abcd.efgh"
. I have searched on google but didnt find any solution .
With awk you can do:
awk '/abcd/ && !/abcd\.efgh/' file
It will match lines that are abcd but not abcd.efgh
Perhaps this is simply what you want:
sed -i '/^abcd$/d' file
It would only delete lines that give an exact match of it.
perl -lne 'print if(/\babcd\b/)' your_file
This might work for you (GUN sed):
sed '/abcd\.efgh/b;/abcd/d' file
or to put another way:
sed '/abcd/{/abcd\.efgh/!d}' file

How to use variables in a command in sed?

I have abc.sh:
exec $ROOT/Subsystem/xyz.sh
On a Unix box, if I print echo $HOME then I get /HOME/COM/FILE.
I want to replace $ROOT with $HOME using sed.
Expected Output:
exec /HOME/COM/FILE/Subsystem/xyz.sh
I tried, but I'm not getting the expected output:
sed 's/$ROOT/"${HOME}"/g' abc.sh > abc.sh.1
Addition:
If I have abc.sh
exec $ROOT/Subsystem/xyz.sh $ROOT/ystem/xyz1.sh
then with
sed "s|\$INSTALLROOT/|${INSTALLROOT}|" abc.sh
it is only replacing first $ROOT, i.e., output is coming as
exec /HOME/COM/FILE/Subsystem/xyz.sh $ROOT/ystem/xyz1.sh
Say:
sed "s|\$ROOT|${HOME}|" abc.sh
Note:
Use double quotes so that the shell would expand variables.
Use a separator different than / since the replacement contains /
Escape the $ in the pattern since you don't want to expand it.
EDIT: In order to replace all occurrences of $ROOT, say
sed "s|\$ROOT|${HOME}|g" abc.sh
This might work for you:
sed 's|$ROOT|'"${HOME}"'|g' abc.sh > abc.sh.1
This may also can help
input="inputtext"
output="outputtext"
sed "s/$input/${output}/" inputfile > outputfile
The safe for a special chars workaround from https://www.baeldung.com/linux/sed-substitution-variables with improvement for \ char:
#!/bin/bash
to="/foo\\bar#baz"
echo "str %FROM% str" | sed "s#%FROM%#$(echo ${to//\\/\\\\} | sed 's/#/\\#/g')#g"

Resources