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
# 8  
Old 06-20-2012
Is this working? Changed the script to multiple patterns.
Code:
awk 'BEGIN{FS=OFS="'"}{for(i=1;i<=NF;i++) if($i ~ /user.*,user.*|Immediate Backup|whatever/) gsub(/,/,"Xox9870563xoX",$i)};1' infile >outfile


Last edited by sdf; 06-20-2012 at 06:32 AM.. Reason: modified sub->gsub and multiple patterns
# 9  
Old 06-20-2012
its not working some syntax problem i guess... with quotes


Code:
awk 'BEGIN{FS=OFS="'"}{for(i=1;i<=NF;i++) if($i ~ /user.*user/) sub(",","Xox9870563xoX",$i)};1'  vo
>
>
>

here vo is file name and whn i execute nothing happens..
# 10  
Old 06-20-2012
Quote:
Originally Posted by vivek d r
its not working some syntax problem i guess... with quotes
Filename vo is not the problem. Try this code. Have changed sub to gsub

Code:
awk 'BEGIN{FS=OFS="'"}{for(i=1;i<=NF;i++) if($i ~ /user.*,user.*|Immediate Backup|whatever/) gsub(/,/,"Xox9870563xoX",$i)};1' vo

# 11  
Old 06-20-2012
@sdf one more doubt are these
Code:
user.*,user.*|Immediate Backup|whatever/)

specific to file contents.... what if there are different names instead of "user"

it should be irrespective of any words in the file... :-(


and still above code dint run... i am guessing my linux machine doesnt support gsub or sub... can it be this reason...?
# 12  
Old 06-20-2012
Nope, the sed statement i proposed (post #6 ) will only replace the third coma of every line.

That's why it supposes that the format of your file doesn't vary.

Just another one in another style:

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

---------- Post updated at 03:03 PM ---------- Previous update was at 01:49 PM ----------

Code:
awk '{OFS=NR%2?",":"Xox9870563xoX";$1=$1}1' RS="\'" FS=,  ORS="\'" yourfile

Code:
# cat tst
a,b,'jklfds','a,b,a,c','f','g','c','c,d'
a,b,'jklfds','a,b,a,c','f','g','c','c,d'
# awk '{OFS=NR%2?",":"#";$1=$1}1' RS="\'" FS=,  ORS="\'" tst
a,b,'jklfds','a#b#a#c','f','g','c','c#d'
a,b,'jklfds','a#b#a#c','f','g','c','c#d'
'# awk '{OFS=NR%2?",":"Xox9870563xoX";$1=$1}1' RS="\'" FS=,  ORS="\'" tst
a,b,'jklfds','aXox9870563xoXbXox9870563xoXaXox9870563xoXc','f','g','c','cXox9870563xoXd'
a,b,'jklfds','aXox9870563xoXbXox9870563xoXaXox9870563xoXc','f','g','c','cXox9870563xoXd'
'#

Oops ... there is a trailing simple quote to remove ...

Last edited by ctsgnb; 06-20-2012 at 10:16 AM..
This User Gave Thanks to ctsgnb For This Post:
# 13  
Old 06-20-2012
thanks will try this now.. i am getting a warning is this normal

Code:
awk '{OFS=NR%2?",":"Xox9870563xoX";$1=$1}1' RS="\'" FS=,  ORS="\'" vo
awk: cmd. line:1: warning: escape sequence `\'' treated as plain `''

---------- Post updated at 06:50 PM ---------- Previous update was at 06:39 PM ----------

thanks this code is working superb:-)

thanks a lot ctsgnd
# 14  
Old 06-20-2012
Quote:
Originally Posted by vivek d r
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 ',
try this
Code:
# awk -F"'[,']" -vc="Xox9870563xoX" -vx="'" -vxx="," '{for(i=1;i<=NF;i++)if($i~x ".*" xx){l=split($i,a,xx x);;for(j=1;j<l;j++)
lx=lx?lx a[j]:a[j] xx x;;gsub(xx,c,a[l]);;$i=lx a[l] x xx;printf "%s",$i;lx=""}else if(i!=NF)printf "%s%s",$i,x xx;else printf "%s",$i}END{print ""}' file
(1,NULL,'FULL',1,0,'Immediate BackupXox9870563xoXDatabase backup successfullXox9870563xoXLicense backup successfullXox9870563xoXCompleted backup operation successfully',5,'COMPLETED','BACKUP',2,100,NULL,NULL,NULL,NULL,'2011-04-04 05:33:33');


Last edited by ygemici; 06-20-2012 at 11:14 AM..
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