Search for specific strings and display only those strings in Linux

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Spork Schivago
    Badcaps Legend
    • Mar 2012
    • 4734
    • United States of America

    #1

    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
  • petehall347
    Badcaps Legend
    • Jan 2015
    • 4425
    • United Kingdom

    #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

    • petehall347
      Badcaps Legend
      • Jan 2015
      • 4425
      • United Kingdom

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

      out of interest what is the project ?

      Comment

      • Spork Schivago
        Badcaps Legend
        • Mar 2012
        • 4734
        • United States of America

        #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

        • Spork Schivago
          Badcaps Legend
          • Mar 2012
          • 4734
          • United States of America

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

          Originally posted by petehall347
          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

          • petehall347
            Badcaps Legend
            • Jan 2015
            • 4425
            • United Kingdom

            #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

            • goontron
              5000!
              • Dec 2011
              • 4108
              • US

              #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

              • petehall347
                Badcaps Legend
                • Jan 2015
                • 4425
                • United Kingdom

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

                Originally posted by Spork Schivago
                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

                • Spork Schivago
                  Badcaps Legend
                  • Mar 2012
                  • 4734
                  • United States of America

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

                  Originally posted by goontron
                  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

                  • Spork Schivago
                    Badcaps Legend
                    • Mar 2012
                    • 4734
                    • United States of America

                    #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

                    • goontron
                      5000!
                      • Dec 2011
                      • 4108
                      • US

                      #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

                      • Spork Schivago
                        Badcaps Legend
                        • Mar 2012
                        • 4734
                        • United States of America

                        #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

                        • Spork Schivago
                          Badcaps Legend
                          • Mar 2012
                          • 4734
                          • United States of America

                          #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

                          • petehall347
                            Badcaps Legend
                            • Jan 2015
                            • 4425
                            • United Kingdom

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

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

                            Comment

                            • Spork Schivago
                              Badcaps Legend
                              • Mar 2012
                              • 4734
                              • United States of America

                              #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

                              Related Topics

                              Collapse

                              • hinisa
                                Xiaomi router freezes at boot after re-capping
                                by hinisa
                                I'm using Xiaomi Router 4A gigabit edition as wireless repeater. It worked fine for 2 years or so, but last week, wireless started to disappear.

                                When I unplug/replug the power, it was working fine for a day/half a day or so until it freezes again.

                                After testing with different power supply adapters and concluding it wasn't the issue I finally opened the case and this was the situation:



                                Anyway I proceed to clean up the mess and replace all caps but unfortunately it made the situation worse. Now router can't even boot, either it freezes during...
                                07-08-2023, 05:58 AM
                              • bastien
                                OBDATA Raccoon Computer V1 for Macbook Pro Air 2012-2025
                                by bastien
                                OBDATA Raccoon Computer V1 for Macbook Pro Air 2012-2025

                                It's the end of three years of work, it costs money, it costs time, and you owe me a beer.

                                This is the new database for FlexBV

                                it's optimized to work with it
                                by using board ID has Default name, give us the opportunity,
                                to use other diode board data with sibling board or family board
                                I Merged the whole data (optimal)


                                A2991 A2992 M3 -> -> 2 Entries diode data M1
                                A2681 A3113 A3114 M1 M2 Air -> -> 2 Entries diode data M1 M2 13"
                                A2442 A2485 A2779...
                                03-18-2025, 08:03 AM
                              • mr.malcom
                                HP 17-cd0606ng no Windows or Linux Boot, exept idle=poll Boot-Parameter
                                by mr.malcom
                                Hello,

                                this is a very strange Problem:

                                Notebook hang instantly on Boot. BIOS is accesable.
                                Windows: instant after showing "loading circle", even Windows Boot Stick hang instant (even without installed SSD/NVME )
                                Linux: instantly (if Linux Boot in leagacy Mode with log messages: Machine Check exception after 0.25 sec)

                                i have tried:
                                - only one RAM Stick, another working RAM, both RAM channel
                                - BIOS Flash to newest Version F.50, BIOS Reset
                                - Memtest86 hang instant at Test 2
                                - HP UEFI Diagnostic wont find...
                                02-26-2022, 05:19 AM
                              • tsavpyn
                                Requests to Intel Flash Programming Tool for Linux Ubuntu x86_64
                                by tsavpyn
                                Dear all,
                                I forgot my BIOS password of my Surface Pro 3, I searched for solutions for a long time and knew that by using FPT to dump my bios to bin file and using decryptor, I can get my BIOS password.
                                Now I have a problem that I am unable to dump my BIOS into bin file.

                                My Surface Pro 3 currently runs on Ubuntu 22.04, with Secure Boot enabled and Boot From SSD Only.

                                I searched for FPT but there is no Linux version and none of them work.
                                I hereby request a copy of Intel Flash Programming Tool for Linux x64.
                                I would really appreciate it if anyone...
                                05-28-2024, 11:18 PM
                              • NostromoKane
                                INVALID entries in BIOS of Lenovo Yoga C630-13Q50 Windows on Snapdragon 2in1
                                by NostromoKane
                                Hello,

                                I have a Lenovo Yoga C630-13Q50 Windows on Snapdragon laptop that I bought used here in Japan. They only sell the 4GB RAM version here in Japan so I decided to purchase an 8GB motherboard off of Aliexpress. The motherboard works without issue but Product ID, MTM, and Lenovo SN entries in the BIOS are INVALID.

                                I would like to input the values from the old motherboard. After searching the internet I learned about LVAR and have tried to use it to input the values but I get the following error.

                                I have tried placing the LVAR folder on C drive and Desktop...
                                05-08-2025, 05:48 PM
                              • Loading...
                              • No more items.
                              Working...