LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-25-2010, 04:18 PM   #1
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Rep: Reputation: 16
Question "if " drop the path


Hi everyone....

i have the following code:

Quote:
hostb=$(awk -F" " 'NR==4{print $2}' /southbuilding/netsouth)
royalb=192.174.23.2

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?

THANK YOU for your input.
 
Old 05-25-2010, 04:52 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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
Hope this helps.
 
Old 05-25-2010, 08:12 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
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):
Code:
royalb=192.174.23.2

mpotsb=$(awk -F" " 'NR==4{if($2 !~ /\//)print $2;else print '"$royalb"'}' /southbuilding/netsouth)
Not sure if you still needed hostb as well??

Last edited by grail; 05-25-2010 at 08:17 PM.
 
Old 05-25-2010, 09:45 PM   #4
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
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".

thought to add the whole thing.

Thank you.

Last edited by NetRock; 05-25-2010 at 09:53 PM.
 
Old 05-26-2010, 12:47 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
Fair enough:
Code:
royalb=192.174.23.2

mpotsb=$(awk -F" " -v rb="$royalb" 'NR==4{if($2 !~ /\// && $2 != rb)print $2;else print rb}' /southbuilding/netsouth)
 
Old 05-26-2010, 10:51 AM   #6
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
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.
 
Old 05-27-2010, 09:13 PM   #7
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
Hi grial,

I ended up using the following code ( you wrote on 05-25-10, 09:12 PM)post # 3

Quote:
royalb=192.174.23.2

mpotsb=$(awk -F" " 'NR==4{if($2 !~ /\//)print $2;else print '"$royalb"'}' /southbuilding/netsouth)
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:

Quote:
mpotsb=$(awk -F" " -v 'NR==4{if($2 = /\//); else print '"$2"'}' /southbuilding/netsouth)
so when there is path after the first space it drops the whole line but if after the first space is an ip address it prints the line (the ip address).

appreciate your help
 
Old 05-28-2010, 12:01 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
Code:
mpotsb=$(awk -F" " -v 'NR==4{if($2 = /\//); else print '"$2"'}' /southbuilding/netsouth)
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.
 
Old 05-28-2010, 01:57 AM   #9
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
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:

Quote:
NS 192.168.76.0
NS 192.168.45.1
NS 192.168.0.100
NS /var/setup
NS 192.174.23.2
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).

Quote:
mpotsb=$(awk -F" " -v 'NR==4{if($2 = /\//); else print '"$2"'}' /southbuilding/netsouth)
echo $mpotsb >> /newfile
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?

hope this is clear.
Thank you for your time.
 
Old 05-28-2010, 06:27 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
hmmm..I am still not sure we are on the same page here

I will use the examples in the post #9.

file1 contains:
Quote:
NS 192.168.76.0
NS 192.168.45.1
NS 192.168.0.100
NS /var/setup
NS 192.174.23.2
script equals:
Code:
mpotsb=$(awk -F" " 'NR==4{if($2 = /\//); else print "'"$2"'"}' file1)
echo $mpotsb
Now let me run the following:
Code:
./script a b
Based on the above information the output of running this is a blank line

file2 contains:
Quote:
NS 192.168.76.0
NS 192.168.45.1
NS 192.168.0.100
NS /var/setup
NS 192.174.23.2
script equals:
Code:
mpotsb=$(awk -F" " 'NR==4{if($2 = /\//); else print "'"$2"'"}' file2)
echo $mpotsb
Now let me run the following:
Code:
./script a b
Based on the above information the output of running this is "b" <-- I am guessing not what you want

My last advice to would be that the if - else is a waste just make the test a not and should be contains and not equals:
Code:
mpotsb=$(awk -F" " 'NR==4{if($2 !~ /\//)print "'"$2"'"}' file2)
echo $mpotsb
 
Old 05-28-2010, 07:47 AM   #11
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
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.


Quote:
file1
NS 192.168.76.0
NS 192.168.45.1
NS 192.168.0.100
NS /var/setup
NS 192.174.23.2
above ln#4 should be dropped.

Quote:
file1
NS 192.168.76.0
NS 192.168.45.1
NS 192.168.0.100
NS 192.168.55.2
NS 192.174.23.2
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)

echo $mpotsb >> myfile
i really want to thank you for your patient!!
 
Old 05-28-2010, 08:33 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
And this will work fine:
Code:
mpotsb=$(awk -F" " 'NR==4{if($2 !~ /\//)print $2}' file2)
echo $mpotsb
Whereas the one in post #5 will do the comparison with the other IP you have.
 
Old 05-28-2010, 01:27 PM   #13
NetRock
Member
 
Registered: Mar 2010
Posts: 134

Original Poster
Rep: Reputation: 16
Hi grail,
Thank you grail for your help. tested & is working the way it is supposed to.
 
Old 05-28-2010, 10:19 PM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
Cool
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
IPTables has a "drop all anywhere" and "accept all anywhere" Zero187 Linux - Security 4 06-18-2009 07:09 PM
"Could not init font path element""Unix /: 7100 removing from list/ zameer_india Linux - Networking 7 07-03-2006 06:11 AM
Cedega from Fat32 (Invalid path "." given for "--use-dos-cwd") bdox Linux - Software 0 03-30-2005 11:24 AM
Cedega and Fat32 (Invalid path "." given for "--use-dos-cwd") bdox Linux - Games 0 03-26-2005 02:48 AM
What does "SFW2-INext-DROP-DEFLT" in my messages log file mean? TrulyTessa Linux - Networking 11 12-22-2004 09:28 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration