read values from a file, change them and write them back to another file

rated by 0 users
This post has 1 Reply | 1 Follower

Not Ranked
Posts 1
returnpath37 Posted: 05-04-2012 8:01 PM

the command

$fld = Select-String -Path mm1.txt -Pattern "jpg"

gives me this result

mm1.txt:2:"P1008.jpg",162.3
mm1.txt:3:"P1009.jpg",33.2
mm1.txt:4:"P1010.jpg",50

I now want to multiply the values which follows to ", in each line (or which can be found starting from the 23rd letter) to multiply with 3 and write them into a file mm2.txt (including some data before) that way:

whatsoever
"P1009.jpg",486.9
"P1009.jpg",99.6
"P1010.jpg",150

whatsoever is something that always can be found in the 1st line of mm1.txt and it should be transfered to mm2.txt too.

My question is how do I do all this?

I have made some thoughts by my own and here they are:

I need to know how much lines $fld consists of. How do I do that?
Then I have to separate the lines of $fld. How do I do that?
Then I have to cut off 22 letters of a line to get a value which I can multiply with 3.
(Or find a smarter solution for that.)
Then I have to reassemble it somehow. Any ideas (belonging to that)?
I would call that big string $fla and write it to mm2.txt

How do I do all this?
The main problem is that I don't know the expressions.

Can you give me some hints? (Or something which is ready to work?)

Top 10 Contributor
Posts 640

Well, I can't write a ready-to-work script for you, but I'm happy to answer the questions you asked. Bear in mind that what you're asking to accomplish is quite complex - you're going to need to experiment, and end up teaching yourself quite a bit.

I need to know how much lines $fld consists of. How do I do that?

If you mean "how many lines," you'd run "Get-Content filename.txt | Measure" to get a count of lines. Assuming it's a text file. Nothing in PowerShell can natively read binary file formats (like JPG).

Then I have to separate the lines of $fld. How do I do that?

Assuming it's a text file, Get-Content will automatically read each line as a separate object. I'm not sure that's the case, but you haven't said. Your example uses JPG files; nothing in PowerShell can natively read a JPG file's binary format in a sensible fashion. But you can do something like "Get-Content filename.txt | ForEach-Object { $_ }" and the $_ will represent a single line of the text file.

Then I have to cut off 22 letters of a line to get a value which I can multiply with 3.

Use the Substr() method of the line - the line is a System.String object, and has a Substr() method. Google "System.String" and you should get to the documentation quickly.

Then I have to reassemble it somehow. Any ideas (belonging to that)?

I'm not sure what you mean by "reassemble." You can concatenate strings using the + operator.

I'm not entirely sure PowerShell is the best possible tool for this job. PowerShell doesn't have as great string-handling capabilities as, say, Perl, or even PHP. I'm not even sure you're reading text files, but your questions suggest that you are. If you aren't working with text files, then my answers won't be useful to you.

Page 1 of 1 (2 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.