Find and Replace in Batch File

I had this question answered earlier, -- which basically removes everything before the @sign in an email address (including the @sign) now instead of simply removing the text before the @ sign I need something more complicated. I need is to make a line like this
"queued message from onlinedoctorate@tikritnews.net was deleted."
into a line like this
"from .*\@tikritnews.\net"
and yes that is a dot before the * (.*\) and a dot after the word deleted (deleted.). I would prefer a batch file but if vb or ps is best then OK--thank you.
LVL 26
Lionel MMSmall Business IT ConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Try this:
@echo off
setlocal
set Line=queued message from onlinedoctorate@tikritnews.net was deleted.
REM *** Remove everything before the "@":
for /f "tokens=1* delims=@" %%a in ("%Line%") do set From=%%b
REM *** Now that the leading spaces have been removed, remove everything after the email address:
for /f "tokens=1" %%a in ("%From%") do set From=%%a
REM *** Now add the ".*\@" and escape the dots in the domain part of the address:
set From=.*\@%From:.=.\%
echo From: '%From%'

Open in new window

0
Lionel MMSmall Business IT ConsultantAuthor Commented:
There is a text file with a whole bunch of lines similar to one example I gave so it needs to go through the text file and do this to each line.
0
oBdACommented:
And the (only) lines you're looking for all have the "queued message from ..." in them?
And I assume you want to do something with each line, other than echoing them to the console?
0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

oBdACommented:
Try this; add your code at line 20:
@echo off
setlocal enabledelayedexpansion
set File=D:\Temp\test.txt
set Search=queued message from
for /f "tokens=1* delims=@" %%a in ('type "%File%" ^| find.exe /i "%Search%"') do (
	REM *** Everything before the "@" is removed in %%b:
	set From=%%b
	REM *** Now that the leading spaces have been removed, remove everything after the email address:
	for /f "tokens=1" %%f in ("!From!") do set From=%%f
	REM *** Now add the ".*@" and escape the dots:
	set From=.*\@!From:.=.\!
	call :Process "!From!"
)
echo Done.
goto :eof

:Process
set From=%~1
echo Processing '%From%' ...
REM ...
goto :eof

Open in new window

0
Lionel MMSmall Business IT ConsultantAuthor Commented:
ok it seems to be working but I only get this echoed to the screen and not a file
Processing '.*\@tikritnews.\net' ...
Processing '.*\@ma-tesol.\com' ...
Processing '.*\@shaggybike.\com' ...

PLUS
The results needs to be "from .*\@" at the start and not just ".*\@" so would you change line 11 to
set from= from .*\@!from:.=.\!
?

And in answer to your earlier q's yes all these lines are similar and these are the only types of lines in this file
0
oBdACommented:
So you want the output in a file (you didn't mention that before)?
@echo off
setlocal enabledelayedexpansion
set InFile=D:\Temp\test.txt
set OutFile=D:\Temp\testout.txt
set Search=queued message from
if exist "%OutFile%" del "%OutFile%"
echo Processing '%InFile%' ...
for /f "tokens=1* delims=@" %%a in ('type "%InFile%" ^| find.exe /i "%Search%"') do (
	REM *** verything before the "@" is removed in %%b:
	set From=%%b
	REM *** Now that the leading spaces have been removed, remove everything after the email address:
	for /f "tokens=1" %%f in ("!From!") do set From=%%f
	REM *** Now add the ".*@" and escape the dots:
	set From=from .*\@!From:.=.\!
	>>"%OutFile%" echo !From!
)
echo Done.

Open in new window

0
Lionel MMSmall Business IT ConsultantAuthor Commented:
I did not sorry but I assumed you would have looked at my earlier question where I did have the file sorted and deduped from one file and created another -- my apologies I should have re-stated it again
0
Lionel MMSmall Business IT ConsultantAuthor Commented:
Can you help me dedupe the results--I thought i could use my earlier solution to do it on my own but can't. I need to remove the duplicates in the resulting file, can't do it at the start because they may not be exact duplicates until after the text has been removed. Thank you.
0
oBdACommented:
@echo off
setlocal enabledelayedexpansion
set InFile=D:\Temp\test2.txt
set OutFile=D:\Temp\testout.txt
set Search=queued message from

set TempFile=%Temp%\%~n0.tmp
if exist "%OutFile%" del "%OutFile%"
if exist "%TempFile%" del "%TempFile%"
echo Processing '%InFile%' ...
for /f "tokens=1* delims=@" %%a in ('type "%InFile%" ^| find.exe /i "%Search%"') do (
	REM *** Everything before the "@" is removed in %%b:
	set From=%%b
	REM *** Now that the leading spaces have been removed, remove everything after the email address:
	for /f "tokens=1" %%f in ("!From!") do set From=%%f
	REM *** Now add the "from .*@" and escape the dots:
	set From=from .*\@!From:.=.\!
	REM *** Write the output to a temp file
	>>"%TempFile%" echo !From!
)
REM Remove duplicates from the list in the temp file, and create the output file
set Line=
for /f "delims=" %%a in ('type "%TempFile%" ^| sort.exe') do (
	if not "!Line!"=="%%a" (
		>>"%OutFile%" echo %%a
		set Line=%%a
	)
)
del "%TempFile%"
echo Done; results are in '%OutFile%'.

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Lionel MMSmall Business IT ConsultantAuthor Commented:
That works perfectly--thank you. I am trying to better understand how all these commands work. What each line is doing, well not what but how so that I can learn and apply to other situations so do you think you could explain how each command does what it does?
0
oBdACommented:
The main part is done by "for /f" loops. A "for /f" loop splits lines from an input stream into single tokens based on the delimiters passed.
In line 10, the beginning of the main loop that reads the input file, each line read is split at the first "@" character ("delims=@"), and the resulting two tokens ("tokens=1*") will be found in %%a (explicitly defined) and %%b (implicitly defined because "tokens=1*" expects two tokens). So %%b now contains everything after the "@".
In line 15, it's basically the same, only it's now splitting at space (and tab), the default if "delims=" is not defined, and this time the only interesting token is the first one. So "From" now contains the domain part of the email address.
Line 17 adds the new prefix and replaces all "." in "From" with "\.".
To remove the duplicates, a new "for /f" loop reads the sorted results of the extracted addresses (here the full line is read because no delims are defined: "delims="), and will only write the current line to the output file if it hasn't been already written in a previous run.
Which brings me to a minor correction: there should be a "/i" after "if" in line 24 to make a case insensitive comparison (so that tikritnews would be considered the same as TikritNews).
@echo off
setlocal enabledelayedexpansion
set InFile=D:\Temp\test2.txt
set OutFile=D:\Temp\testout.txt
set Search=queued message from

set TempFile=%Temp%\%~n0.tmp
if exist "%OutFile%" del "%OutFile%"
if exist "%TempFile%" del "%TempFile%"
echo Processing '%InFile%' ...
for /f "tokens=1* delims=@" %%a in ('type "%InFile%" ^| find.exe /i "%Search%"') do (
	REM *** Everything before the "@" is removed in %%b:
	set From=%%b
	REM *** Now that the leading spaces have been removed, remove everything after the email address:
	for /f "tokens=1" %%f in ("!From!") do set From=%%f
	REM *** Now add the "from .*@" and escape the dots:
	set From=from .*\@!From:.=.\!
	REM *** Write the output to a temp file
	>>"%TempFile%" echo !From!
)
REM Remove duplicates from the list in the temp file, and create the output file
set Line=
for /f "delims=" %%a in ('type "%TempFile%" ^| sort.exe') do (
	if /i not "!Line!"=="%%a" (
		>>"%OutFile%" echo %%a
		set Line=%%a
	)
)
del "%TempFile%"
echo Done; results are in '%OutFile%'.

Open in new window

0
Lionel MMSmall Business IT ConsultantAuthor Commented:
Works great. Thanks so much for all your help, time and education. I really appreciate it. Have a great day
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.