Replacing multiple characters


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replacing multiple characters
# 1  
Old 05-12-2011
Replacing multiple characters

I have a file as follows

Code:
ggsnApnDownlinkBytes.X
ggsnApnDownlinkDrops.X
ggsnApnDownlinkPackets.X
ggsnApnName.X
ggsnApnUplinkBytes.X
ggsnApnUplinkDrops.X
ggsnApnUplinkPackets.X
ggsnApnDownlinkBytes.Y
ggsnApnDownlinkDrops.Y
ggsnApnDownlinkPackets.Y
ggsnApnName.Y
ggsnApnUplinkBytes.Y
ggsnApnUplinkDrops.Y
ggsnApnUplinkPackets.Y
ggsnRadiusAuthenticationFailure.0
ggsnRadiusAccountingFailure.0
ggsnApnRadiusAuthenticationFailure.Y
ggsnApnRadiusAccountingFailure.Y
ggsnApnAttemptedAuthActivation.Y
ggsnApnFailedAuthActivation.Y
ggsnNbrOfActivePdpContexts.0
ggsnGtpuCpuUsage.18
ggsnGtpuCpuUsage.48
ggsnGtpuCpuUsage.50
ggsnGtpuCpuUsage.64
ggsnGtpcCpuUsage.16
ggsnGtpcCpuUsage.34
ggsnGtpcCpuUsage.66
ggsnGtpuNbrOfActivePdpContexts.18
ggsnGtpuNbrOfActivePdpContexts.48
ggsnGtpuNbrOfActivePdpContexts.50
ggsnGtpuNbrOfActivePdpContexts.64
ggsnGtpcNbrOfActivePdpContexts.16
ggsnGtpcNbrOfActivePdpContexts.34
ggsnGtpcNbrOfActivePdpContexts.66
ggsnGtpuUserUplinkDrops.18
ggsnGtpuUserUplinkDrops.48
ggsnGtpuUserUplinkDrops.50
ggsnGtpuUserUplinkDrops.64
ggsnGtpuUserDownlinkDrops.18
ggsnGtpuUserDownlinkDrops.48
ggsnGtpuUserDownlinkDrops.50
ggsnGtpuUserDownlinkDrops.64

I need to replace all X with $G10B and replace all Y with $G10I. Again I believe sed is the answer but unsure to do so for multiple entries
# 2  
Old 05-12-2011
Try this:
Code:
awk -F. '{$NF=$NF=="X"?"$G10B":$NF=="Y"?"$G10I":$NF}1' OFS=. file

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 05-12-2011
Many thanks Franklin, works a treat!

---------- Post updated at 11:23 AM ---------- Previous update was at 03:04 AM ----------

SHould say it did work a treat however when run from my machine on
Code:
 SunOS alps 5.10 Generic_125100-05 sun4v sparc SUNW,Sun-Fire-T200

I get the error
Code:
 awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1

---------- Post updated at 11:31 AM ---------- Previous update was at 11:23 AM ----------

I have used
Code:
sed 's/X/$G10B/g'

but it only replaces X with $G10B and not the actual value worked out in the script for $G10B...Anyone any further ideas?


.
.
.
.
.
.
.
.
Some time later after looking around I see that

Code:
 sed -e "s/X/$G10B/g" <filename>

works

Last edited by rob171171; 05-12-2011 at 03:53 PM..
# 4  
Old 05-12-2011
Quote:
Originally Posted by rob171171
SHould say it did work a treat however when run from my machine on
Code:
 SunOS alps 5.10 Generic_125100-05 sun4v sparc SUNW,Sun-Fire-T200

I get the error
Code:
 awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1

Use nawk or /usr/xpg4/bin/awk on Solaris.

With sed you can do something like:
Code:
sed -e 's/.X$/.$G10B/' -e 's/.Y$/.$G10I/' file

This User Gave Thanks to Franklin52 For This Post:
# 5  
Old 05-12-2011
Give a try with:
Code:
sed "s/X/$G10B/g;s/Y/$G10I/g;" infile

# 6  
Old 05-12-2011
Thanks again Franklin, see my lat post which I used
Code:
 sed -e "s/X/$G10B/g"

I notice you do both sed's on 1 line which I will try. Up to now sed has scared me due to the vast amount of combinations i have seen it being used however this particular one I like!
# 7  
Old 05-12-2011
Quote:
Originally Posted by rob171171
Thanks again Franklin
I am ctsgnb , Franklin is another guy Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Issue with search and replacing multiple items in multiple files

Im having an issue when trying to replace the first column with a new set of values in multiple files. The results from the following code only replaces the files with the last set of values in val.txt. I want to replace all the files with all the values. for date in {1..31} do for val in... (1 Reply)
Discussion started by: ncwxpanther
1 Replies

2. Shell Programming and Scripting

sed parser behaving strange on replacing multiple words in multiple files

I have 4000 files like $cat clus_grp_seq10_g.phy 18 1002 anig_OJJ65951_1 ATGGTTTCGCAGCGTGATAGAGAATTGTTTAGGGATGATATTCGCTCGCGAGGAACGAAGCTCAATGCTGCCGAGCGCGAGAGTCTGCTAAGGCCATATCTGCCAGATCCGTCTGACCTTCCACGCAGGCCACTTCAGCGGCGCAAGAAGGTTCCTCG aver_OOF92921_1 ... (1 Reply)
Discussion started by: sammy777888
1 Replies

3. Shell Programming and Scripting

sed replacing specific characters and control characters by escaping

sed -e "s// /g" old.txt > new.txt While I do know some control characters need to be escaped, can normal characters also be escaped and still work the same way? Basically I do not know all control characters that have a special meaning, for example, ?, ., % have a meaning and have to be escaped... (11 Replies)
Discussion started by: ijustneeda
11 Replies

4. UNIX for Dummies Questions & Answers

best method of replacing multiple strings in multiple files - sed or awk? most simple preferred :)

Hi guys, say I have a few files in a directory (58 text files or somthing) each one contains mulitple strings that I wish to replace with other strings so in these 58 files I'm looking for say the following strings: JAM (replace with BUTTER) BREAD (replace with CRACKER) SCOOP (replace... (19 Replies)
Discussion started by: rich@ardz
19 Replies

5. Shell Programming and Scripting

Replacing text from multiple files at multiple location

Hi, I have many files scattered in all different folders. I want to replace the text within all the files using a single command ( awk, sed...) Is it possible? example find all the files in which there is text "memory" and replace it with "branded_memories". the files can be at the... (2 Replies)
Discussion started by: rudoraj
2 Replies

6. Shell Programming and Scripting

Replacing characters

Hi fellow experts, I have a question for you. Data looks like: 00877,05/13/2010,PBO,P,0000708331,518 00877,05/13/2010,PBO,P,0000708331,519 ... ... 00877,05/13/2010,PBO,P,0000708331,2103 00877,05/13/2010,PBO,P,0000708331,2104,etc,etc Basically I have to replace 518,519,2103,2104,... (4 Replies)
Discussion started by: Devski123
4 Replies

7. Shell Programming and Scripting

Replacing Characters with |

Hi All, example data.log 526569346 66815531961 09 526569346 66815531961 09 526569346 66815531961 09 526569346 66815531961 09 526569346 66815531961 09 I want like this to 526569346|66815531961|09 526569346|66815531961|09... (4 Replies)
Discussion started by: ooilinlove
4 Replies

8. Shell Programming and Scripting

Replacing Characters

Hi All, I have a file which is delimeted with the character '. i need to replace this character with the same character and also a new line. Can anyone please help me with the tr command for this. Many thanks Karan (11 Replies)
Discussion started by: karansachdeva
11 Replies

9. Shell Programming and Scripting

replacing characters

hi all I have a file that has sone spaces in start then / at last. i want to get rid of this. how to do? eg. 11414/ 49878/ 27627/ I WANT THE FILE AS 11414 49878 27627 PLEASE HELP (3 Replies)
Discussion started by: infyanurag
3 Replies

10. UNIX for Dummies Questions & Answers

replacing characters

Hi, I have a script for replacing bad characters in filenames for f in *; do mv $f `echo $f | tr '+' '_'` done; this replaces + for _ But I need to replace all bad characters ? / % + to _ Pls how can i do this in one script ? (3 Replies)
Discussion started by: palmer18
3 Replies
Login or Register to Ask a Question