LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 12:43 AM   #1
steve51184
Member
 
Registered: Dec 2006
Posts: 381

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, 01:13 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 01:23 AM   #3
steve51184
Member
 
Registered: Dec 2006
Posts: 381

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, 01:40 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 01:44 AM   #5
steve51184
Member
 
Registered: Dec 2006
Posts: 381

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, 01:58 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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, 02:11 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 02:24 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

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

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 09:56 AM   #10
steve51184
Member
 
Registered: Dec 2006
Posts: 381

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 09:58 AM.
 
Old 09-02-2010, 09:58 AM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

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

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, 10:11 AM   #13
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 10:11 AM   #14
steve51184
Member
 
Registered: Dec 2006
Posts: 381

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, 10:42 AM   #15
steve51184
Member
 
Registered: Dec 2006
Posts: 381

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


Reply



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
[SOLVED] Replace 2nd occurrence of a string in a file - sed or awk? kushalkoolwal Programming 26 09-26-2021 04:10 PM
How to replace a string in a text file jpan Linux - General 3 10-14-2012 06:17 PM
Problem using sed to replace string in file umk Debian 12 02-01-2012 08:39 AM
How to replace string containing / in a text file tikit Linux - Newbie 4 09-05-2008 08:48 AM
replace a string/number in a text file jpan Linux - General 3 10-22-2004 09:33 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:20 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration