Hi, this is Sandra Henry-Stocker, author of the “Unix as a Second Language” blog on NetworkWorld.
In this Linux tip, we’re going to look at the awk command. Awk is a scripting language but offers a lot of useful features for the command line. In fact, it’s frequently used to select one or more pieces of text from lines of input. Here’s a basic example with awk being used to select the 3rd and 4th pieces of text from the input provided.
This next awk command will show groups starting with “st” in the /etc/group file:
Note that ^ refers to the beginning of the line and $ to the end
If we want to work with a particular field in a file that contains colon-separated text like /etc/group, we have to provide the separator to awk.
In this command, we only want to see the first field:
In this command, we only want to list groups with group IDs greater than 1,000 (user groups rather than system groups).
To show groups with members (lines not ending with a :)
Awk is a very useful tool for selecting data from files or the output from other processes.
That’s your Linux tip for today. If you have questions or would like to suggest a topic, please add a comment below. And don’t forget to subscribe to the IDG Tech(talk) channel on YouTube. If you liked this video, please hit the like and share buttons. For more Linux tips, be sure to follow us on Facebook, YouTube and NetworkWorld.com.