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?
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.
Comments