Computing.Net > Forums > Programming > Find a line number then copy that line

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Find a line number then copy that line

Reply to Message Icon

Name: pegzyisdeed
Date: September 16, 2009 at 23:28:46 Pacific
OS: Windows XP
CPU/Ram: N/A
Product: Microsoft Windows xp professional w/service pack 3
Subcategory: Batch
Comment:

I want to create a batch file which will essentially find a string in a text file, via Findstr for example and also print the line number where the result occurs. Then from the line number result, copy the previous 2 lines of text.

Findstr /N 35=order24 log01.txt

*** Then with the result of the line number I want to copy the 2 line before.

For example say the result I get is

[1001] lofile sfe 37=order24

I want to copy lines 999, 1000, and 1001.

Is this possible? I hope I've given enough information. I'm using XP.



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: September 17, 2009 at 00:39:15 Pacific
Reply:

@echo off > newfile & setLocal enableDELAYedexpansion

set N=

for /f "tokens=1 delims=[]" %%a in ('find /n "35=order24" ^< log01.txt') do (
set F=%%a
)
set /a P=!F!-1
set /a PP=!F!-2

for /f "tokens=1* delims=[]" %%a in ('find /n /v "" ^< log01.txt') do (
set /a N+=1
if !P! equ !N! >> newfile echo %%b
if !PP! equ !N! >> newfile echo %%b
)


=====================================
Helping others achieve escape felicity

M2


0

Response Number 2
Name: klint
Date: September 17, 2009 at 03:26:08 Pacific
Reply:

Alternatively, use GNU grep for Win32 and do it in one line of code:

type log01.txt | grep -B 2 "35=order24"


0

Response Number 3
Name: ghostdog
Date: September 17, 2009 at 05:02:51 Pacific
Reply:

>> type log01.txt | grep -B 2 "35=order24"

No need "type". grep takes in an argument, a file.

grep -B 2 "35=order24" log01.txt

GNU win32 packages | Gawk


1

Response Number 4
Name: klint
Date: September 17, 2009 at 05:33:52 Pacific
Reply:

@ghostdog: you are correct, but when I tried it, my version of grep appears to have a bug and it doesn't print the matched lines except if you pipe the input into it. It doesn't work with redirection either. grep -V says GNU grep 2.5.4, but it's probably not this version itself but the port, or the run-time library, that is to blame. I don't remember where I got this from, it could be gnuwin32, mingw, cygwin, or something else. I wish GNU utilities would tell you which port, or which compiler and compilation options, they were built with. All they say is their version number, which is not very helpful sometimes.


0

Response Number 5
Name: ghostdog
Date: September 17, 2009 at 06:18:26 Pacific
Reply:

well, it shouldn't be because i am using GNU grep 2.5.4 as well... and -B works correctly for me. Download grep from GNU .

GNU win32 packages | Gawk


0

Related Posts

See More



Response Number 6
Name: klint
Date: September 17, 2009 at 08:09:19 Pacific
Reply:

Aha! Found out the problem. In fact, I was using exactly the version you suggested I download. The problem was that the file I was using as a test, happened to be in Unicode. D'oh!

The cmd.exe TYPE command converts Unicode files to the 8-bit code page you're currently using, which is why grep worked when I piped the output of TYPE to it.


0

Response Number 7
Name: Judago
Date: September 17, 2009 at 13:47:45 Pacific
Reply:

"The cmd.exe TYPE command converts Unicode files to the 8-bit code page you're currently using, which is why grep worked when I piped the output of TYPE to it."

There is one very notable exception to this, unicode files created "cmd /u" on Xp(maybe others as well) don't have the two byte unicode header so type interprets them as text.

cmd /u /c "dir > sometextfile.txt"
type sometextfile.txt


0

Response Number 8
Name: Mechanix2Go
Date: September 17, 2009 at 23:22:18 Pacific
Reply:

Aha

Good detective work, k & J.

Also a cool name for a band. LOL


=====================================
Helping others achieve escape felicity

M2


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Find a line number then copy that line

Finding a line in text file www.computing.net/answers/programming/finding-a-line-in-text-file/19390.html

Find a matching line and extract till eof www.computing.net/answers/programming/find-a-matching-line-and-extract-till-eof-/20158.html

sum of first and last numbers c++ www.computing.net/answers/programming/sum-of-first-and-last-numbers-c/13997.html