Findstr output format

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ladduq
Posts: 31
Joined: 22 Jan 2012 01:08

Findstr output format

#1 Post by ladduq » 06 Feb 2012 23:09

I'm new to batch programming please help me

I want to search some keywords from hi.txt file

hi.txt file has 2 keywords like

hello
raju

when I use this command findstr /g:c:\\hi.txt *.* output is in below format

Copy (3) of New Text Document.txt:helloCopy (4) of New Text Document.txt:rajuss
txt:rajuFINDSTR: Cannot open \r\n


BUT I want output of FIVE lines for each string like

--------START------------- 1
Search string is:hello 2
File path is :filepath\filename 3
Line number is :line number 4
--------END------------- 5

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

Re: Findstr output format

#2 Post by foxidrive » 10 Feb 2012 06:25

Code: Select all

@echo off
for /f "tokens=1,2 delims=:" %%a in ('findstr /g:c:\hi.txt *.*') do (
for /f "tokens=1 delims=:" %%c in ('findstr /n /c:"%%b" "%%a"') do (
echo --------START-------------
echo Search string is:%%b
echo File path is :%%~dpa%%a
echo Line number is :line number %%c
echo --------END-------------
)
)
pause

ladduq
Posts: 31
Joined: 22 Jan 2012 01:08

Re: Findstr output format

#3 Post by ladduq » 20 Feb 2012 06:18

@foxidrive

Thank you for your reply.

But it worked only for once and stopped working from second time.

I dont know why. The script written by you takes input string from hi.txt and searches for that string in all the files in a specific folder and prints it in the specified output format.

But it worked only Once :(

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

Re: Findstr output format

#4 Post by foxidrive » 20 Feb 2012 06:28

Try this with debugging steps in. It will echo two lines and a pause to the screen for every file that matches. If it doesn't echo to the screen then there are no files in the current folder that match the search terms.

Code: Select all

@echo off
for /f "tokens=1,2 delims=:" %%a in ('findstr /g:c:\hi.txt *.*') do (
echo. "%%b"
echo. "%%a"
pause
for /f "tokens=1 delims=:" %%c in ('findstr /n /c:"%%b" "%%a"') do (
echo --------START-------------
echo Search string is:%%b
echo File path is :%%~dpa%%a
echo Line number is :line number %%c
echo --------END-------------
)
)
pause

ladduq
Posts: 31
Joined: 22 Jan 2012 01:08

Re: Findstr output format

#5 Post by ladduq » 21 Feb 2012 04:36

@foxidrive

Thank you for replying.

checked with it and its not working

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

Re: Findstr output format

#6 Post by foxidrive » 21 Feb 2012 07:04

Change the search terms. try a simple search for 'a' without the quotes.

ladduq
Posts: 31
Joined: 22 Jan 2012 01:08

Re: Findstr output format

#7 Post by ladduq » 22 Feb 2012 09:27

@foxidrive

Thank you for replying :)

but for some reason the script is not working.

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

Re: Findstr output format

#8 Post by foxidrive » 22 Feb 2012 09:32

Try this batch file. backup c:\hi.txt first.

Is the problem that the search string which was matched is not printed, but what is printed is the matched text?
I've changed this to show the search string but it will take longer, depending on the number of files.



Code: Select all

@echo off

echo a>c:\hi.txt

(
echo abc zzz
echo def
echo ghi
)>test1.txt

(
echo 123
echo def
echo ghi
)>test2.txt


(
echo 123
echo aaa bbb
echo ghi
)>test3.txt


for /f "delims=" %%z in ('type "c:\hi.txt"') do (
for /f "tokens=1,2,* delims=:" %%a in ('findstr /n /c:"%%z" *.*') do (
echo --------START-----------------
echo Search string is:               %%z
echo Search string which is matched: %%c
echo File path is :                  %%~fa
echo Line number is :                %%b
echo --------END-------------------
))
pause

Post Reply