Deleting particular characters from each line in a file in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting particular characters from each line in a file in bash
# 1  
Old 04-25-2012
Bug 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
Code:
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
Code:
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,/


Last edited by Franklin52; 04-25-2012 at 04:57 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 04-25-2012
Try this,
Code:
sed 's/\%//g; s/G//g' text

Regards,
Indu
These 2 Users Gave Thanks to Indumathy For This Post:
# 3  
Old 04-25-2012
Thanks Indu. Works perfectly.
# 4  
Old 04-25-2012
Code:
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:
Code:
sed 's/[%G],/,/g' infile

or safer yet:
Code:
awk -F, '{for(i=4;i<NF;i++)sub(/[G%]$/,x,$i)}1' OFS=, infile

# 5  
Old 04-25-2012
Code:
sed -g 's/G//' 's/\%//'

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 04-25-2012 at 05:50 AM.. Reason: Please use code tags
# 6  
Old 04-25-2012
@vino
What does -g do? What sed supports this flag?
# 7  
Old 04-25-2012
Bug Unzip selected file

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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert a line in a file by deleting that line from another file

Hi all, I have a huge file(size more that 5GB). I want to do some manupulation with the records and write to another file. As the size of the file is huge and there is a space constraint in that directory, I want to delete that record from first file after writing it in to second file.... (3 Replies)
Discussion started by: gani_85
3 Replies

2. Shell Programming and Scripting

Deleting new line characters

Hi, I have a weird requirement. I am having a file with 12fields in it and the end of the line for each record is "\n" (Just \n and no carriage returns) and the field delimiter is "|". Problem is I can have new line characters in any field in the data and these new line characters can even come... (11 Replies)
Discussion started by: ngkumar
11 Replies

3. UNIX for Dummies Questions & Answers

Bash: using SED, trying to replace some characters except first or last line

Hi, I require to replace 2 items: 1. replace start of all lines in a file with ' except the first line 2. replace end of all lines in a file with '||chr( except last line I am able to do the entire file using sed -e s/^/\'/g -e s/$/\'\|\|chr\(/g "$file" > newfile.txt but am not yet... (3 Replies)
Discussion started by: Chella15
3 Replies

4. Shell Programming and Scripting

Bash: using SED, trying to replace some characters except first or last line

Hi, I require to replace 2 items: 1. replace start of all lines in a file with ' except the first line 2. replace end of all lines in a file with '||chr( except last line I am able to do the entire file using sed -e s/^/\'/g -e s/$/\'\|\|chr\(/g "$file" > newfile.txt but am not yet able... (0 Replies)
Discussion started by: Chella15
0 Replies

5. UNIX for Dummies Questions & Answers

Help with deleting characters from text file

I have a text file that looks like this: I want to delete the last character of first column in all rows so that my output looks like this: Thanks a lot! (1 Reply)
Discussion started by: evelibertine
1 Replies

6. UNIX for Dummies Questions & Answers

Deleting the first line of .gz file

Hi All, I have too many .gz files (test.gz). Task is to remove first line of each file. Can I do it without unzipping the files? Your help is appreciated. (4 Replies)
Discussion started by: Chulamakuri
4 Replies

7. UNIX for Dummies Questions & Answers

Need help with deleting certain characters on a line

I have a file that looks like this: It is a huge file and basically I want to delete everything at the > line except for the number after “C”. >c1154... (2 Replies)
Discussion started by: kylle345
2 Replies

8. Shell Programming and Scripting

Deleting Characters at specific position in a line if the line is certain length

I've got a file that would have lines similar to: 12345678 x.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 23456781 x.00 xx.00 xx.00 xx.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 34567812 x.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 45678123 x.00 xx.00 xx.00 xx.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 xx.00... (10 Replies)
Discussion started by: Cailet
10 Replies

9. Shell Programming and Scripting

Deleting First Two Characters On Each Line

How would one go about deleting the first two characters on each line of a file on Unix? I thought about using awk, but cannot seem to find if it can explicitly do this. In this case there might or might not be a field separator. Meaning that the data might look like this. 01999999999... (5 Replies)
Discussion started by: scotbuff
5 Replies

10. Shell Programming and Scripting

Deleting the blank line in a file and counting the characters....

Hi, I am trying to do two things in my script. I will really appreciate any help in this regards. Is there a way to delete a last line from a pipe delimited flat file if the last line is blank. If the line is not blank then do nothing..... Is there a way to count a word that are starting... (4 Replies)
Discussion started by: rkumar28
4 Replies
Login or Register to Ask a Question