ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
if [ "${hostb}" != "${royalb}" ]
then
mpotsb="$hostb"
else
mpotsb="$royalb"
fi
when the value of hostb is ip address (like 171.16.16.15) the "if" statement works fine. But sometimes the value of hostb is a path like ( /name/service) then always the value of $mpotsb becomes the path. what can i do to ignore the value of $hostb when it is a "path" & just take the value of royalb? without breaking what's working now?
You need an additional test on the value of hostb. For example you might see if it doesn't match a "/" (assumed the alternative to the IP address is always a path) or if it matches a regular expression for IPs. In bash the double brackets construct [[...]] let the usage of the =~ operator to match against a pattern or a regular expression, e.g.
Code:
if [[ ! "${hostb}" =~ "/" && "${hostb}" != "${royalb}" ]]
then
mpotsb="$hostb"
else
mpotsb="$royalb"
fi
If I remember a bit about your previous query on this, were you not adding CIDR to the end of the address?
Just urging some caution as your hostb may look like - 171.16.16.15/24
Which will still have a slash in it.
Also, you could do most of this in your awk (if you chose to):
Thank you colucix & grail for your help.
hostb look like - 171.16.16.15 & hostb needed as long as it is an IP address. otherwise must be dropped (ignored) if it is a path. the thing is line #4 has
ip address or "Path"(like /blah/blah). I will try your solution & let you know.
That is kind of you grail to recall my previous pots, usually people do not....!!!
also, still i need to check this:
Quote:
if [ "${hostb}" != "${royalb}" ]
so if the value of hostb is "path" then the value of mpotsb="$royalb" otherwise if the value of hostb is an ip address & not equal: mpotsb="$hostb" & if it is equal is mpotsb="$royalb".
Thanks for the great help. spent almost half a day on that. trying my best to avoid posting & do my best first but i guess i need more time to became a MASTER like grail. appreciate grail for your quick ,efficient to the point solution.
but the value of $mpotsb=192.1740.230.2 no idea where these extra 0's coming from & if i replace $royalb with $2 ( which is an ip address) comes as 0 only. very strange. i changed few things like " " or ' ' anything i can think of but nothing worked.
i want to used this code for my another step in the project where if ln#4 is something like /blah/blah drop the line but if it is an ip address just keep it so i used:
Not sure if this is what you wanted, but just to confirm, the else here is looking for an argument ($2) passed to your script, ie not the field $2 from the file (hope this makes sense)
Did you try the one from post #5? It may resolve the issue with the extra characters/numbers and it is a better fit for the test of not equal to royalb variable.
thanks for the code. $2 is the field from the file. So, my file on ln#4 once it has a path like (blah/blah) must be dropped & when it has an Ipaddress should be kept. when netsouthFile looks like:
in the case of the above example line#4 should be dropped but when ln#4 has an ipaddress should be prited.
i could use something like: grep -v "/" but it is not attacking only line#4 it looks at the whole file. I just need to focus on ln#4. next the output will be sent out to a "newfile". so in my newfile i must have ip addresses. so the final code will look like below (for example).
i used the code on post#5 for another step it works GREAT! (thanks a lot) it does the magic. but for this step i used Post#3 but at the end of the code when i echo $mpotsb it comes as 192.1740.230.2 instead of 192.174.23.2 no idea where these extra 0's coming from?
Thanks for your detailed reply. Sorry not able to explain clearly. "blank line" is good when i have /blah/blah in ln#4 but in the same file when i have an ip address i should see ln#4.
then line 4 is ok i will keep it once i want to direct it to the another file (let's say myfile).
so i am looking for:
Quote:
in file1
mpotsb=$(awk -F" " -v 'NR==4{if($2 = /blah/blah then drop the whole line#4 ; else print '"$2"' if it is an ip address in line#4 and output to myfile}' /file1)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.