batch for grep in some other text for input

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mor.bas
Posts: 66
Joined: 25 Apr 2012 04:28

batch for grep in some other text for input

#1 Post by mor.bas » 11 Jul 2013 03:22

Hi,


I need a batch that use findstr or something like that that take a txt for input that have a line like:



Account name for installing services & interfaces - E_X\try



the script speared the domain and the user (speared by \)



and editing my line in script :



User =<username>(in this case try)



Domain=<domain>(in this case E_X)


Thanks in advance...

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: batch for grep in some other text for input

#2 Post by foxidrive » 11 Jul 2013 08:05

Where you say 'speared' I think you mean 'separated'.

This works here. Input.txt has the data.

Code: Select all

@echo off
for /f "delims=" %%a in ('type "input.txt" ^| find "Account name for installing services"') do set "var=%%a"
set "var=%var:* - =%"
for /f "tokens=1,* delims=\" %%a in ("%var%") do set "user=%%b"&set "domain=%%a"

echo "user=%user%" "domain=%domain%"
pause

mor.bas
Posts: 66
Joined: 25 Apr 2012 04:28

Re: batch for grep in some other text for input

#3 Post by mor.bas » 11 Jul 2013 08:15

Hi,thanks

Can you explain the line:

set "var=%var:* - =%"

?

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: batch for grep in some other text for input

#4 Post by Squashman » 11 Jul 2013 08:43

mor.bas wrote:Hi,thanks

Can you explain the line:

set "var=%var:* - =%"

?

Removes everything before the hyphen.
If you turned ECHO ON and watch the code execute you can see what it is doing. Seeing is believing.

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: batch for grep in some other text for input

#5 Post by Aacini » 11 Jul 2013 14:23

You may also try my FindRepl.bat program this way:

Code: Select all

@echo off
for /F "tokens=1,2" %%a in ('^< test.txt FindRepl "Account name for installing services & interfaces - (\w+)\\(\w+)" /$:1:2') do (
   set "domain=%%~a"
   set "user=%%~b"
)
echo "domain=%domain%" "user=%user%"
pause

That is, you are looking for "Account name for installing services & interfaces - " text, followed by any string of alphanumeric characters and underscore (represented by \w) repeated one or more times (the +), a backslash (put it twice) and another similar value. The parentheses enclose the values you want to extract, that are displayed via /$:1:2 switch. Easy! Isn't it?

Antonio

Post Reply