awk field separator
Separating fields in awk
You’ve probably come across awk command and its most simple use: splitting string elements separated by blank spaces. In this short post I’d like to expand a little bit on using awk field separators.
To demonstrate, let’s inspect the output of ifconfig command on my Macbook:
I’d like to extract the IP address for this interface, which means we should first use grep to isolate just the line of output we want:
Great! Now it should be fairly easy to use awk to get that IP address. Since it’s the second word from the left, we’re telling awk to print parameter 2, {print $2}:
** uses space as field separator by default, but you can also specify any other character to use as separator instead.
To continue with our example, I further parse the output (which is just the IP address at this stage) using . character as field separator:
Obviously, if I want to access the last 2 octets of the IP address, I will modify the last awk command accordingly:
See Also
- Advanced Unix commands
- Review logs with tail and awk
Separating fields in awk
You’ve probably come across awk command and its most simple use: splitting string elements separated by blank spaces. In this short post I’d like to expand a little bit on using awk field separators.
To demonstrate, let’s inspect the output of ifconfig command on my Macbook:
I’d like to extract the IP address for this interface, which means we should first use grep to isolate just the line of output we want:
Great! Now it should be fairly easy to use awk to get that IP address. Since it’s the second word from the left, we’re telling awk to print parameter 2, {print $2}:
** uses space as field separator by default, but you can also specify any other character to use as separator instead. To continue with our example, I further parse the output (which is just the IP address at this stage) using . character as field separator:
Obviously, if I want to access the last 2 octets of the IP address, I will modify the last awk command accordingly:
See Also
- Advanced Unix commands
- Review logs with tail and awk