I want to get the .txt file group by block lines that starts at “BEGIN” and ends with “END00” and the first word of the “BEGIN” line is BLUE and the first word line “END00” is YELL has which replace by YELL BLUE.
I would use Import-CSV to import the file, specifying a -Delimiter “|” to have it break on the pipe characters. From there, a ForEach loop would let you enumerate each line one at a time and work with the column values.
ForEach ($line in (Import-CSV whatever.txt -Delim “|”)) {
$line[0] # first column
$line[5] # last column
}
Unfortunately, I’m not understanding what it is you want to do with the data. Additionally, Export-CSV, while it can use the | as a delimiter, might not preserve the column widths. You might need to manage that yourself.
these lines within the foreach are not valid because if your realizing the example the “END” is not always in position 5, can appear in position 4 or position 6 or in any other.
$line[0] # first column
$line[5] # last column
I think it should be something who searched the strings “BEGIN” and “END”, but do not know what.
What I want to do with the data is something something like this:
foreach {IF (H5 like “BEGIN” and H1 like “BLUE” and H5 Like “END” and H1 like “YELL”) {
H1 replace “YELL”, “BLUE”
}
}
And change the initial document only these cases and keep the rest who do not obey this condition.
Do all lines start with ‘BLUE’ or ‘YELL’?
If so, do you need to capture all lines between the ‘BEGIN’ and ‘END00’ lines of each text file?
If not, do you need to capture ONLY lines that begin with ‘BLUE’ or ‘YELL’ that are between the
‘BEGIN’ and ‘END00’ lines of each text file?
I need to capture ONLY lines between the ‘BEGIN’ and ‘END00’ that has “BLUE” in ‘BEGIN’ and “YELL” in ‘END00’.
I need to pass only the lines corresponding to this condition and this code passes all the lines and corrects all “YELL” to “BLUE” even those who do not obey to this condition.
The problem of the both post are both search every line of the .txt file and when find the string “YELL” replace “YELL” by “BLUE” independently the string are in the first column or in 10th.
A big problem is if you have a string “YELLOW” with the both post the result are “BLUEOW” because it´s made the replace “YELL” by “BLUE”
Another problem is I wont Only replace the “YELL” by “BLUE” When i have something like that:
Hi Ricardo,
Here is an example of the logic you need. Basically you have to test each line to determine if is it the appropriate begin or end line and match them up to create your block of text. You can then do your replace on just that block. Let me know if you need explanation on what the below is doing.
Two questions for now if I want import a txt file instead of a multiple lines how do I do that?
And for export for a txt file with same structure have in the initial files how do I do that?
Hi Richardo,
You would use get-content to get the content of the text file.
You would use out-file to write the data out to a file. Since the out-put data is being generated in a for loop, you would need to either output the data with out-file using the -append parameter inside the for loop, or collect all the data into a variable inside the for loop and then write it out to file once after the loop has completed.
Your Add-content is in the wrong place and adding the wrong content. Additionally, your replace logic is backward from the original example. Replacing FE with 01 is like replacing BLUE with YELL instead of YELL with BLUE. In any case. Here is the right way replacing 01 with FE instead of FE with 01 and using Add-Content in the correct spot.