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

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:]]*-//.

Related

SED Replace character with ' [duplicate]

This question already has answers here:
How do I replace single quotes with another character in sed?
(6 answers)
Closed 5 months ago.
Trying to put ' before each line of text and ' at the end of each line of text.
I have been using sed 's/^/1/' file.txt to replace to begging of each line and sed 's/$/0/' file.txt to replace the end of each line.
What I am trying to make work is sed 's/^/'/' and sed 's/$/'/'
This would format my file to make each line reach as a command, when applied to a separate script.
echo abc | sed "s/.*/'&'/"
Output:
'abc'
From man sed:
The replacement may contain the special character & to refer to that portion of the pattern space which
matched

How to reverse all words in line with sed? [duplicate]

This question already has answers here:
Reverse input order with sed
(6 answers)
Closed 4 years ago.
For example, we have:
This is the song that doesn`t end
What sed command will turn it into this?
end doesn`t that song the is This
I've found only how to reverse lines in a file (a.k.a. tac):
sed -n '1!G;h;$p'
This might work for you (GNU sed):
sed -r 'G;:a;s/^(\S+)(\s*)(.*\n)/\3\2\1/;ta;s/\n//' file
Append a newline as a delimiter. Split the current line into three and prepend the first word, the following space and the remainder of the line following the newline in that order. Iterate until the pattern matching fails and then remove the introduced newline.
Could you please try following and let me know if this helps you.
awk '{for(i=NF;i>0;i--){printf("%s%s",$i,(i>1?OFS:ORS))}}' Input_file

sed pattern replace, trailing spaces ending with + only with +

sed pattern replace, trailing spaces ending with + only with +
input
Summary of differences with Numeric +34
First-step changes +34
output
Summary of differences with Numeric+34
First-step changes+34
Did not find answer here
dos this work for you?
sed 's/ *+/+/'
add g if you want to multi-replacements in line.
sed -r 's|\s+([+]\S+)$|\1|' file
Output:
Summary of differences with Numeric+34
First-step changes+34
You can for example do:
$ sed -r 's/\s{2,}\+/+/g' file
Summary of differences with Numeric+34
First-step changes+34
This removes multiple spaces (at least 2) whenever they are followed by the + character. Note + has to be escaped to be interpreted as character and not as a regex symbol.

Using command line to find and replace? [duplicate]

This question already has answers here:
Remove everything after the first / including the first / for each line
(3 answers)
Closed 8 years ago.
I'm trying to use command line to find and replace some text. I have a file with a few million lines that are similar to this:
Something-Here/Grafton-WV</loc>
More-Information/Claremore-OK</loc>
This-Is-It/Seminole-OK</loc>
Your-Company/Lunenburg-MA</loc>
What I need to do is remove the slash and everything after it. I've done wildcard find/replace before but I'm not sure what command would need to be used to start at the slash and continue until the end of the line.
Here's what the output should be:
Something-Here
More-Information
This-Is-It
Your-Company
The following one-liner could work for you:
perl -pe 's{/.*}{}' file.txt
Explanation:
Switches:
-p: Creates a while(<>){...; print} loop for each “line” in your input file.
-e: Tells perl to execute the code on command line.
Code:
s{/.*}{}: Remove all characters after the first forward slash from the line
This is usually done with sed:
sed 's|/.*||' file.txt > newfile.txt

SED Command to remove first digits and spaces of each line

I have a simple text file in below format.
1 12658003Y
2 34345345N
3 34653785Y
4 36452342N
5 86747488Y
6 34634543Y
so on
10 37456338Y
11 33535555Y
12 37456378Y
so on
100 23432434Y
As you can see there are two white spaces after first number.
I'm trying to write SED command to remove the digits before whitespaces. Is there any SED command to remove spaces and number before spaces?
Output file should look like below.
12658003Y
34345345N
34653785Y
36452342N
so on..
Please assist. I'm very new to shell scripting.
sed 's/[0-9]\+\s\+//' infile > outfile
Explanation:
s: we want to use substitution
/: mark start and end of the expression we want to match
[0-9]: match any digit
+: match the previous one or more time
\s: space
+: match the previous one or more time
/: mark start of what we want to change our matches to (which is nothing)
/: some special operators goes after this (we use no such)
infile: the file we want to change
>: pipe stdout to
outfile: where we want to store output
Your sed command would be,
sed 's/.* //g' file
This would remove the first numbers along with the space followed.
Remove leading digits, then following spaces:
sed 's/^[0-9]* *//' file
sed 's/^[0-9]*[ ]*//g' input.txt