Deleting particular characters from each line in a file in bash

Hi All,

I am struck with an issue. I need to delete '%' and 'G' from all lines in the input file.
Below is what I want to do.

InputFile

04/09/2012.21:58:17,well9,rootfs,3.9G,2.7G,1.1G,71%,/
04/09/2012.21:58:17,well9,/dev/hda2,3.9G,2.7G,1.1G,71%,/
04/09/2012.21:58:17,well9,/dev/hda3,16G,9.8G,5.0G,67%,/inet
04/09/2012.21:58:17,well13,rootfs,3.9G,2.7G,1.1G,73%,/
04/09/2012.21:58:17,well13,/dev/hda2,3.9G,2.7G,1.1G,73%,/
04/09/2012.21:58:17,well13,/dev/hda3,16G,8.9G,5.8G,61%,/inet
04/09/2012.21:58:17,pier4,rootfs,3.9G,2.3G,1.5G,61%,/
04/09/2012.21:58:17,pier4,/dev/hda2,3.9G,2.3G,1.5G,61%,/

Output

04/09/2012.21:58:17,well9,rootfs,3.9,2.7,1.1,71,/
04/09/2012.21:58:17,well9,/dev/hda2,3.9,2.7,1.1,71,/
04/09/2012.21:58:17,well9,/dev/hda3,16,9.8,5.0,67,/inet
04/09/2012.21:58:17,well13,rootfs,3.9,2.7,1.1,73,/
04/09/2012.21:58:17,well13,/dev/hda2,3.9,2.7,1.1,73,/
04/09/2012.21:58:17,well13,/dev/hda3,16,8.9,5.8,61,/inet
04/09/2012.21:58:17,pier4,rootfs,3.9,2.3,1.5,61,/
04/09/2012.21:58:17,pier4,/dev/hda2,3.9,2.3,1.5,61,/

Try this,

sed 's/\%//g; s/G//g' text

Regards,
Indu

2 Likes

Thanks Indu. Works perfectly.

sed 's/[%G]//g' infile

Might be safer to tie it to a comma to diminish the chance of unwanted modifications in fields 2 and 3 and the last field:

sed 's/[%G],/,/g' infile

or safer yet:

awk -F, '{for(i=4;i<NF;i++)sub(/[G%]$/,x,$i)}1' OFS=, infile
sed -g 's/G//' 's/\%//'

@vino
What does -g do? What sed supports this flag?

Hi

In my zip folder i have many files but i want to extract only .log file from it.

How can i do it.

Please help