Replace a pattern in a file with a generated number using sed or awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace a pattern in a file with a generated number using sed or awk
# 1  
Old 06-20-2012
Replace a pattern in a file with a generated number using sed or awk

my file has thousands of line but let me show what i want to achieve... here is one line from that file


Code:
 
cat fileName.txt
(2,'','user3002,user3003','USER_DATA_SINGLE',1,0,0,'BACKUP',2,NULL,0,450,NULL,NULL,'','2011-05-10 09:22:06','/var/opt/ipc/service_backup_restore','LOCAL',NULL,'2012-06-16 08:12:24');

here you can observe that for
Code:
'user3002,user3003'

there is a comma inbetween single quotes... here instead of this comma i want to replace a varchar say Xox9870563xoX

we cant directly cut using this single quotes since there are others strings having single quotes... one way we could do is if there is any comma between ,' and ', then replace with that number.... is this achievable using sed or awk...?

the output should look something like below

Code:
 
cat fileName.txt
(2,'','user3002Xox9870563xoXuser3003','USER_DATA_SINGLE',1,0,0,'BACKUP',2,NULL,0,450,NULL,NULL,'','2011-05-10 09:22:06','/var/opt/ipc/service_backup_restore','LOCAL',NULL,'2012-06-16 08:12:24');

any help is deeply appreciated....
# 2  
Old 06-20-2012
try this
Code:
sed "s/'\([[:alnum:]][[:alnum:]]*\),\([[:alnum:]][[:alnum:]]*\)'/\1Xox9870563xoX\2/" infile

# 3  
Old 06-20-2012
Code:
[jun19]# cat vkk
INSERT INTO `Table55` (`id`, `backupReason`, `backupScope`, `backupType`, `controllingZoneId`, `delaySeconds`, `immediateBackup`, `jobType`, `maxRetries`, `performingZoneId`, `recurring`, `requestingUserId`, `schemaDifference_blob_reserved`, `schemaDifference_reserved`, `scpKey`, `startDateTime`, `storageFile`, `storageType`, `zoneScope`, `lastModified`) VALUES (2,'','user3002,user3003','USER_DATA_SINGLE',1,0,0,'BACKUP',2,NULL,0,450,NULL,NULL,'','2011-05-10 09:22:06','/var/opt/ipc/service_backup_restore','LOCAL',NULL,'2012-06-16 08:12:24');
[jun19]#  sed "s/'\([[:alnum:]][[:alnum:]]*\),\([[:alnum:]][[:alnum:]]*\)'/\1Xox9870563xoX\2/"  vkk
INSERT INTO `Table55` (`id`, `backupReason`, `backupScope`, `backupType`, `controllingZoneId`, `delaySeconds`, `immediateBackup`, `jobType`, `maxRetries`, `performingZoneId`, `recurring`, `requestingUserId`, `schemaDifference_blob_reserved`, `schemaDifference_reserved`, `scpKey`, `startDateTime`, `storageFile`, `storageType`, `zoneScope`, `lastModified`) VALUES (2,'',user3002Xox9870563xoXuser3003,'USER_DATA_SINGLE',1,0,0,'BACKUP',2,NULL,0,450,NULL,NULL,'','2011-05-10 09:22:06','/var/opt/ipc/service_backup_restore','LOCAL',NULL,'2012-06-16 08:12:24');


the code partially works.. because its deleting the enclosing single quotes for
Code:
user3002Xox9870563xoXuser3003

# 4  
Old 06-20-2012
lets add the single quotes

Code:
sed "s/'\([[:alnum:]][[:alnum:]]*\),\([[:alnum:]][[:alnum:]]*\)'/'\1Xox9870563xoX\2'/" infile

# 5  
Old 06-20-2012
thanks it working good :-)
# 6  
Old 06-20-2012
If your file is always formatted the same way :

Code:
sed 's/,/Xox9870563xoX/3' yourfile

# 7  
Old 06-20-2012
but wont that replace all comma's in the file.... i wanted to replace only if it comes between ,' ',

---------- Post updated at 02:05 PM ---------- Previous update was at 01:56 PM ----------

@ygemici


the code wont work for below scenario... how to modify the code to work for below scenario


Code:
(1,NULL,'FULL',1,0,'Immediate Backup,Database backup successfull,License backup successfull,Completed backup operation successfully',5,'COMPLETED','BACKUP',2,100,NULL,NULL,NULL,NULL,'2011-04-04 05:33:33');

there are multiple comma's between ,' and ',
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pattern replace from a text file using sed

I have a sample text format as given below <Text Text_ID="10155645315851111_10155645333076543" From="460350337461111" Created="2011-03-16T17:05:37+0000" use_count="123">This is the first text</Text> <Text Text_ID="10155645315851111_10155645317023456" From="1626711840902323"... (3 Replies)
Discussion started by: my_Perl
3 Replies

2. Shell Programming and Scripting

sed command to replace two character pattern with another pattern

Not able to paste my content. Please see the attachment :-( (2 Replies)
Discussion started by: vivek d r
2 Replies

3. Shell Programming and Scripting

Sed/awk/perl command to replace pattern in multiple lines

Hi I know sed and awk has options to give range of line numbers, but I need to replace pattern in specific lines Something like sed -e '1s,14s,26s/pattern/new pattern/' file name Can somebody help me in this.... I am fine with see/awk/perl Thank you in advance (9 Replies)
Discussion started by: dani777
9 Replies

4. Shell Programming and Scripting

sed to replace the matching pattern with equal number of spaces

Hi I have written a shell script which used sed code below sed -i 's/'"$Pattern"'/ /g' $FileName I want to count the length of Pattern and replace it with equal number of spaces in the FileName. I have used $(#pattern) to get the length but could not understand how to replace... (8 Replies)
Discussion started by: rakeshkumar
8 Replies

5. Shell Programming and Scripting

replace using pattern using awk or sed

I have file file_1.txt which contains: file_1.txt <tr> 1 MAIL # 1 TO src_1 </tr> <tr><td class="hcol">col_id</td> <td class="hcol">test_dt</td> <td class="hcol">user_type</td> <td class="hcol">ct</td></tr> <tr><td class="bcol">1</td> <td class="bcol">2012-09-20</td> <td class="bcol">a</td>... (2 Replies)
Discussion started by: sol_nov
2 Replies

6. Shell Programming and Scripting

Pattern Replace using sed or awk

Hi , My file have data like 4:ALMOST NEVER PR 1925836 5:NEVER PR W DDA 5857610 6:NEVER PR WO DDA 26770205 but i want to replace the spaces before last numric digits out put should be like this 4:ALMOST NEVER PR=1925836 5:NEVER PR W DDA=5857610 6:NEVER PR WO... (7 Replies)
Discussion started by: max_hammer
7 Replies

7. Shell Programming and Scripting

Can sed replace every 2 instances it finds in a file? Pattern.

My goal is to make a script to find/replace the variable "PORT" with a unique number. Like the following <VirtualHost 174.120.36.236:PORT> ServerName architect.com.ph ServerAlias www.architect.com.ph DocumentRoot /home/architec/public_html ServerAdmin... (16 Replies)
Discussion started by: EXT3FSCK
16 Replies

8. Shell Programming and Scripting

pattern replace inside text file using sed

Hi, I have a situation where I want to replace some occurrences of ".jsp" into ".html" inside a text file. For Example: If a pattern found like <a href="http://www.mysite.com/mypage.jsp"> it should be retained. But if a pattern found like <a href="../mypage.jsp"> it should be changed to... (4 Replies)
Discussion started by: meharo
4 Replies

9. Shell Programming and Scripting

Need help in sed command (adding a blank line btw each block generated by pattern)

Hello friends, I have a C source code containing sql statements. I use the following sed command to print all the sql blocks in the source code.... sed -n "/exec sql/,/;/p" Sample.cpp The above sed command will print the sql blocks based on the pattern "exec sql" & ";"... (2 Replies)
Discussion started by: frozensmilz
2 Replies

10. Shell Programming and Scripting

awk or sed for finding closest pattern to a line number

hi guys, I want to do pattern matching with awk or sed but I don't know how. here's what I want: I have a line number for a pattern that I have already found using grep, and I know a pattern like "---" that happens a few lines above that certain line number. I want to print out the chunk... (1 Reply)
Discussion started by: alirezan
1 Replies
Login or Register to Ask a Question