Search Results for: awk

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

AWK ignore case(CAPS/lower) while patten search

Recently I come across a requirement to search for lines in a given file/files where the search term is mix of capital letters and small letters. Normally when we search with awk it search exactly what we try to search but not ignoring case. We can use grep to accomplish this task as sown below. grep -i 'available' files | awk '{print $1,$2}' But I don't want to use grep to do this as there is a way we can accomplish with just awk. We can specify ignoring case to AWK in three ways as mention below. Use AWK builtin variable called IGNORECASE enable ignore case switch with search pattern Use not so sophisticated method of using both cases in search term Let see these three in examples. Example1: Search for a word 'available' which some times written as Available or AVAILABLE. Using IGNORECASE AWK inbuilt variable example as below awk 'BEGIN{IGNORECASE=1} /available/{print $1}' filename BEGIN is an AWK keyword which is useful to execute awk code before actual AWK script. We are setting IGNORECASE before our actual AWK code execution. Example2: Search for word 'available' using 'i' switch so that my match will have both cases awk '/available/i {print $1}' filename if you observe after serach pattern we given /i which will help us to ignore case with our search term. Example3: Search for 'available' word with combination of cases which will not work...

Read More

Over 16,000 readers, Get fresh content from “The Linux juggernaut”

Email Subscribe

ABOUT ME..!

My photo
My name is Surendra Kumar Anne. I hail from Vijayawada which is cultural capital of south Indian state of Andhra Pradesh. I am a Linux evangelist who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. At present I work at Bank of America as Sr. Analyst Systems and Administration. You can contact me at surendra (@) linuxnix dot com.

banner