• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Powershell command

gouda96

Senior member
Trying to do something in powershell that I just accomplished in linux and trying to see if it is easily done.

I have a page of text and I am trying to pull out two things. Lets just say my script looks like this:

This is my email
This is Test's email
my email is 324@fdaf.com This is my email
that is my email

OK, so I want to txt a big document that looks exactly like this. I want to pull JUST the email out of it. Not the entire line with the email but JUST the email. I also want to get the line before it.

With Linux i did a cut to erase the stuff before and after the email and I grep'd for *@* basically. To get the line before it I used a grep -A1 and it worked. Anyone know if I can do this same thing with powershell and output it to a log file? Thanks for any help!!!
 
I would tackle via regular expressions, though I'm not an expert. And I'm not certain what you are looking for..

Here is a simple function that takes a part of the data and searches for email addresses using the .NET framework's RegEx API.

Code:
function testRegex(){
	$data = "my email is 324@fdaf.com This is my email"
	$pattern = "[A-Za-z0-9\.-_]+@[A-Za-z0-9]+\.[A-Za-z0-9]{2,4}"
	
	foreach($match in [System.Text.RegularExpressions.Regex]::Matches($data, $pattern, [System.Text.RegularExpressions.RegexOptions]::Multiline)){
		if($match.Success -eq $true) {
			write-host $match.Value
		}
	}
}

While on the subject of PS, this is by far my most favorite command, stored in $profile:

Code:
$np = "C:\Program Files (x86)\Notepad++\Notepad++.exe"
function np() {
	if($Args){
		start-process $np -ArgumentList $Args
	}
	else {
		start-process $np
	}
}

Use:

Code:
jsedlak > np $profile
 
Last edited:
You could try this - it is a Windows command (formerly DOS batch) file that will find words containing an @ in a text file.

It has a number of deficiencies and limitations, in particular it only checks for a word containing an @, this is not necessarily an e-mail address for example xyz123@test.

Tested with your example and a few others under XP SP3.

Use at your discretion, no support or documentation is provided and no warranty or liability is expressed or implied.

In the fourth line (for command) note that the findstr command in parentheses is contained in back quote characters. It is a bit of an unusual syntax, see for /? for further details.

Snapshot1

HTML:
@echo off
setlocal
if "%1"=="" goto :Usage
for /f "usebackq tokens=1*" %%a in (`findstr "@" %1`) do call :FindAtWord %%a %%b
goto :EOF
:FindAtWord
if "%1"=="" goto :EOF
set word=%1
set noat=%word:@=%
if %word%==%noat% goto :NextWord
echo %word%
:NextWord
shift
goto :FindAtWord
:Usage
echo atwordscan filename - finds words containing an @ (e-mail addresses) in text file
ETA: I missed the requirement to capture the preceding line in addition to the word with the @. You will need something else to get those lines.
 
Last edited:
This should be closer to what you asked for, it prints the preceding line and the word containing the @ sign.

Use at your discretion, no support or documentation is provided and no warranty or liability is expressed or implied.

Snapshot1

Code:
@echo off
setlocal enabledelayedexpansion
if "%1"=="" goto :Usage
set lastline=.
for /f "tokens=1*" %%a in (%1) do (call :FindAtWord %%a %%b & set lastline=!thisline:~1!)
goto :EOF
:FindAtWord
set thisline=
:CheckWord
if "%1"=="" goto :EOF
set word=%1
set thisline=!thisline! !word!
set noat=%word:@=#%
if %word%==%noat% goto :NextWord
echo %lastline% "%word%"
:NextWord
shift
goto :CheckWord
:Usage
echo emailscan filename - finds words containing an @ (e-mail addresses) in text file, prints the preceding line and the word containing the @ sign
 
Last edited:
Back
Top