Solved If I wanted to grep through a file or multiple at once....
-
Is it possible to grep for a set number of characters around a regular expression and give me that? For example
"Server 2012 R2" so give me the previous 20 characters, Server 2012 R2, and the next 2012 characters?
-
@DustinB3403 said in If I wanted to grep through a file or multiple at once....:
Windows Server 2008 R2 | SVR12.localdomain
Something like
/\werver.+/g
would get you "Server 2008 R2 | SVR12.localdomain"
You'd have to do a negative lookahead in order to capture the prior input. the /g gives you global results so that it doesn't stop at the first match. -
Okay I think I have a process that should work.
grep -E "{0.30}Windows Server.{0.30}" server.txt | sort --unique
Now just to get the remainder of the lines/next lines if they are wrapped.
-
@DustinB3403 : Well that looks like fun
-
@manxam said in If I wanted to grep through a file or multiple at once....:
@DustinB3403 : Well that looks like fun
I assume you have a better approach? How would your approach look like, I just tested it and got no output.
-
@DustinB3403 : No, I definitely don't have a better approach, especially when you have to capture wrapped lines.
I'd just toss a sample of your input file into a regex tester online and build out your regex from there... -
@manxam said in If I wanted to grep through a file or multiple at once....:
@DustinB3403 : No, I definitely don't have a better approach, especially when you have to capture wrapped lines.
I'd just toss a sample of your input file into a regex tester online and build out your regex from there...Doh I thought you may have had an idea. No worries, I'll keep at it.
-
This is a small subset of the data I'm working with (anonymous) On for All programs and services except those I select OS Manufacturer: Microsoft Corporation OS Version: 5.2.3790 Service Pack 2 (Build 3790) OS Caption: Microsoft(R) Windows(R) Server 2003 Standard x64 Edition OS Virtual Memory: 2528 MB OS System Directory: C:\WINDOWS\system32 OS Windows Directory: C:\WINDOWS OS Install Date: 8/5/2008 12:49:17 PM DB-Server Windows Server 2012 R2 Standard Remote Listening Ports: RDP (3389/TCP)DB-Virtual Windows Server 2008 Standard
Now if I wanted to find "Windows Server 2012" and then the line above it, which is the server name. How in the heck would I do that?
Paging @scottalanmiller
-
This will output the Server detail, but doesn't jump to the prior or next lines.
grep -riE "((.*\Windows Server){5}}*Windows Server" source.txt
-
Just spitballing again, but what about grep with -A# and -B# and a regex of
.+?(Server.+)
A# stands for n lines "after" the match.
B# stands for m lines "before" the match.Using
grep -rni -E '.+?(Server.+)' -B1
should captureDB-Server Windows Server 2012 R2 Standard
-
@manxam said in If I wanted to grep through a file or multiple at once....:
Just spitballing again, but what about grep with -A# and -B# and a regex of
.+?(Server.+)
A# stands for n lines "after" the match.
B# stands for m lines "before" the match.Using
grep -rni -E '.+?(Server.+)' -B1
should captureDB-Server Windows Server 2012 R2 Standard
That I can at least work with, with relative ease. Still not perfect, but way better than the full details I was working with.
-
This is the final working regex that was used for anyone else who may ever need this.
grep -ri -E '.+?(Windows Server.+)' -B7 source.txt > regex.txt
From
@manxam said in If I wanted to grep through a file or multiple at once....:
Just spitballing again, but what about grep with -A# and -B# and a regex of
.+?(Server.+)
A# stands for n lines "after" the match.
B# stands for m lines "before" the match.Using
grep -rni -E '.+?(Server.+)' -B1
should captureDB-Server Windows Server 2012 R2 Standard
-
Thanks a ton @manxam!
-
@DustinB3403 : Team effort!