0

The file abc.txt must be read from the end of file till the specific string called "Install location: "C:\Users\desktop\1234_5467.dat".
The problem here is we have repetitive occurrences of the string like "1234_5467.dat" and I want the content to be read from the end till the "Install location: "C:\Users\desktop\1234_5467.dat" " without using tail as we dont have clear line number of the specified string line in the file. I am using reading from end of file as the contents in file gets appended every time so its better we go from end of file.

I have tried using Tail ,though it works we cant proceed as we the string line number is unpredictable

05/28/2019 18:25:08 - Successfully deleted HKEY_LOCAL_MACHINE\SOFTWARE\EMC\Symmwin\SystemInstallState Registry Key

05/28/2019 18:35:43 - =========================================================================

05/28/2019 18:35:43 - Installation Started

05/28/2019 18:35:43 - Installation Location: c:\Temp\456.exe

05/28/2019 18:42:20 - Tools Installation Completed

05/28/2019 18:42:20 - SnippT completed

05/28/2019 18:42:31 - Adobe Installation Completed.

05/28/2019 18:42:31 - Installing Paint

05/28/2019 18:48:00 - MS OFFICE Completed

05/28/2019 18:48:06 - Font Completed

05/28/2019 18:48:06 - Installing Windows Master File

05/28/2019 18:48:06 - Notepad++ Completed

05/28/2019 18:48:06 - Notepad Settings

05/28/2019 18:48:15 - ABC Installation Completed

05/28/2019 18:37:43 - --------------------------------------------------

05/28/2019 18:37:43 - This is the point of no return!

05/28/2019 18:37:43 - --------------------------------------------------

05/28/2019 18:37:44 - Installing Windows Features

05/28/2019 18:42:20 - Tools Installation Completed

05/28/2019 18:42:31 - Adobe Installation Completed.

05/28/2019 18:42:31 - Installing Paint

05/28/2019 18:48:00 - MS OFFICE Completed

05/28/2019 18:48:06 - Font Completed

05/28/2019 18:48:06 - Installing Windows Master File

05/28/2019 18:48:06 - Notepad++ Completed

05/28/2019 18:48:06 - Notepad Settings

05/28/2019 18:48:15 - ABC Installation Completed

05/28/2019 18:48:21 - --------------------------------------------------

05/28/2019 18:48:21 - Install Complete.

05/28/2019 18:48:21 - --------------------------------------------------

05/28/2019 18:35:43 - =========================================================================

5/28/2019 18:35:43 - Installation Started

05/28/2019 18:35:43 - Installation Location: c:\Temp\3456.exe

05/28/2019 18:35:43 - Installation Started

05/28/2019 18:35:43 - Installation Location: c:\Temp\456.exe

05/28/2019 18:42:20 - Tools Installation Completed

05/28/2019 18:42:20 - SnippT completed

05/28/2019 18:42:31 - Adobe Installation Completed.

05/28/2019 18:42:31 - Installing Paint

05/28/2019 18:48:00 - MS OFFICE Completed

05/28/2019 18:48:06 - Font Completed

05/28/2019 18:48:06 - Installing Windows Master File

05/28/2019 18:48:06 - Notepad++ Completed

05/28/2019 18:48:06 - Notepad Settings

05/28/2019 18:48:15 - ABC Installation Completed

05/28/2019 18:25:03 - Aborting the installation.

Aborting the installation!.

C:\ drive does not have enough space to install the application.

The minimum space required to install Enginuity installer in C:\ Drive is 2 GB.

05/28/2019 18:35:43 - =========================================================================

05/28/2019 18:35:43 - Installation Started

05/28/2019 18:35:43 - Installation Location: c:\Temp\3456.exe

05/28/2019 18:37:44 - Installing Windows Features

05/28/2019 18:42:20 - Tools Installation Completed

05/28/2019 18:42:31 - Adobe Installation Completed.

05/28/2019 18:42:31 - Installing Paint

05/28/2019 18:48:00 - MS OFFICE Completed

05/28/2019 18:48:06 - Font Completed

05/28/2019 18:48:06 - Installing Windows Master File

05/28/2019 18:48:06 - Notepad++ Completed

05/28/2019 18:48:06 - Notepad Settings

05/28/2019 18:48:15 - ABC Installation Completed

05/28/2019 18:37:43 - This is the point of no return!

05/28/2019 18:37:43 - --------------------------------------------------

05/28/2019 18:37:44 - Installing Windows Features

05/28/2019 18:42:20 - Tools Installation Completed

05/28/2019 18:42:31 - Adobe Installation Completed.

05/28/2019 18:42:31 - Installing Paint

05/28/2019 18:48:00 - MS OFFICE Completed

05/28/2019 18:48:06 - Font Completed

05/28/2019 18:48:06 - Installing Windows Master File

05/28/2019 18:48:06 - Notepad++ Completed

05/28/2019 18:48:06 - Notepad Settings

05/28/2019 18:48:15 - ABC Installation Completed

05/28/2019 18:48:21 - --------------------------------------------------

05/28/2019 18:48:21 - Install Complete.

05/28/2019 18:48:21 - --------------------------------------------------

I need the contents of file from last occurrence of c:\Temp\3456.exe till end

05/28/2019 18:35:43 - Installation Location: c:\Temp\3456.exe

05/28/2019 18:37:44 - Installing Windows Features

05/28/2019 18:42:20 - Tools Installation Completed

05/28/2019 18:42:31 - Adobe Installation Completed.

05/28/2019 18:42:31 - Installing Paint

05/28/2019 18:48:00 - MS OFFICE Completed

05/28/2019 18:48:06 - Font Completed

05/28/2019 18:48:06 - Installing Windows Master File

05/28/2019 18:48:06 - Notepad++ Completed

05/28/2019 18:48:06 - Notepad Settings

05/28/2019 18:48:15 - ABC Installation Completed

05/28/2019 18:37:43 - This is the point of no return!

05/28/2019 18:37:43 - --------------------------------------------------

05/28/2019 18:37:44 - Installing Windows Features

05/28/2019 18:42:20 - Tools Installation Completed

05/28/2019 18:42:31 - Adobe Installation Completed.

05/28/2019 18:42:31 - Installing Paint

05/28/2019 18:48:00 - MS OFFICE Completed

05/28/2019 18:48:06 - Font Completed

05/28/2019 18:48:06 - Installing Windows Master File

05/28/2019 18:48:06 - Notepad++ Completed

05/28/2019 18:48:06 - Notepad Settings

05/28/2019 18:48:15 - ABC Installation Completed

05/28/2019 18:48:21 - --------------------------------------------------

05/28/2019 18:48:21 - Install Complete.

05/28/2019 18:48:21 - --------------------------------------------------
Theo
  • 57,719
  • 8
  • 24
  • 41
Hidei
  • 197
  • 4
  • 15
  • Welcome as a new user to SO. Please take the [tour] and also read [ASK]. [SO] isn't a free script writing service. Own research and code attempts are expected. To get help with your code, show your code by [edit]ing your question to include a [mcve]. –  May 20 '19 at 16:13
  • Why not read line by line until you have `$str.contains("Install location")`? https://stackoverflow.com/questions/18877580/powershell-and-the-contains-operator – Riley Carney May 20 '19 at 16:54
  • 1
    It's probably overkill, but here's a regex in case the script also contains more instances of "Install Location": `$Match = Get-Content -Path "./abc.txt" | Select-String '\d{2}\/\d{2}\/\d{4}\s\d{2}:\d{2}:\d{2}\s-\s"Install\slocation:\s"C:\\Users\\desktop\\1234_5467.dat"'; $Match.LineNumber` – kuzimoto May 20 '19 at 17:11

1 Answers1

0

This new text example makes things different from what you posted before. Using this new example, the following should work:

$file   = 'D:\blah.txt'
$search = '*Installation Location: C:\Temp\*.exe*'

# get the data as string array, remove empty or whitespace only elements
$data = Get-Content -Path $file | Where-Object { ![string]::IsNullOrWhiteSpace($_) }

if (($data -like $search).Count -eq 0) {
    Write-Warning "Search string '$search' not found in file '$file'"
}
else {
    # read the array from last to first line and collect the lines in $result
    $result = @()
    for ($i = ($data.Count -1); $i -ge 0; $i--) {
        $result += $data[$i]
        if ($data[$i] -like $search) { 
            # keep adding lines to the result until the divider string 
            # '=============================================' is found
            while (--$i -ge 0 -and $data[$i] -notmatch '.*=+') {
                $result += $data[$i]
            }
            # also add the '=============================================' ?
            # $result += $data[$i]
            # exit the for loop because we're done
            break
        }
    }

    # reverse the collected lines
    [array]::Reverse($result)
    $result
}

Result:

05/28/2019 18:35:43 - Installation Started
05/28/2019 18:35:43 - Installation Location: c:\Temp\3456.exe
05/28/2019 18:37:44 - Installing Windows Features
05/28/2019 18:42:20 - Tools Installation Completed
05/28/2019 18:42:31 - Adobe Installation Completed.
05/28/2019 18:42:31 - Installing Paint
05/28/2019 18:48:00 - MS OFFICE Completed
05/28/2019 18:48:06 - Font Completed
05/28/2019 18:48:06 - Installing Windows Master File
05/28/2019 18:48:06 - Notepad++ Completed
05/28/2019 18:48:06 - Notepad Settings
05/28/2019 18:48:15 - ABC Installation Completed
05/28/2019 18:37:43 - This is the point of no return!
05/28/2019 18:37:43 - --------------------------------------------------
05/28/2019 18:37:44 - Installing Windows Features
05/28/2019 18:42:20 - Tools Installation Completed
05/28/2019 18:42:31 - Adobe Installation Completed.
05/28/2019 18:42:31 - Installing Paint
05/28/2019 18:48:00 - MS OFFICE Completed
05/28/2019 18:48:06 - Font Completed
05/28/2019 18:48:06 - Installing Windows Master File
05/28/2019 18:48:06 - Notepad++ Completed
05/28/2019 18:48:06 - Notepad Settings
05/28/2019 18:48:15 - ABC Installation Completed
05/28/2019 18:48:21 - --------------------------------------------------
05/28/2019 18:48:21 - Install Complete.
05/28/2019 18:48:21 - --------------------------------------------------

As you can see, I have added a test now to check if the search string you are using can be found in the file or not. If it is nowhere to be found in the file, you will see a warning message:

WARNING: Search string '*Installation Location: C:\Temp\z*.exe*' not found in file 'D:\blah.txt'

and the script simply exits after that. This way, you can experiment more with creating a search string that works for you given the actual file.

Theo
  • 57,719
  • 8
  • 24
  • 41
  • If the $search is occurring multiple times in the file and if I need only the contents from the last occurrence of $search how should it be tackled? As I'm able to get all the occurrences of $search – Hidei May 28 '19 at 14:43
  • @Hidei I cannot reproduce that as both methods break the loop as soon as the match is found and both read the lines from tail to start. – Theo May 28 '19 at 14:54
  • is it possible to read line from tail to match when the loops breaks once match is found? – Hidei May 28 '19 at 14:57
  • @Hidei That is exactly what the code does! I have edited my answer to provide proof. – Theo May 28 '19 at 14:58
  • the code retrieves whole content once match is found. I am wondering why its happening as it should reproduce content from end till match. I will look into it again – Hidei May 28 '19 at 15:02
  • @Hidei You need to look at what is in variable `$result`, not in `$data` because the latter of course still holds all the lines. Is that what you are doing maybe? – Theo May 28 '19 at 15:05
  • ,there is a scenario where the match is occuring multiple times and the code is retrieving all the lines despite of using only the last occurrence . Its $result value variable which puts whole content – Hidei May 28 '19 at 15:20
  • @Hidei then that scenario differs from the example you gave us a lot, or most likely your search string is wrong and it never finds a match, so the whole content is collected in the `$result` variable.. You need to make sure your search string actually CAN be found. – Theo May 28 '19 at 15:38
  • @Hidei I don't know what else could be in your text file to work on, but perhaps make your search string less rigid. Something like `$search = '"Install location:'`. – Theo May 29 '19 at 09:37
  • I tried using $search = '"Install location:'. I still get -1 for $search in index value. Also to be noted before jumping to your solutions I was using $Copy = Get-Content -Path $Path | Out-String $Index = $Copy.LastIndexOf($search) $result = $Copy.Substring($Index) In the above code snippet I got error as Index cant be less than 0 and the same I got for your solution too – Hidei May 29 '19 at 09:52
  • The text file gets updated every time and we need to retrieve the latest appended contents and the text will be having multiple occurrences of $search.We need to retrieve the last occurrence of $search from the end. Is there any help? – Hidei May 29 '19 at 09:59
  • the code snippet should be made available in both win7 and win10 platforms – Hidei May 29 '19 at 10:02
  • @Hidei See my updated answer. If you still cannot get the desired result, I think it is time for you to give us an **real** example instead of the one in the question. Now, I can only guess what would be a good string to search for.. – Theo May 29 '19 at 10:03
  • I will share the text file. The solution you provided doesn't work – Hidei May 29 '19 at 10:12
  • I have updated the text file . Any solution for the problem? – Hidei May 29 '19 at 10:24
  • @Hidei OK, if this new text you have posted is the real deal, then see my updated answer – Theo May 29 '19 at 11:49
  • The script still lists all contents. Also as per your solution we get the content till Updated Install location but I need content from 06/16/2017 17:15:52 - Installation Location: C:\Temp\3456.exe. As the text file is very large, I had cut paste a bit of file, When I run the solution I am getting whole file.Is there any solution? – Hidei May 29 '19 at 13:16
  • I am unable to copy the whole file here in stack overflow to specify the solution – Hidei May 29 '19 at 13:19
  • @Hidei The only solution is for you to decide on a search string that can actually be found. All the examples you have given seem not to reflect the actual contents of your file. Given the examples, the code works as proven more than once. – Theo May 29 '19 at 13:49
  • I have finally updated the file along with the required description at the end . Can we retrieve the contents from the last occurrence of Installation Location: c:\Temp\3456.exe . As we have mutilple such occurrence, I dont need the whole content only the last appended part – Hidei May 29 '19 at 14:07
  • @Hidei It is clear to me what you want, but really hard to get right when the examples don't match the actual file. I hope this time what you posted is real. I have added new code, including a test to see if your search string can be found or not and if not, it will not collect any strings, simply emits a warning about it. – Theo May 29 '19 at 14:30
  • I was able to get desired result.Thanks for the help:) – Hidei May 30 '19 at 02:05