Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace string at end of matching line 2

Status
Not open for further replies.

RFC1795

IS-IT--Management
Feb 13, 2003
76
US
Hi all,

This is probably simple but for some reason I'm struggling to find an answer.

I'm hoping to use sed to replace a random string in a file that matches a string with something else.

Example, in the file.txt it contains various lines of text and also the lines I wish to change:
Code:
text
Telephone number 1 555-3232
more text
Telephone number 1 3432223
Fax number 1 4645787-344
more text etc

I'm looking to replace the last string in any lines matching: "string 'number 1' <string_to_be_replaced>"

So I want to come out with something like this:
Code:
text
Telephone number 1 XXXXXXX
more text
Telephone number 1 XXXXXXX
Fax number 1 XXXXXXX
more text etc

Hope that makes sense.

Thanks... T
 
Hi Feherke

Thanks, that's pretty close to what I need, except I still see the numbers.

Output appears as so using your example:
Code:
text
Telephone number 1 555-3232XXXXXXX
more text
Telephone number 1 3432223XXXXXXX
Fax number 1 4645787-344XXXXXXX
more text etc

I'll have a play around with what you suggested and see how I get on.

Thanks... T
 
Hi Feherke

That does look a whole lot better :)

Thanks a lot for your help there!!

Cheers... T
 
BTW, why when I click the " Thank feherke
for this valuable post!" does nothing happen?

No message saying a window been blocked and I've got full jave enabled.

Strange?
 
Never mind ... I figured out the voting issue. Window was open and hidden under the piles of other ones hehe.

However, I've been thinking of using the above line in another area for another file containing passwords.

I applied the following to my test file:

Code:
sed '/password/s/[0-9 A-Z-]*$/ XXXXXXXXXXXX/'

However, the password contains various other characters eg:
Code:
$1$LuDC$b/siw3\*&sw.{]%sdjhakw83q2

Is this also something easily adjusted in the above line? I tried some options but gave up as it's not urgent really.

Thanks T

 
Rather than trying to list all possible encrypted password characters in your character class, you can instead use an exclusion list based on the field separator. If the syntax of the file just uses space separators, e.g.:

Code:
password $1$LuDC$b/siw3\*&sw.{]%sdjhakw83q2

Then this should do it:

Code:
sed '/password/s/ [^ ]*/ XXXXXXXXXXXX /'

If that's not the syntax of the file and you can't figure out how to change it, please post an example of the file content.


Annihilannic.
 
Hi

I think RFC1795 actually posted an example of file content :

Code:
 [red]ID[/red] [green]username[/green]     [blue]password[/blue]
 [red]|[/red]  [green]|_[/green]   [blue]___________|____________[/blue]
 [red]|[/red] [green]/[/green]  [green]\[/green] [blue]/[/blue]                        [blue]\[/blue]
[gray]$[/gray][red]1[/red][gray]$[/gray][green]LuDC[/green][gray]$[/gray][blue]b/siw3\*&sw.{]%sdjhakw83q2[/blue]
[gray]|[/gray] [gray]|[/gray]    [gray]|[/gray]
[gray]+-+----+--- delimiter[/gray]
In which case Annihilannic's code needs just minor modifications :
Code:
sed 's/\$[^$]*$/$XXXXXXXXXXXX/'


Feherke.
 
Hi chaps

Thanks both for taking the time to help and explain this one for me :)

I've tried both examples you provided. Annihilannic one replaced the word password with X's
And Feherke's one replaced some of it exactlly as described it would.

The thing is, that whole string is the actual password in its encrypted format:
Code:
$1$LuDC$b/siw3\*&sw.{]%sdjhakw83q2

Maybe it would be better to work it this way; replace the whole string after the word password as i'm sure it will always be only that (I hope anyway)

I'm just struggling to understand the deliminator syntax to make it work.

 
What about this ?
Code:
sed '/password/s/ [^ ]*$/ XXXXXXXXXXXX/'

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PHV - works 100% that :)

Thanks all for the help as usual .. one day all these bits and pieces of puzzling bits will fall into place in my head hehe.

Cheers... T
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top