how do i replace a text string in a file with a random string? (with sed etc)
Linux - SoftwareThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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()
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.
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
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 09:58 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.