Thank you to the guys at HEGE supporting Badcaps [ HEGE ] [ HEGE DEX Chart ]

Announcement

Collapse
No announcement yet.

Search for specific strings and display only those strings in Linux

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Search for specific strings and display only those strings in Linux

    Hi! I have a string like this:
    Code:
    192.168.2.2 192.168.2.3 192.168.1.122 192.168.2.4 192.168.2.5 192.168.2.6
    I want to be able to search that string and only display the 192.168.2.* entries. How can I accomplish this in Linux? I will not know ahead of time how many entries there are or what order they go in. In my example, 192.168.1.122 is the third string, but it might not always be.

    I have tried, unsuccessfully:
    Code:
    cat fake_ips | grep "192.168.2."
    This shows the entire line, including the 192.168.1.122. I need the line minus the 192.168.1.122

    Code:
    cat fake_ips | awk '/192.168.2./'
    This has the same effect as the grep command.

    Code:
    cat fake_ips | awk '/192.168.2./ {print $1}'
    This is close, but obviously only prints the first string found. Looping through the variables doesn't help either, because at least one of them will contain the strings I do not want (192.168.1.122 in my example).

    I am sure there is a way to accomplish this, but I am having trouble finding it.
    -- Law of Expanding Memory: Applications Will Also Expand Until RAM Is Full

    #2
    Re: Search for specific strings and display only those strings in Linux

    Ctrl f
    sorry dont have a clue ...
    Last edited by petehall347; 10-17-2018, 05:21 PM.

    Comment


      #3
      Re: Search for specific strings and display only those strings in Linux

      out of interest what is the project ?

      Comment


        #4
        Re: Search for specific strings and display only those strings in Linux

        Code:
        cat fake_ips | grep -o "192\.168\.2..."
        comes really close, but it displays each string on a new line, whereas I need them all returned on one line.

        I am writing a script to automate editing a config file for two...so ctrl-f wouldn't really work here. Thanks though!
        -- Law of Expanding Memory: Applications Will Also Expand Until RAM Is Full

        Comment


          #5
          Re: Search for specific strings and display only those strings in Linux

          Originally posted by petehall347 View Post
          out of interest what is the project ?
          I customize my Linux machines in a certain way. I have various aliases setup, I use nano so I have a .nanorc file setup, I run an ssh server, but I only listen on the local area network and on a non-standard port, I have various icons setup on my Gnome desktop, various programs that will always be installed, etc...

          I have created a bash script that automates all of this, minus the sshd config file, because the IP addresses change.

          So, I am trying to obtain the IP addresses of the various systems in the script at run time, have the bash script search the sshd_config file, and replace the default ListenAddress with one that only has the private IP addresses.
          -- Law of Expanding Memory: Applications Will Also Expand Until RAM Is Full

          Comment


            #6
            Re: Search for specific strings and display only those strings in Linux

            i know nothing about scripting . i stopped at programming amstrad shit in the 1980.s ...
            i didnt know if ctrl f could be invoked within the script . was only joking anyway .

            Comment


              #7
              Re: Search for specific strings and display only those strings in Linux

              Try pipping it through "strings" first.

              Learn regex, it helps on every platform.
              Things I've fixed: anything from semis to crappy Chinese $2 radios, and now an IoT Dildo....

              "Dude, this is Wyoming, i hopped on and sent 'er. No fucking around." -- Me

              Excuse me while i do something dangerous


              You must have a sad, sad boring life if you hate on people harmlessly enjoying life with an animal costume.

              Sometimes you need to break shit to fix it.... Thats why my lawnmower doesn't have a deadman switch or engine brake anymore

              Follow the white rabbit.

              Comment


                #8
                Re: Search for specific strings and display only those strings in Linux

                Originally posted by Spork Schivago View Post
                I customize my Linux machines in a certain way. I have various aliases setup, I use nano so I have a .nanorc file setup, I run an ssh server, but I only listen on the local area network and on a non-standard port, I have various icons setup on my Gnome desktop, various programs that will always be installed, etc...

                I have created a bash script that automates all of this, minus the sshd config file, because the IP addresses change.

                So, I am trying to obtain the IP addresses of the various systems in the script at run time, have the bash script search the sshd_config file, and replace the default ListenAddress with one that only has the private IP addresses.
                as i thought well beyond my knowledge of copy paste and certain easy things to remember . good luck with sorting it out .. am sure someone else will have more input than myself here .

                Comment


                  #9
                  Re: Search for specific strings and display only those strings in Linux

                  Originally posted by goontron View Post
                  Try pipping it through "strings" first.

                  Learn regex, it helps on every platform.
                  I forgot about strings! I have tried learning regex a few times but that shit is confusing to me!!!! It seems to be extremely powerful, I just can't seem to grasp the expressions.
                  -- Law of Expanding Memory: Applications Will Also Expand Until RAM Is Full

                  Comment


                    #10
                    Re: Search for specific strings and display only those strings in Linux

                    I can't seem to pipe it to strings. strings wants a filename, and my example was poor. The pattern I'm looking for comes from stdout, not a file.
                    -- Law of Expanding Memory: Applications Will Also Expand Until RAM Is Full

                    Comment


                      #11
                      Re: Search for specific strings and display only those strings in Linux

                      Strings wants a filename?

                      Let me give you an example here.

                      Code:
                      cat BIOS.BIN |strings | grep -i .....-.....-.....-.....-.....
                      Will pull a Windows key from a BIOS image.

                      EG:
                      Code:
                      cat Acer_bios.BIN |strings | grep -i .....-.....-.....-.....-.....
                      Will output:
                      Code:
                      MWGVN-CRM3B-THJMB-00000-XXXXX
                      Also:
                      Code:
                      man regex
                      Last edited by goontron; 10-17-2018, 05:45 PM.
                      Things I've fixed: anything from semis to crappy Chinese $2 radios, and now an IoT Dildo....

                      "Dude, this is Wyoming, i hopped on and sent 'er. No fucking around." -- Me

                      Excuse me while i do something dangerous


                      You must have a sad, sad boring life if you hate on people harmlessly enjoying life with an animal costume.

                      Sometimes you need to break shit to fix it.... Thats why my lawnmower doesn't have a deadman switch or engine brake anymore

                      Follow the white rabbit.

                      Comment


                        #12
                        Re: Search for specific strings and display only those strings in Linux

                        I was using strings wrong. I was using it like:
                        Code:
                        cat fake_ips | strings "192.168.2."
                        Using your example produces the same unwanted output (the entire line, including the 192.168.1.122).

                        I believe I found a way using tr.
                        Code:
                        hostname -I | grep -o "192.168.2..." | tr '\n' ' '
                        or, using my fake_ips example,
                        Code:
                        cat fake_ips | grep -o "192.168.2..." | tr '\n' ' '
                        Thanks!
                        Last edited by Spork Schivago; 10-17-2018, 05:48 PM.
                        -- Law of Expanding Memory: Applications Will Also Expand Until RAM Is Full

                        Comment


                          #13
                          Re: Search for specific strings and display only those strings in Linux

                          Grrr, still not quiet right. With the grep -o "192.168.2..." it's only pulling up to two wildcard numbers at the end. If I have IP addresses with three, it won't grab them. If I add a third dot, then it pulls the 1 from the next string if the first string has an ending digit that's only 1 or 2 digits long.

                          So I try grabbing the IPs using ifconfig, but it's pulling in the broadcast. So I try searching for a range:
                          Code:
                          cat fake_ips | egrep "192.168.2.[2-254]"
                          But this isn't searching for a range. It shows 192.168.2.2, .5, and .4! Not 192.168.2.2 - 192.168.2.254

                          I try it with grep as well and same results. What am I doing wrong there? From what I've read, it should work.
                          -- Law of Expanding Memory: Applications Will Also Expand Until RAM Is Full

                          Comment


                            #14
                            Re: Search for specific strings and display only those strings in Linux

                            are the spacing's correct ? sorry my limited knowledge .

                            Comment


                              #15
                              Re: Search for specific strings and display only those strings in Linux

                              petehall347,

                              Yes, my spacing was correct. I goontron's advice about the regex, because I felt this was probably the only way to accomplish this. Without regex, the way grep works, I ran into issues. the [2-254] was searching for numbers 2-2 and 5, 4. The final solution came from this website:
                              https://www.regular-expressions.info/numericranges.html

                              Code:
                              cat fake_ips | grep -o -E '192\.168\.2\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\b' | tr '\n' ' '
                              It explained why I was obtaining the numbers I was obtaining from my range search with grep, but also showed me how to properly use the range search and word boundaries. The command above prints out the contents of fake_ips to stdout and then pipes it through to grep, which only displays the strings found (-o), and uses the regex expression (-E) to search for numbers 0-9, 10-99, 100-199, 200 - 249, and finally 250 - 254. The \b is the word boundary that says don't do a partial match (so a number like 192.168.2.254 doesn't show up as 192.168.2.25). The \b says make sure there's nothing else attached to the string.

                              What a learning experience! I really thought I knew how to use grep, boy was I wrong!
                              -- Law of Expanding Memory: Applications Will Also Expand Until RAM Is Full

                              Comment

                              Working...
                              X