Calling Unixguy and the gods of Bash! a Cut, Sed, Awk query

ally_ukally_uk Member Posts: 1,145 ■■■■□□□□□□
One for the gods of Centos / Bash probably are a few ways of resolving this issue.

ip addr show - shows IP Address and loads of information

I am purely just interested in having the IP adress to be used in a script.

So if I narrow results down further:

ip addr show | grep 192

Produces:

inet 192.168.1.5/24 brd 192.168.1.255 scope global enp03s

Now I could run further greps to narrow it down but how do I just get the line 192.168.1.5

Am I right in thinking Cut? Sed? Awk? please elaborate and help a brother out with your wisdom
smile emoticon
Like · Comment · Share
Microsoft's strategy to conquer the I.T industry

" Embrace, evolve, extinguish "

Comments

  • networker050184networker050184 Mod Posts: 11,962 Mod
    You can do grep -o pretty easy. Probably want to do -Eo and have a regex match any IP in case the IP doesn't start with 192.
    An expert is a man who has made all the mistakes which can be made.
  • ipchainipchain Member Posts: 297
    Try this: echo "inet 192.168.1.5/24 brd 192.168.1.255 scope global enp03s" | cut -f 2 -d " " | cut -f 1 -d "/"

    It should give you what you need.
    Every day hurts, the last one kills.
  • ExpectExpect Member Posts: 252 ■■■■□□□□□□
    a really quick and dirty code, there are better ways than this but it works.
    ip addr show | grep global | awk -F' ' '{print $2}'  | awk -F'/'  '{print $1}'
    
  • ChickenNuggetzChickenNuggetz Member Posts: 284
    Depends on what you're trying to accomplish. If you just want to set a variable with the IP address of the machine (i.e. this could be a generic script being used on any number of hosts, so the IP is variable), then I'd probably use a combination of awk and sed.

    Awk can let you filter by "column," so you can use awk to isolate the "column" of your output to just the IP.
    Sed will let you trim off the "/24" on the end.

    I wont tell you how exactly to do this, because half the fun is in figuring it out on your own! Good luck!
    :study: Currently Reading: Red Hat Certified Systems Administrator and Engineer by Ashgar Ghori

    Certifications: CCENT; CCNA: R&S; Security+

    Next up: RHCSA
  • ChickenNuggetzChickenNuggetz Member Posts: 284
    Expect wrote: »
    a really quick and dirty code, there are better ways than this but it works.
    ip addr show | grep global | awk -F' ' '{print $2}'  | awk -F'/'  '{print $1}'
    

    That's the beauty of Linux, there's so many ways to get things done!
    :study: Currently Reading: Red Hat Certified Systems Administrator and Engineer by Ashgar Ghori

    Certifications: CCENT; CCNA: R&S; Security+

    Next up: RHCSA
  • ally_ukally_uk Member Posts: 1,145 ■■■■□□□□□□
    Thanks I got this aswell:

    ip addr show | awk '/192/ {print $2}'
    Microsoft's strategy to conquer the I.T industry

    " Embrace, evolve, extinguish "
  • networker050184networker050184 Mod Posts: 11,962 Mod
    What happens if the address is a 10. or 172. though? Probably want that to match on a variable length [1-9]{1,3}\. or something along those lines.
    An expert is a man who has made all the mistakes which can be made.
  • ally_ukally_uk Member Posts: 1,145 ■■■■□□□□□□
    Yeah you are right if the I.P address isn't 192 I am not sure about how to go about what you are saying though.
    Microsoft's strategy to conquer the I.T industry

    " Embrace, evolve, extinguish "
  • networker050184networker050184 Mod Posts: 11,962 Mod
    ip addr show | grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'

    That will pull out the IPs. Now you just need to get just the IP from the interface you want. The ip addr show command will give you all interface IPs.
    An expert is a man who has made all the mistakes which can be made.
  • ally_ukally_uk Member Posts: 1,145 ■■■■□□□□□□
    That's way above my level :)

    I need to go back to basics starting with the cut command, Like fields and rows I don't really understand it i'm reading tutorials but it's hurting my head lol
    Microsoft's strategy to conquer the I.T industry

    " Embrace, evolve, extinguish "
  • ally_ukally_uk Member Posts: 1,145 ■■■■□□□□□□
    I need to take a baby steps approach to solidify concepts do you mind guiding me?


    The first thing is say if I have lot's of text on the screen from a command output How would I go about just printing the 1st line of output and nothing else?


    I now have a line of text say for example:


    1: lo: <LOOPBACK,UP,LOWER,_UP> mtu 65536 qdisc noqueue state UNKNOWN


    Using the cut utility I only want to display lo: my question is how the hell do you go about working out fields, rows, tables, sections :) when the formatting is off like that and how the hell do you know what to cut? god i'm so confused.....

    is the 1: classed as field1 or is the : field2?


    Thank you for help guys :)
    Microsoft's strategy to conquer the I.T industry

    " Embrace, evolve, extinguish "
  • DoubleNNsDoubleNNs Member Posts: 2,015 ■■■■■□□□□□
    ally_uk wrote: »
    The first thing is say if I have lot's of text on the screen from a command output How would I go about just printing the 1st line of output and nothing else?
    head -n 1
    
    prints the 1st line. So does
    awk 'NR==1 {print;exit}'
    
    head would be slightly faster, but the benefit of the awk snippet is you can set that "1" to any line number you want. Change it to 25 and it'll print just the 25th line.
    Using the cut utility I only want to display lo: my question is how the hell do you go about working out fields, rows, tables, sections icon_smile.gif when the formatting is off like that and how the hell do you know what to cut?

    You can set your field delimiter to whatever you want. I think by default it's simply whitespace. So field 1 is the beginning of the line towards the first whitepace char (tab/space). Field 2 would be the text directly following the first space but before the 2nd space.

    If you're working in a comma-deliminated file then you can make the field every time a new comma comes. You can even set the field to a number or letter. But I'm pretty sure w/ cut, the delimiter has to be a single char. If your text file has a string w/ multiple-char-length you want to set as your delimiter, you can use a sed substitution to turn that multiple-char-string into a single char - something that won't show up elsewhere on the line.
    Goals for 2018:
    Certs: RHCSA, LFCS: Ubuntu, CNCF CKA, CNCF CKAD | AWS Certified DevOps Engineer, AWS Solutions Architect Pro, AWS Certified Security Specialist, GCP Professional Cloud Architect
    Learn: Terraform, Kubernetes, Prometheus & Golang | Improve: Docker, Python Programming
    To-do | In Progress | Completed
  • DoubleNNsDoubleNNs Member Posts: 2,015 ■■■■■□□□□□
    You can also set the entire line to a variable and cut off the beginning and front of the variable strings. I think the expansion is something similar to ${,,varname} and ${varname##}.

    To be honest I don't remember the exact syntax off head. Maybe someone can correct it.
    Goals for 2018:
    Certs: RHCSA, LFCS: Ubuntu, CNCF CKA, CNCF CKAD | AWS Certified DevOps Engineer, AWS Solutions Architect Pro, AWS Certified Security Specialist, GCP Professional Cloud Architect
    Learn: Terraform, Kubernetes, Prometheus & Golang | Improve: Docker, Python Programming
    To-do | In Progress | Completed
  • ally_ukally_uk Member Posts: 1,145 ■■■■□□□□□□
    Wow thank you for the response :) you really do know your stuff you are i'm getting it I think :)

    using this line as a example and working with cut:

    (example1)

    1: lo: <LOOPBACK,UP,LOWER,_UP> mtu 65536 qdisc noqueue state UNKNOWN

    What would the cut command look like if I wanted to display just: ( alternative I could do this with sed? )

    (example 2)

    1: lo:

    <LOOPBACK,UP,LOWER,_UP> mtu 65536 qdisc noqueue state UNKNOWN

    (example 3)

    Or display a specific part of the line say: This could be done with a grep?:

    mtu 65546


    Thanks for the guidance and excuse the amount of examples I find it helps me to cement it into my grey matter :)
    Microsoft's strategy to conquer the I.T industry

    " Embrace, evolve, extinguish "
  • DoubleNNsDoubleNNs Member Posts: 2,015 ■■■■■□□□□□
    grep doesn't print only a part of a line. The grep command will always grab the entire line. You can make grep grab multiple lines surrounding the specified string/pattern, but you can't (to my knowledge) make grep grab only part of the line. You would usually use cut/awk to break up lines and grab specific output from it.

    cut would be faster - which is important when working w/ large files. awk is much more flexible.


    Try to edit/reformat your examples. They didn't seem to come out right and I can't quite make out what your questions are.
    Goals for 2018:
    Certs: RHCSA, LFCS: Ubuntu, CNCF CKA, CNCF CKAD | AWS Certified DevOps Engineer, AWS Solutions Architect Pro, AWS Certified Security Specialist, GCP Professional Cloud Architect
    Learn: Terraform, Kubernetes, Prometheus & Golang | Improve: Docker, Python Programming
    To-do | In Progress | Completed
  • DoubleNNsDoubleNNs Member Posts: 2,015 ■■■■■□□□□□
    I'm double posting a lot (procrastinating on last min studying) but I just understood what the "grep -o" option does. It DOES allow you to print just the matching part of the line. You'd want to use regex w/ the grep -o option to make it useful. (I'm simply echoing the very 1st response in this thread fromnetworker050184unfortunately.)

    Additionally, found out Linux has the hostname -I command. Try it.
    Depending on what you're trying to accomplish (and what platform you're coding on) that might be all you need.
    Goals for 2018:
    Certs: RHCSA, LFCS: Ubuntu, CNCF CKA, CNCF CKAD | AWS Certified DevOps Engineer, AWS Solutions Architect Pro, AWS Certified Security Specialist, GCP Professional Cloud Architect
    Learn: Terraform, Kubernetes, Prometheus & Golang | Improve: Docker, Python Programming
    To-do | In Progress | Completed
  • ally_ukally_uk Member Posts: 1,145 ■■■■□□□□□□
    Good morning :) thank you for your replies. I am going to be taking things back to basics and will be spending some time learning the basics of cut, sed, awk, grep.

    I gave myself a migraine yesterday from over thinking haha I was getting really confused with the cut command, Like I understand that it can specifically remove characters and fields and you can use a delimiter that is all good if you are working with a nice neatly arranged file such as

    one : two : three : four : five

    but what if you output a command and it's a bit all over the place such as

    one twothreee fourfivvvvvvvesixxxxxx

    How does cut know to remove remove specific parts say for example if I wanted to output nothing else apart from the threee in the above example. or would I use a more suitable tool such as awk or sed or something.
    Microsoft's strategy to conquer the I.T industry

    " Embrace, evolve, extinguish "
  • UnixGuyUnixGuy Mod Posts: 4,570 Mod
    why confuse yourself with 'cut' ? I'd stick with "awk".

    Use the solution by @Expect, it works. Try other variations with awk as well. Stick with one tool, master it, then move to another tool IF necessary. I don't use 'cut' at all, awk does everything just fine
    Certs: GSTRT, GPEN, GCFA, CISM, CRISC, RHCE

    Learn GRC! GRC Mastery : https://grcmastery.com 

  • ally_ukally_uk Member Posts: 1,145 ■■■■□□□□□□
    Well that was AWKward! :) see what I did there?

    I was thinking that aswell I will use cut when files are neatly organized i.e one: two: three:
    actually your right I could just use awk why am I even having nightmares about cut.... :)


    Thank you UnixGuy, I have another brain teaser for yourself on Centos 7 I want to find out my I.P address for a script. A guy posted me a solution for centos 6.



    Find the list of active ethernet interfaces.....

    route -n|grep -v Destination|awk '{print $8}'|sort -u

    Then for each of these (eth0 shown for example):

    ifconfig eth0|sed -n -e '/inet /p'|awk '{print $2}'


    I haven't tested on Centos 7 but I assume you would just change the ifconfig? or is there a better way to achieve the above

    Many Thanks
    Microsoft's strategy to conquer the I.T industry

    " Embrace, evolve, extinguish "
Sign In or Register to comment.