cogiz
December 3, 2016, 5:55pm
1
#!/bin/bash
X=(0 2 4 6 7 0 0 0 0)
Let me just say from the start that sed confuses the hell out of me!
In the above line of code how can I use sed to remove all of the 0's except the first one?
I have tried sed -e 's/[0]*$//g' but it removes all of the 0's.
Thank you in advance for any and all suggestions.
Cogiz
mjf
December 3, 2016, 6:39pm
2
Here is one way:
$ echo '0 2 4 6 7 0 0 0 0' | sed 's/0/\n/g; s/\n/0/; s/\n//g'
0 2 4 6 7
Not sure what you are after. If you want to used sed to act on the bit of shell code you posted as input file:
Try:
$ echo 'X=(0 2 4 6 7 0 0 0 0)' | sed 's/\( 0\)\{1,\})/)/'
X=(0 2 4 6 7)
or with -E (BSD sed) or -r (GNU sed)
$ echo 'X=(0 2 4 6 7 0 0 0 0)' | sed -E 's/( 0)+\)/)/'
X=(0 2 4 6 7)
--
Note: using \n
in the replacement part of a substitution (s-command) is a GNU sed only extension.
Remove all 0's with a leading space
sed 's/ 0//g'
1 Like