Issue
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?
Solution
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
Answered By - Wiktor Stribiżew Answer Checked By - Pedro (WPSolving Volunteer)