2

I'm looking for a way to export all lines from within a text file where part of the line matches a certain string. The string is actually the first 4 bytes of the file and I'd like to keep the command to only checking those bytes; not the entire row. I want to write the entire row. How would I go about this?

I am using Windows only and don't have the option to use many other tools that might do this.

Thanks in advance for any help.

1
  • 1
    what have you tried ? Is a console application also applicable in your case ? Commented Feb 4, 2014 at 10:37

3 Answers 3

6

Do you want to perform a simple "grep"? Then try this

select-string .\test.txt -pattern "\Athat" | foreach {$_.Line} 

or this (very similar regex), also writes to an outfile

select-string .\test.txt -pattern "^that" | foreach {$_.Line} | out-file -filepath out.txt 

This assumes that you want to search for a 4-byte string "that" at the beginning of the string , or beginning of the line, respectively.

Sign up to request clarification or add additional context in comments.

2 Comments

That second one liner works great. I've just started using Powershell and it's proving really handy. Is there a way to do this while specifying a value elsewhere on the row? E.g. byte 150 to 152 = 127. Please let me know if this should be asked separately.
There might be a better way, but you could try something like: select-string .\test.txt -pattern "^.{149}$([char]127){3}" | foreach {$_.Line} | out-file -filepath out.txt That'll skip the first 149 characters, and then looks for 3 char 127s.
1

Something like the following Powershell function should work for you:

function Get-Lines { [cmdletbinding()] param( [string]$filename, [string]$prefix ) if( Test-Path -Path $filename -PathType Leaf -ErrorAction SilentlyContinue ) { # filename exists, and is a file $lines = Get-Content $filename foreach ( $line in $lines ) { if ( $line -like "$prefix*" ) { $line } } } } 

To use it, assuming you save it as get-lines.ps1, you would load the function into memory with:

. .\get-lines.ps1 

and then to use it, you could search for all lines starting with "DATA" with something like:

get-lines -filename C:\Files\Datafile\testfile.dat -prefix "DATA" 

If you need to save it to another file for viewing later, you could do something like:

get-lines -filename C:\Files\Datafile\testfile.dat -prefix "DATA" | out-file -FilePath results.txt 

Or, if I were more awake, you could ignore the script above, use a simpler solution such as the following one-liner:

get-content -path C:\Files\Datafile\testfile.dat | select-string -Pattern "^DATA" 

Which just uses the ^ regex character to make sure it's only looking for "DATA" at the beginning of each line.

Comments

0

To get all the lines from c:\somedir\somefile.txt that begin with 'abcd' :

(get-content c:\somedir\somefile.txt) -like 'abcd*' 

provided c:\somedir\somefile.txt is not an unusually large (hundreds of MB) file. For that situation:

get-content c:\somedir\somefile.txt -readcount 1000 | foreach {$_ -like 'abcd*'} 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.