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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityThe 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'
tr
easier to use, which supports a-d
parameter to delete characters instead of translating them and also supports certain character classes. In this case justtr -d '[*][:space:]'
might work well for you. But if you really meant to only change lines with a[*]
prefix you would be better of withsed
.