Select-String with Context

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
#1
I have a file, astronomy.txt, which is generated by doing;
Code:
lynx -dump -nolist "https://www.timeanddate.com/astronomy/canada/london" > c:\utils\astronomy.txt
The goal is to extract the following from astronomy.txt;
Code:
Sun in London - Next 7 Days

     Scroll right to see more
      2018  Sunrise/Sunset      Daylength            Solar Noon
      Jan   Sunrise Sunset  Length  Difference       Time       Mil. km
     Jan 14 7:53 am 5:14 pm 9:20:53 +1:36      12:34 pm (25.8°) 147.153
     Jan 15 7:53 am 5:15 pm 9:22:33 +1:40      12:34 pm (26.0°) 147.162
     Jan 16 7:52 am 5:17 pm 9:24:15 +1:42      12:34 pm (26.2°) 147.172
     Jan 17 7:52 am 5:18 pm 9:26:01 +1:46      12:35 pm (26.4°) 147.182
     Jan 18 7:51 am 5:19 pm 9:27:50 +1:49      12:35 pm (26.6°) 147.193
     Jan 19 7:51 am 5:20 pm 9:29:42 +1:52      12:35 pm (26.8°) 147.205
     Jan 20 7:50 am 5:22 pm 9:31:36 +1:54      12:36 pm (27.0°) 147.216
This is simple to do in PowerShell;
Code:
powershell -command "$f = type c:\utils\astronomy.txt | Select-String -Pattern 'Sun in London - Next 7 Days' -context 0,11; $f"
This is how to call it from TCC;
Code:
pshell /s "type c:\utils\astronomy.txt | Select-String -Pattern 'Sun in London - Next 7 Days' -context 0,11"
Joe

Ref: https://docs.microsoft.com/en-us/po...ell.utility/select-string?view=powershell-5.1
Ref: https://en.m.wikipedia.org/wiki/Lynx_(web_browser)
 

samintz

Scott Mintz
May 20, 2008
1,229
11
Solon, OH, USA
#2
The PSHELL command doesn't return anything for me.
Code:
$ pshell /s "type foo.txt | Select-String -Pattern 'Sun in London - Next 7 Days' -context 0,11"

$ powershell -command "$f = type foo.txt | Select-String -Pattern 'Sun in London - Next 7 Days' -context 0,11; $f"

> Sun in London - Next 7 Days

       Scroll right to see more
        2018  Sunrise/Sunset      Daylength            Solar Noon
        Jan   Sunrise Sunset  Length  Difference       Time       Mil. km
       Jan 14 7:53 am 5:14 pm 9:20:53 +1:36      12:34 pm (25.8°) 147.153
       Jan 15 7:53 am 5:15 pm 9:22:33 +1:40      12:34 pm (26.0°) 147.162
       Jan 16 7:52 am 5:17 pm 9:24:15 +1:42      12:34 pm (26.2°) 147.172
       Jan 17 7:52 am 5:18 pm 9:26:01 +1:46      12:35 pm (26.4°) 147.182
       Jan 18 7:51 am 5:19 pm 9:27:50 +1:49      12:35 pm (26.6°) 147.193
       Jan 19 7:51 am 5:20 pm 9:29:42 +1:52      12:35 pm (26.8°) 147.205
       Jan 20 7:50 am 5:22 pm 9:31:36 +1:54      12:36 pm (27.0°) 147.216
 

samintz

Scott Mintz
May 20, 2008
1,229
11
Solon, OH, USA
#3
Even though I am in the same directory as foo.txt, it turns out I need to specify the full path to foo.txt in order for the PSHELL version to work.
Code:
$ pshell /s "type c:\temp\foo.txt | Select-String -Pattern 'Sun in London - Next 7 Days' -context 0,11"

> Sun in London - Next 7 Days

       Scroll right to see more...
 
#4
Even though I am in the same directory as foo.txt, it turns out I need to specify the full path to foo.txt in order for the PSHELL version to work.
Code:
$ pshell /s "type c:\temp\foo.txt | Select-String -Pattern 'Sun in London - Next 7 Days' -context 0,11"

> Sun in London - Next 7 Days

       Scroll right to see more...
One of several differences that I have found between PSHELL and powershell.exe

No error returned, or indication that the file was not found. However;
Code:
c:\users\jlc\utils>powershell -command "$f = type foo.txt | Select-String -Pattern 'Sun in London - Next 7 Days' -context 0,11; $f"
type : Cannot find path 'C:\Users\jlc\utils\foo.txt' because it does not exist.
At line:1 char:6
+ $f = type foo.txt | Select-String -Pattern 'Sun in London - Next 7 Da ...
+      ~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\jlc\utils\foo.txt:String) [Get-Content], ItemNotFoundExcepti
   on
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
...returns an indication of what went wrong.

Joe
 
#5
It would appear that Write-Host still does not work with PSHELL;

Code:
c:\users\jlc\utils>powershell -command "$thefile = 'c:\utils\astronomy.txt'; if(Test-Path $thefile) {type $thefile | Select-String -Pattern 'Sun in London - Next 7 Days' -context 0,11} else {Write-Host 'File does not exist'}"

> Sun in London - Next 7 Days

     Scroll right to see more
      2018  Sunrise/Sunset      Daylength            Solar Noon
      Jan   Sunrise Sunset  Length  Difference       Time       Mil. km
     Jan 15 7:53 am 5:15 pm 9:22:33 +1:40      12:34 pm (26.0°) 147.162
     Jan 16 7:52 am 5:17 pm 9:24:15 +1:42      12:34 pm (26.2°) 147.172
     Jan 17 7:52 am 5:18 pm 9:26:01 +1:46      12:35 pm (26.4°) 147.182
     Jan 18 7:51 am 5:19 pm 9:27:50 +1:49      12:35 pm (26.6°) 147.193
     Jan 19 7:51 am 5:20 pm 9:29:42 +1:52      12:35 pm (26.8°) 147.205
     Jan 20 7:50 am 5:22 pm 9:31:36 +1:54      12:36 pm (27.0°) 147.216
     Jan 21 7:49 am 5:23 pm 9:33:34 +1:58      12:36 pm (27.2°) 147.229
Code:
c:\users\jlc\utils>powershell -command "$thefile = 'c:\utils\astronoma.txt'; if(Test-Path $thefile) {type $thefile | Select-String -Pattern 'Sun in London - Next 7 Days' -context 0,11} else {Write-Host 'File does not exist'}"
File does not exist
Code:
c:\users\jlc\utils>pshell /s "$thefile = 'c:\utils\astronoma.txt'; if(Test-Path $thefile) {type $thefile | Select-String -Pattern 'Sun in London - Next 7 Days' -context 0,11} else {Write-Host 'File does not exist'}"
Joe
 
#7
Setting the file name and location from TCC;
Code:
  set thefile=%@truename[astronomy.txt]
  pshell /s "if(Test-Path %thefile) {type %thefile | Select-String -Pattern 'Sun in London - Next 7 Days' -context 0,11} else {Write-Output 'File does not exist'}"
Now, it should not be a problem using PSHELL from the same location as the file.

Joe