Remove string between two space characters with sed

han5000

somehow I can't wrap my head around this. I have the following string:

>sp.A9L976 PSBA_LEMMI Photosystem II protein D1 organism=Lemna minor taxid=4472 gene=psbA

I would like to use sed to remove the string between the 1th and 2nd occurrence of a space. Hence, in this case, the PSBA_LEMMI should be removed. The string between the first two spaces does not contain any special characters.

So far I tried the following:

sed 's/\s.*\s/\s/'

But this removes everything unitl the last occurring space string, resulting in:>sp.A9L976 TESTgene=psbA. I thought by leaving out the greedy expression g sed will only match the first occurrence of the string. I also tried:

sed 's/(?<=\s).*(?=\s)//'

But this did not match / remove anything. Can someone help me out here? What am I missing?

Wiktor Stribiżew

You can use

sed -E 's/\s+\S+\s+/ /'
sed -E 's/[[:space:]]+[^[:space:]]+[[:space:]]+/ /'

The two POSIX ERE patterns are the same, they match one or more whitespaces, one or more non-whitespaces, and one or more whitespaces, just \s and \S pattern can only be used in the GNU sed version.

Note that you cannot use \s as a whitespace char in the replacement part. \s is a regex pattern, and regex is used in the LHS (left-hand side) to search for whitespaces. So, a literal space is required to replace with a space.

Since you can also use an awk solution you may use

awk '{$2=""}1' file

Here, the lines ("records") are split into "fields" with whitespace (it is the default field separator), and the second field ($2) value is cleared with {$2 = ""} and the 1 forces awk to output the result (calling the default print command).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Remove characters between two patterns using sed

How to remove everything between two characters with SED?

How to remove everything between two characters with sed?

How to remove white space characters in between the string?

How to remove string between two characters and before the first occurrence using sed

Remove a string from a URL between AND after two different characters using sed

Insert space between two characters of the same field using sed

Remove text that is between two specific characters of a string

Regex to remove string between two characters (exclusive)

SQL Remove string between two characters

Regex in Textwrangler - Remove String Between Two Characters

Replace / Remove String between two characters

How to remove text between a string and a space using SED

sed remove first two characters if some string existed

Attempting to remove string between two strings, whilst omitting the string if - sed

java String.format - how to put a space between two characters

Remove spaces between characters in cell, but only if the space exists between two single characters

Remove characters from string with sed

Using sed to remove string between two other stings in specific column

Remove string between two characters using no regex Javascript

Remove space between two trapezoids

sed replace string with space and special characters

Remove everthing between two characters

remove space between digits of a string

Remove multiple space in between string to a single space

How to remove one white space if two white space found between string in php?

Remove Strings from two patterns/characters with sed

How to remove white space between two characters from a line from text file

Remove Last Two Characters in a String