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

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

    IT Discussion
    grep unix docx2txt regex
    2
    19
    1.5k
    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.
    • D
      DustinB3403
      last edited by DustinB3403

      Okay newby question, if I wanted to grep through a bunch of .txt files and find anything like .Windows Server. with the Period . being used for the special expression of "give me everything like this" why am I only getting a single record one time, when I know the record is in the source file at least several times.

      IE Windows Server 2008 R2 - SVR12.localdomain

      I'm trying to get unique records ideally and at the moment just want to consolidate this all into a quick list more or less.

      What is happening is I'm not getting every record of at least 1 system that I know is in the source file.

      grep -rnIi -e '.Server.' servers.txt --exclude=out.txt > out.txt

      Looking for some guidance.

      1 Reply Last reply Reply Quote 0
      • D
        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
        • D
          DustinB3403
          last edited by

          Sample output I'm trying to find would be

          Windows Server 2008 R2 | SVR12.localdomain

          If that helps, I know it's multiple expressions, but the formatting isn't working (I'm sure I broke it).

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

            Wouldn't the following get every line containing a variation of "server" within this file?
            grep -rin server server.txt > out.txt

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

              @manxam while that would, it would also get anything with the word Server listed, I need to find anything with the word Server and then some server-name in it, ideally piping only those details to a output file.

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

                I'm trying to break out a datadump essentially into unique records pertaining to "Servers" only and I'm not sure if I can get a generic enough regular expression to do just that.

                1 Reply Last reply Reply Quote 0
                • D
                  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
                    • D
                      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 🙂

                        D 1 Reply Last reply Reply Quote 0
                        • D
                          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...

                            D 1 Reply Last reply Reply Quote 0
                            • D
                              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
                              • D
                                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
                                • D
                                  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
                                    
                                    D 1 Reply Last reply Reply Quote 1
                                    • D
                                      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
                                      • D
                                        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
                                        • D
                                          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