ipcalc not working when used with xargs
by bignellrp from LinuxQuestions.org on (#4XAX0)
Hi,
Im trying to use ipcalc on the command line when manipulating data. Im not looking to use this within a script as the task may change each time.
I have a set of 100s of log files with ip information. Some contain slash notation, some contain netmask. I want the output to always be consistent and just display network / netmask
The logs need a lot of tidying and my rough grep,sed,awk commands output what i need.
Code:grep "ip address" /home/blah/logs/*.txt | sed 's/logs\///' | sed 's/_/ /g' | sed 's/ip address//' | sed 's/\// /' | grep -v match | grep -v no | awk '{print $5 "/" $6}' | head -n1I've added head -n1 to just work with 1 IP but the output could be 100 IPs.
This outputs 107.15.11.3/24
So i add | xargs ipcalc -nm to the end of my command but i get an error:
ipcalc: bad prefix: 24
If i use xargs -t ipcalc -nm i can see the command is correct.
So i tested the basic command by sending my command to a file.
Code:grep "ip address" /home/blah/logs/*.txt | sed 's/logs\///' | sed 's/_/ /g' | sed 's/ip address//' | sed 's/\// /' | grep -v match | grep -v no | awk '{print $5 "/" $6}' | head -n1 > filecat file | xargs -t ipcalc -nm
but then strangely it works fine
cat file | xargs -t ipcalc -nm
ipcalc -nm 107.15.11.3/24
NETMASK=255.255.255.0
NETWORK=107.15.11.0
The next chalenge would be to display the output on one line and use awk so it appears as extra columns from my original output.
e.g.
107.15.11.3 , 24 , 255.255.255.0 , 107.15.11.0
107.15.12.76 , 24 , 255.255.255.0 , 107.15.12.0
10.115.80.7 , 16 , 255.255.0.0 , 10.115.80.0
Please note im not looking to tidy up my awk/sed/grep as i know it can all be done in awk with some big long clever string i just want to work out why the above fails and how i add my ipcalc output to the columnar list.


Im trying to use ipcalc on the command line when manipulating data. Im not looking to use this within a script as the task may change each time.
I have a set of 100s of log files with ip information. Some contain slash notation, some contain netmask. I want the output to always be consistent and just display network / netmask
The logs need a lot of tidying and my rough grep,sed,awk commands output what i need.
Code:grep "ip address" /home/blah/logs/*.txt | sed 's/logs\///' | sed 's/_/ /g' | sed 's/ip address//' | sed 's/\// /' | grep -v match | grep -v no | awk '{print $5 "/" $6}' | head -n1I've added head -n1 to just work with 1 IP but the output could be 100 IPs.
This outputs 107.15.11.3/24
So i add | xargs ipcalc -nm to the end of my command but i get an error:
ipcalc: bad prefix: 24
If i use xargs -t ipcalc -nm i can see the command is correct.
So i tested the basic command by sending my command to a file.
Code:grep "ip address" /home/blah/logs/*.txt | sed 's/logs\///' | sed 's/_/ /g' | sed 's/ip address//' | sed 's/\// /' | grep -v match | grep -v no | awk '{print $5 "/" $6}' | head -n1 > filecat file | xargs -t ipcalc -nm
but then strangely it works fine
cat file | xargs -t ipcalc -nm
ipcalc -nm 107.15.11.3/24
NETMASK=255.255.255.0
NETWORK=107.15.11.0
The next chalenge would be to display the output on one line and use awk so it appears as extra columns from my original output.
e.g.
107.15.11.3 , 24 , 255.255.255.0 , 107.15.11.0
107.15.12.76 , 24 , 255.255.255.0 , 107.15.12.0
10.115.80.7 , 16 , 255.255.0.0 , 10.115.80.0
Please note im not looking to tidy up my awk/sed/grep as i know it can all be done in awk with some big long clever string i just want to work out why the above fails and how i add my ipcalc output to the columnar list.