SED command to remove trailing white spaces [duplicate] - sed

This question already has answers here:
How to remove trailing whitespaces with sed?
(12 answers)
Closed 9 years ago.
Is there a sed command to remove trailing white spaces?

use the command below:
sed 's/\s*$//g' your_file

Related

Removing empty lines with grep as well as [, ], ', and ,? [duplicate]

This question already has answers here:
Combining two sed commands
(2 answers)
Closed 6 months ago.
sed "s/[][,']//g"
I used this, but then I have empty lines getting returned. I know that you can use:
sed '/^[[:space:]]*$/d'
Delete empty lines using sed
However, trying sed "s/[][[:space:]][,']//g" didn't work as well as other stuff I have tried.
sed is a scripting language; combine the two commands to get the effect you seek.
sed "s/[][,']//g;"'/^[[:space:]]*$/d'

Remove string from the beginning and the end of line keeping the ones in the middle (sed) [duplicate]

This question already has answers here:
How to remove the leading and trailing space from each line of a file using shell script?
(2 answers)
Closed 8 months ago.
The community reviewed whether to reopen this question 8 months ago and left it closed:
Original close reason(s) were not resolved
I am have the following text:
>seq1
--A--CGT-A--
>seq2
-GA-T-A-CC--
I would like to remove all "-" from the beginning and the end of the lines, i.e., keeping the "-" between the letters. Expected output:
>seq1
A--CGT-A
>seq2
GA-T-A-CC
I have tried the following sed, but it deletes only the "-" from the beginning.
sed 's/^\(-\)*//'
Can anyone help, please?
You can use
sed 's/^-*\|-*$//g' file
sed -E 's/^-*|-*$//g' file
sed -E 's/^-+|-+$//g' file
Each of the commands removes hyphens from the start and from the end of the lines. Note the g flag that enables multiple matching on the same line.
To support cases with leading or trailing whitespaces, add [[:space:]] / \s:
sed 's/^\s*-*\|-*\s*$//g'
sed -E 's/^[[:space:]]*-*|-*[[:space:]]*$//g'
Note: \s and \| examples are only valid for GNU sed.
See the online demo:
#!/bin/bash
s='>seq1
--A--CGT-A--
>seq2
-GA-T-A-CC--'
sed 's/^-*\|-*$//g' <<< "$s"
Output:
>seq1
A--CGT-A
>seq2
GA-T-A-CC
This removes leading and trailing dashes on every line not beginning with > (including indented >).
sed -E '/^[[:space:]]*>/!{s/^-+|-+$//}'
Consider allowing for indented dashes: s/[[:space:]]*-//.

Use separator other than / in sed [duplicate]

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 4 years ago.
I want to use sed on some text that contains backslashes, and want to avoid lots of escaped \/ characters.
How can I change the delimiter or separator character to be other than /?
In GNU sed, simply swap out the / for the character you wish to use:
% echo /// | sed 's_/_x_g'
xxx
\ can also be used, and must not be escaped (it fails when escaped as \\):
% echo xxx | sed 's\x\y\g'
yyy

Sed find replace combination with less sign and slash and more sign [duplicate]

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 7 years ago.
I have a stumbling block with sed replace (Linux shell)
I need to replace
</test>
to
</test1>
tried
sed -i 's/<\/test>/</test1>/g'
and similar variants -but still no luck...so thanks for any hint to try
Try this:
echo '</test>' | sed 's|</test>|</test1>|'
For what you tried, you need to escape the slash in the replacement string:
sed -i 's/<\/test>/<\/test1>/g'
Or change the regex boundary marker character:
sed -i 's%</test>%</test1>%g'

sed delete line with file path [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
sed: delete using a different delimiter
I can substitute line in file like this
sed "s|$PATH_WITH_SLASH||" file
but I cannot delete it
sed "|$PATH_WITH_SLASH|d" file
The problem is that | character cannot be used for deletion. Why?
If you use other character as address delimiter, you need to use backslash before the first char. So
/address/
or
\|address|