ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    If I wanted to grep through a file or multiple at once....

    Scheduled Pinned Locked Moved Solved IT Discussion
    grepunixdocx2txtregex
    19 Posts 2 Posters 1.6k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • DustinB3403D
      DustinB3403
      last edited by

      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?

      1 Reply Last reply Reply Quote 0
      • M
        manxam
        last edited by

        @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.

        1 Reply Last reply Reply Quote 0
        • DustinB3403D
          DustinB3403
          last edited by

          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.

          M 1 Reply Last reply Reply Quote 0
          • M
            manxam @DustinB3403
            last edited by

            @DustinB3403 : Well that looks like fun 🙂

            DustinB3403D 1 Reply Last reply Reply Quote 0
            • DustinB3403D
              DustinB3403 @manxam
              last edited by

              @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.

              M 1 Reply Last reply Reply Quote 0
              • M
                manxam @DustinB3403
                last edited by

                @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...

                DustinB3403D 1 Reply Last reply Reply Quote 0
                • DustinB3403D
                  DustinB3403 @manxam
                  last edited by

                  @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.

                  1 Reply Last reply Reply Quote 0
                  • DustinB3403D
                    DustinB3403
                    last edited by

                    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

                    1 Reply Last reply Reply Quote 0
                    • DustinB3403D
                      DustinB3403
                      last edited by

                      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

                      1 Reply Last reply Reply Quote 0
                      • M
                        manxam
                        last edited by manxam

                        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 capture

                        DB-Server
                        Windows Server 2012 R2 Standard
                        
                        DustinB3403D 1 Reply Last reply Reply Quote 1
                        • DustinB3403D
                          DustinB3403 @manxam
                          last edited by

                          @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 capture

                          DB-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.

                          1 Reply Last reply Reply Quote 0
                          • DustinB3403D
                            DustinB3403
                            last edited by DustinB3403

                            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 capture

                            DB-Server
                            Windows Server 2012 R2 Standard
                            
                            1 Reply Last reply Reply Quote 0
                            • DustinB3403D
                              DustinB3403
                              last edited by

                              Thanks a ton @manxam!

                              M 1 Reply Last reply Reply Quote 1
                              • M
                                manxam @DustinB3403
                                last edited by

                                @DustinB3403 : Team effort! 🙂

                                1 Reply Last reply Reply Quote 0
                                • 1 / 1
                                • First post
                                  Last post