How to remove the last dot from a string in bash? - sed

I have a string as below:
domain="abc-xyz.com."
I need to remove the . at the last so it would look like abc-xyz.com
I tried this and it doesn't work as it is expecting a file
sed 's/\.$//' $domain
Can someone please help me?

Answering OP's issue's fix: Since you are reading input from a variable, then that's not the correct way to pass value from variable to sed what you are using shown in your question(you must be getting No such file or directory error while executing your code).
Try using:
echo "$domain" | sed 's/\.$//'
OR use:
sed 's/\.$//' <<<"$domain"
Ideal way to deal is could be: Try following if you are ok to use parameter expansion as shown by #anubhava sir in comments.
var="${domain%.}"

This might work for you (GNU sed):
sed 's/\(.*\)\./\1/' file
Use greed to locate the last period and replace it by what came before it.

Related

sed backreference not being found

I am trying to use 'sed' to replace a list of paths in a file with another path.
An example string to process is:
/path/to/file/block
I want to replace /path/to/file with something else.
I have Tried
sed -r '/\s(\S+)\/block/s/\1/new_path/'
I know it's finding the matching string but I'm getting an invalid back reference error.
How can I do this?
This may do:
echo "/path/to/file/block" | sed -r 's|/\S*/(block)|/newpath/\1|'
/newpath/block
Test
echo "test=/path/file test2=/path/to/file/block test3=/home/root/file" | sed -r 's|/\S*/(block)|/newpath/\1|'
test=/path/file test2=/newpath/block test3=/home/root/file
Back-references always refer to the pattern of the s command, not to any address (before the command).
However, in this case, there's no need for addressing: we can apply the substitution to all lines (and it will change only lines where it matches), so we can write:
s,\s(\S+)/block/, \1/new_path,
(I added a space to the RHS, as I'm guessing you didn't mean to overwrite that; also used a different separator to reduce the need for backslashes.)

How to use variable in Linux sed command

I have already read some topic but there is no similar to mine.
I need to pass to sed command with a $value but its too hard :
sed "1,$valued" filename.txt
like
sed "1,4d" filename.txt
How can i do that ?
That's trying to use a variable called $valued.
So you can either:
1,${value}d.
Or set $valued to 4d.
Easiest way:
sed "1,$value"d filename.txt
$value is a shell variable (I assume you were already doing that). So you just needed a way to separate the variable name from what comes next (to avoid evaluating $valued). Quote marks do the job just fine since the shell removes them before calling sed.

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/"

One-liners to remove lines in which a specific character appears more than x times

I think the title says it all, I'm looking for a one-liner to remove lines of a file in which a specific character, let's say /, appears more than x times - 5, for instance.
Start:
/Bo/byl/apointe
S/ta/ck/ov/er/flo/w
M/oon/
Expected result:
/Bo/byl/apointe
M/oon/
Thank you for your suggestions !
You can use gsub function of awk. gsub return number of successful substitution made. So you can use that as reference to identify number of occurrences of particular character.
awk 'gsub(/\//,"&")<5' file
Updated Based on Ed Morton's suggestion.
This might work for you (GNU sed):
sed 's|/|&|5;T;d' file
All you need is:
awk -F/ 'NF<6' file
Look:
$ cat file
/Bo/byl/apointe
S/ta/ck/ov/er/flo/w
M/oon/
$ awk -F/ 'NF<6' file
/Bo/byl/apointe
M/oon/
I believe sed would be sufficient here. You'll want to look into //d and supply the correct condition. I'm going to try something and update when I have better ideas, you should too :)
Once you find it sed -i /{blah}/d will be enough to change it in the file, but you might want to run it without the -i and pipe it through less first to confirm it's doing what you think it's doing.
This would do :
sed -r '/(\/.*){5}\//d' file

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