AWK: Print header line and pattern match
AWK is a powerful regular expression filtering and pattern matching scripting language. Please consider heading to awk tutorials section to read through our other awesome AWK tutorials which deep dive into different aspects of the language. In this tutorial, we look at how we can use AWK to print the header lines from a file or a command output along with the pattern being searched. While filtering output from certain commands or lengthy reports, it may be important to display the first line of the file or the header line to make sense of the rest of the output which is being displayed. Consider the below output. [sahil@linuxnix ~]$ df -hTP Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/vg_pbox6-lv_root ext4 18G 4.9G 12G 30% / tmpfs tmpfs 491M 80K 491M 1% /dev/shm /dev/sda1 ext4 477M 35M 418M 8% /boot /dev/sr0 iso9660 3.7G 3.7G 0 100% /media/CentOS_6.8_Final /dev/sdb ext4 488M 396K 462M 1% /u01 /dev/sdc ext4 488M 396K 462M 1% /u02 We would like to print only the ext4 type file systems but along with the header line as well to make sense of the values indicated by the respective fields. We could use grep to meet this requirement as done in the below command [sahil@linuxnix ~]$ df -hTP | grep -E "Filesystem|ext4" Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/vg_pbox6-lv_root ext4 18G 4.9G 12G 30% / /dev/sda1 ...
Read More