LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 09-02-2010, 01:43 AM   #1
steve51184
Member
 
Registered: Dec 2006
Posts: 372

Rep: Reputation: 30
how do i replace a text string in a file with a random string? (with sed etc)


hey all i have a line in a text file that has 40 random characters within a tag and i want to change the characters to a new set of 40 random characters (alphanumeric a-z 0-9 etc)

the line in the text file looks like this:

Quote:
<string>40randomcharacters</string>
how would i go about doing that?

also second question same as the above but how would i remove them instead of replacing them?
 
Old 09-02-2010, 02:13 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

I do hope your example is relevant, using tags might complicate things a bit:

Changing the 40 chars:
sed 's%<string>40randomcharacters</string>%<string>NEW40RANDOM</string>%' infile

Removing them:
sed 's%<string>40randomcharacters</string>%<string></string>%' infile

If you also want to remove the tags:
sed 's%<string>40randomcharacters</string>%%' infile

Hope this helps.
 
Old 09-02-2010, 02:23 AM   #3
steve51184
Member
 
Registered: Dec 2006
Posts: 372

Original Poster
Rep: Reputation: 30
wow thank you for the fast reply and that is almost what I need

Basicly the 40randomcharacters is just that random and that command won't find that line :/

Also I'm wanting to replace it with a new random lot of 40 random characters..

In other words I need to replace everything within the <string> tag with 40 random characters

Sorry for the confusion and I hope that makes sense
 
Old 09-02-2010, 02:40 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Totally my bad, it is still early where I live....

sed 's%<string>.*<string>%<string>NEW40RANDOM</string>%' infile

The above will replace anything within the string tags with NEW40RANDOM. You don't mention how these 40 new chars are generated, if they are in a variable then this should work (VARIABLE holds the new chars):

sed "s%<string>.*<string>%<string>$VARIABLE</string>%" infile

Mind the quoting, it switched from single to double.

Hope this is what you are looking for.
 
Old 09-02-2010, 02:44 AM   #5
steve51184
Member
 
Registered: Dec 2006
Posts: 372

Original Poster
Rep: Reputation: 30
They will be genarated by this cod or part of it lol

Not sure how to do it but thats why I'm asking
 
Old 09-02-2010, 02:58 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by steve51184 View Post
hey all i have a line in a text file that has 40 random characters within a tag and i want to change the characters to a new set of 40 random characters (alphanumeric a-z 0-9 etc)
If you have Python
Code:
import random
import string
alnum=string.ascii_letters+string.digits
s=""
for i in range(40):
    s=s+random.choice(alnum)
with open("file") as f:
    for line in f:
        if "</string>" in line:
            line=line.replace("<string>","<string>"+s)
        print line.rstrip()
 
Old 09-02-2010, 03:11 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Took a little longer, but this works:
Code:
#!/bin/bash

nanosectime=`date '+%N'`
awk -v NST=$nanosectime '
BEGIN { 
split("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", choices, "")
srand(NST)
i = 1
while ( i <= 40 ) {
  printf("%s", choices[int(1 + rand() * 61)])
  i++
  }
print ""
}'
 
Old 09-02-2010, 03:24 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
srand(systime()) might be sufficient..
 
Old 09-02-2010, 03:38 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,
Quote:
Originally Posted by ghostdog74 View Post
srand(systime()) might be sufficient..
Doesn't systime() stop at seconds?

With the speed of modern computers you might end up feeding the same string a few times. The nanosectime=`date '+%N'` makes sure that the number is always different. Just a precaution on my side.
 
Old 09-02-2010, 10:56 AM   #10
steve51184
Member
 
Registered: Dec 2006
Posts: 372

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by ghostdog74 View Post
If you have Python
Code:
import random
import string
alnum=string.ascii_letters+string.digits
s=""
for i in range(40):
    s=s+random.choice(alnum)
with open("file") as f:
    for line in f:
        if "</string>" in line:
            line=line.replace("<string>","<string>"+s)
        print line.rstrip()
not to sure about python.... how would i run that script and how do i set it to run on a specific file? save it as script.py and run it as './script.py file.txt' ?

Quote:
Originally Posted by druuna View Post
Hi,

Took a little longer, but this works:
Code:
#!/bin/bash

nanosectime=`date '+%N'`
awk -v NST=$nanosectime '
BEGIN { 
split("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", choices, "")
srand(NST)
i = 1
while ( i <= 40 ) {
  printf("%s", choices[int(1 + rand() * 61)])
  i++
  }
print ""
}'
could that be integrated into the other sed script you posted before to make it 100% complete?

Last edited by steve51184; 09-02-2010 at 10:58 AM.
 
Old 09-02-2010, 10:58 AM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Save as a filename, run
Code:
$ python myscript.py
 
Old 09-02-2010, 11:03 AM   #12
steve51184
Member
 
Registered: Dec 2006
Posts: 372

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by ghostdog74 View Post
Save as a filename, run
Code:
$ python myscript.py
so:

python myscript.py myfile.txt

?
 
Old 09-02-2010, 11:11 AM   #13
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Code:
#!/bin/bash

nanosectime=`date '+%N'`
rndString=$(awk -v NST=$nanosectime '
BEGIN { 
split("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
choices, "")
srand(NST)
i = 1
while ( i <= 40 ) {
  printf("%s", choices[int(1 + rand() * 61)])
  i++
  }
print ""
}')

sed -i.bak "s%<string>.*</string>%<string>$rndString</string>%" infile
This will change in place and creates a copy of the original with a .bak extension attached.

Hope this helps.
 
Old 09-02-2010, 11:11 AM   #14
steve51184
Member
 
Registered: Dec 2006
Posts: 372

Original Poster
Rep: Reputation: 30
tried to run it as:

python myscript.py myfile.txt

and also changed 'file' t oteh location of myfile.txt but got this error:

Quote:
file.py:7: Warning: 'with' will become a reserved keyword in Python 2.6
File "file.py", line 9
as f:
^
SyntaxError: invalid syntax
 
Old 09-02-2010, 11:42 AM   #15
steve51184
Member
 
Registered: Dec 2006
Posts: 372

Original Poster
Rep: Reputation: 30
perfect thank you all very much
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to replace a string in a text file jpan Linux - General 3 10-14-2012 07:17 PM
Problem using sed to replace string in file umk Debian 12 02-01-2012 09:39 AM
[SOLVED] Replace 2nd occurrence of a string in a file - sed or awk? kushalkoolwal Programming 10 05-02-2011 03:30 PM
How to replace string containing / in a text file tikit Linux - Newbie 4 09-05-2008 09:48 AM
replace a string/number in a text file jpan Linux - General 3 10-22-2004 10:33 PM


All times are GMT -5. The time now is 12:47 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration