sed remove special characters and spaces in line

αԋɱҽԃ αмєяιcαη

Input

[*] 111, 22, 33, 44

Output

111,22,33,44

Using sed command sed 's/^\[\*\][[:space:]]*//' file were able to display the output as 111, 22, 33, 44 which means space still there.

So what is the correct sed command?

igal

The short answer is that this should do what you want:

sed 's/[][*]\|[[:space:]]//g'

Test it out like so:

echo '[*] 111, 22, 33, 44' | sed 's/[][*]\|[[:space:]]//g'

Output:

111,22,33,44

A longer explanation follows.


The key ingredient that your expression was missing was the use of the g command to perform global substitution. Without this command only the first match on each line will be replaced.

The most counter-intuitive bit (in my opinion) of my solution is how to include the square brackets inside a character class. For that, we refer to the sed manual:

‘]’
    ends the bracket expression if it’s not the first list item.
    So, if you want to make the ‘]’ character a list item, you must put it first.

For further discussion on that topic, see the following post on SeverFault:

Alternatively, you could have taken your expression and piped it into another sed command, e.g.:

echo '[*] 111, 22, 33, 44' \
| sed 's/^\[\*\]//' \
| sed 's/[[:space:]]//g'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to omit special characters with spaces with sed in website?

SED remove spaces and line breaks

How to remove spaces from special characters?

Remove Double Line Spaces With A Special Symbol @

sed replace special characters with new line on Linux

sed delete a line matching a pattern with special characters

Changing a line of text with sed with special characters

remove sequence of line feeds and spaces in file with sed

how to remove unwanted spaces and new line characters

Remove all special characters, punctuation and spaces from string

remove white spaces between special characters and words python

How to remove strings(made of special characters) with spaces using regex?

sed find line in file and remove characters

Delete spaces and special characters

SED; adding # in front of matching line with various special characters

sed command to find and replace string with special characters to a string in command line

Sed - delete line with multiple special characters using a variable

SED - How to exclude special characters in the same line as the text?

Using sed with special characters

Escaping special characters with sed

Sed special characters

SED escape special characters

Bash: special characters in sed

sed/ perl with special characters (@)

SED Command to remove first digits and spaces of each line

sed remove spaces (multiple encoding) from end of line

Sed to match special characters and add special characters

Remove leading spaces with sed

remove special character (^@) with sed