Need to remove first and last character using sed

Hi

I have file in below format. How i can remove the first and lost comma from this below file

,001E:001F,,,02EE,0FED:0FEF,

I need output has below

001E:001F,,,02EE,0FED:0FEF

for first and last, try:

sed 's/^,//; s/,$//;' infile

for lost, not sure. maybe there is a lost and found section someplace.

1 Like

Hi,

The sed command can take care of this for you:

$ cat test.txt
,001E:001F,,,02EE,0FED:0FEF,
$ sed -i 's/^,//g;s/,$//g' test.txt
$ cat test.txt
001E:001F,,,02EE,0FED:0FEF

The effect of this command is to substitute commas at the start of a line (represented by ^, ) with nothing, and to also substitute commas at the end of a line (represented by ,$ ) with nothing. So any commas at the start and end will be removed, with everything else being left untouched.

Note that it will only remove exactly what it matches - i.e. one single comma, so if there are multiple commas at the start or end of some lines in your file you'd need to tweak this. But if every line is of the exact same format, then this would do the job.

Hope this helps !

1 Like

Thanks it was working fine

POSIX shell, 'sh', compliant:-

Longhand using OSX 10.14.3, default bash terminal:

Last login: Tue Aug  6 15:56:39 on ttys000
AMIGA:amiga~> echo ',001E:001F,,,02EE,0FED:0FEF,' > /tmp/text
AMIGA:amiga~> text=$( cat /tmp/text )
AMIGA:amiga~> text=${text#?}; text=${text%?}
AMIGA:amiga~> echo "${text}"
001E:001F,,,02EE,0FED:0FEF
AMIGA:amiga~> _

Hello ranjancom2000,

We encorage users to post their efforts which they are putting to solve their own problem. Please always do add same in CODE TAGS.

Thanks,
R. Singh

On GNU bash, version 4.4.12

$ echo ${text:1:-1}
001E:001F,,,02EE,0FED:0FEF