Using awk to sum numbers in a file
The awk command found on Linux/Unix and Mac OS X systems can be used to
sum numbers in a file. E.g., suppose the file
numbers.txt
contains the following numbers:
10
20
30
40
50
1
2
3
4
5
The contents of the file can be piped into the awk
command with
the cat
command and then summed by awk
.
$ cat numbers.txt | awk '{sum+=$1} END {print sum}'
165
If the numbers are not in the first column in the file, but were in the
second column instead, you can adjust $1
to be the relevant
column instead. E.g, if the file contents looked like the following with
the numbers in the second column, then you would use $2
instead.
Dave 10
Bill 20
Joe 30
Mary 40
Maria 50
Howard 1
Sam 2
Lisa 3
Karen 4
Nina 5
$ cat numbers.txt | awk '{sum+=$2} END {print sum}'
165
If you know the numbers always occur in specific colum positions in the file,
e.g., in positions 10 to 15, you could also use the cut
command instead of the cat
command. E.g., if you file contained:
Dave 10
Bill 20
Joe 30
Mary 40
Maria 50
Howard 1
Sam 2
Lisa 3
Karen 4
Nina 5
$ cut -c10-11 numbers.txt | awk '{sum+=$1} END {print sum}'
165
[/os/unix/commands]
permanent link
Managing Wi-Fi from the terminal command line under OS X
To manage
Wi-Fi connections from a shell prompt on a Mac OS X system you can
obtain a command line interface by running the
Terminal program located
in Applications/Utilities. From that command line interface, you can determine
whether a WiFi interface is present on the system using the command
networksetup -listallnetworkservices
. You should see "Wi-Fi"
in the list of services that appears when you issue the command.
$ networksetup -listallnetworkservices
An asterisk (*) denotes that a network service is disabled.
Bluetooth DUN
Ethernet
FireWire
Wi-Fi
To determine the hardware interface supporting Wi-Fi connections you
can use the command networksetup -listallhardwareports
.
$ networksetup -listallhardwareports
Hardware Port: Bluetooth DUN
Device: Bluetooth-Modem
Ethernet Address: N/A
Hardware Port: Ethernet
Device: en0
Ethernet Address: d4:9a:20:0d:e6:ec
Hardware Port: FireWire
Device: fw0
Ethernet Address: d4:9a:20:ff:fe:0d:e6:ec
Hardware Port: Wi-Fi
Device: en1
Ethernet Address: f8:1e:df:d9:2b:66
VLAN Configurations
===================
In the case above, the Wi-Fi interface is en1
.
To get information on the status of the system's Wi-Fi connection, you
can use the command networksetup -getinfo Wi-Fi
.
$ networksetup -getinfo Wi-Fi
DHCP Configuration
IP address: 192.168.0.5
Subnet mask: 255.255.255.0
Router: 192.168.0.1
Client ID:
IPv6: Automatic
IPv6 IP address: none
IPv6 Router: none
Wi-Fi ID: f8:1e:df:d9:2b:66
To find if the system is currently connected to a wireless network and the
network name for the current wireless connection, you can use
networksetup -getairportnetwork <device name>
where
device name is the network interface on the system that supports
WiFi connections. E.g.:
$ networksetup -getairportnetwork en1
Current Wi-Fi Network: Copernicus
If you stipulate a network interface that is not a WiFi interface, you
will get an error message indicating the interface is not a Wi-Fi interface
as shown below:
$ networksetup -getairportnetwork en0
en0 is not a Wi-Fi interface.
** Error: Error obtaining wireless information.
If you wish to to turn the Wi-Fi connection on or off from a shell
prompt, you can use the networksetup -setairportnetwork
command.
networksetup -setairportnetwork <device name> <network> [password]
$ networksetup -setairportpower en1 off
$ networksetup -getairportnetwork en1
You are not associated with an AirPort network.
Wi-Fi power is currently off.
$ networksetup -setairportpower en1 on
$ networksetup -getairportnetwork en1
Current Wi-Fi Network: Copernicus
[/os/os-x]
permanent link