Powershell command

gouda96

Senior member
Jun 15, 2003
835
0
76
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!!!
 

jsedlak

Senior member
Mar 2, 2008
278
0
71
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:

Snapshot1

Member
Dec 26, 2011
42
0
0
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:

Snapshot1

Member
Dec 26, 2011
42
0
0
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:
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |