1

I am using PowerShell and I need replace a line in a .txt file.

The .txt file always has different number at the end of the line.

For example:

...............................txt (first)....................................
appversion= 10.10.1
............................txt (a second time)................................
appversion= 10.10.2
...............................txt (third)...................................
appversion= 10.10.5

I need to replace appversion + number behind it (the number is always different). I have set the required value in variable.

How do I do this?

PeterK
3,8372 gold badges19 silver badges24 bronze badges
asked Apr 21, 2015 at 8:33
1
  • Are you wanting to replace all appversion= 10.10.X with the same thing? Commented Apr 21, 2015 at 8:36

2 Answers 2

2

Part of this issue you are getting, which I see from your comments, is that you are trying to replace text in a file and saved it back to the same file while you are still reading it.

I will try to show a similar solution while addressing this. Again we are going to use -replaces functionality as an array operator.

$NewVersion = "Awesome"
$filecontent = Get-Content C:\temp\file.txt
$filecontent -replace '(^appversion=.*\.).*',"`1ドル$NewVersion" | Set-Content C:\temp\file.txt 

This regex will match lines starting with "appversion=" and everything up until the last period. Since we are storing the text in memory we can write it back to the same file. Change $NewVersion to a number ... unless that is your versioning structure.

Not sure about what numbers you are keeping

About which part of the numbers, if any, you are trying to preserve. If you intend to change the whole number then you can just .*\. to a space. That way you ignore everything after the equal sign.

answered Apr 21, 2015 at 11:23
Sign up to request clarification or add additional context in comments.

Comments

0

Yes, you can with regex. Let call $myString and $verNumber the variables with text and version number

$myString = "appversion= 10.10.1";
$verNumber = 7;

You can use -replace operator to get the version part and replace only last subversion number this way

$mystring -replace 'appversion= (\d+).(\d+).(\d+)', "appversion= `1ドル.`2ドル.$verNumber";
answered Apr 21, 2015 at 8:50

3 Comments

(gc c:\test.txt) -replace 'appversion=(/d)',"appversion= $FolderName" | Set-content c:\test.txt
Is gc c:\test.txt returning you a string or an object?
so you should get the text, try gc c:\test.txt -raw I suggest you to split your code, instead of using pipe save each step in a variable and check step by step what is wrong

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.