WeTi
asked on
Replace text in powershell.
Dear expert
I would like to programming using powershell the idea is in a test like this:
Text, text, Text, text, Text, text,
Text, text, Text, text, Text, text, Text,
text, Text, text, Text, text, Text, text, Text,
text1, Text2, text3, Text4, text5, Text6, text, Text, text, Text, text, Text, text, Text,
I was to grab the last line of text like text5 and replace the text5 to test_weti is it possible?
I would like to programming using powershell the idea is in a test like this:
Text, text, Text, text, Text, text,
Text, text, Text, text, Text, text, Text,
text, Text, text, Text, text, Text, text, Text,
text1, Text2, text3, Text4, text5, Text6, text, Text, text, Text, text, Text, text, Text,
I was to grab the last line of text like text5 and replace the text5 to test_weti is it possible?
ASKER
what happens in this case:
Text, text, Text, text, Text, text,
Text, text, Text, text, Text, text, Text,
text, Text, text, Text, text, Text, text, Text,
text1, Text2, text3, Text4, text5, Text6, text, Text, text5, Text, text, Text5, text, Text,
I only want to change first text5
Text, text, Text, text, Text, text,
Text, text, Text, text, Text, text, Text,
text, Text, text, Text, text, Text, text, Text,
text1, Text2, text3, Text4, text5, Text6, text, Text, text5, Text, text, Text5, text, Text,
I only want to change first text5
Then
$path = "c:\path\to\your\file\in\quotes\TEXT.txt"
(Get-Content $path | Out-String) -replace "(.*?)text5(.*)",('$1{0}$2' -f "test-weti") | out-file $path
ASKER
Thanks, what happen if I want to change the middle test5?
I need to learn about this sorry. This:
(.*?)text5(.*) .*? what does it means? and the '$1{0}$2' what does that means?
I need to learn about this sorry. This:
(.*?)text5(.*) .*? what does it means? and the '$1{0}$2' what does that means?
This reads the whole file, then reads up to the first instance of the string you want to replace using find and replace, then ignores everything after that first instance.
"what happen if I want to change the middle test5? "
There are only two - if you want to be case sensitive :)
-replace isn't case sensitive but there are other PS commands that can just look for "Test5" and ignore "test5"
I just spotted Dan has posted a link to a summary of basic commands - have a read through and then come back :)
"what happen if I want to change the middle test5? "
There are only two - if you want to be case sensitive :)
-replace isn't case sensitive but there are other PS commands that can just look for "Test5" and ignore "test5"
I just spotted Dan has posted a link to a summary of basic commands - have a read through and then come back :)
ASKER
Hi, after the reading the RegExp, this: (.*?)text5(.*) .*? the . means matches any single character, * is matches any instances of the preceding character, ? Matches zero or one instance of the preceding character. Those critera together will match the text5, but my question is I want to grab the middle text5, the capital T may be confusing ok i change it:
Text, text, Text, text, Text, text,
Text, text, Text, text, Text, text, Text,
text, Text, text, Text, text, Text, text, Text,
text1, text2, text3, text4, text5, text6, text, text, text5, text, text, text5, text, text,
I want to change the second text5 not first one.
Text, text, Text, text, Text, text,
Text, text, Text, text, Text, text, Text,
text, Text, text, Text, text, Text, text, Text,
text1, text2, text3, text4, text5, text6, text, text, text5, text, text, text5, text, text,
I want to change the second text5 not first one.
RegEx allows for complex expressions and simple ones, often doing the same. A simple but inflexible way to check for the second occurance of test5 is:
$path = "c:\path\to\your\file\in\quotes\TEXT.txt"
(Get-Content $path -ReadCount 0) -replace '( text5,.*) text5,', '$1 yourreplacement,'
This requires the text to be repeated twice, and having text following (because of the checking of a terminating comma).
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Yep, that "greedy" always gets me :/
Open in new window