'awk' Script Examples

from 'FE Nautilus Scripts'

('complete' code samples)

Home > RefInfo menu > Computer topics menu >

(Linux) Handy Commands and Scripts Menu page >

'awk' Script Examples (Complete Scripts) menu page >

This 'awk' Script Examples from 'FE Nautilus Scripts' page.

! Note !
More sample 'awk' scripts may be added
to this page --- when I revisit or use this page
--- and if I add more 'awk' scripts to 'FE Nautilus Scripts'.

! Note !
Descriptions are placed below each link-to-a-script below,
to draw attention to noteworthy 'awk' features of the scripts.

< Go to List of Scripts, below. >
(Skip the following Introduction.)

INTRODUCTION :

There are many tutorial web pages and books that contain 'snippets' of 'awk' code that give an idea of the many things you can do with 'awk' --- in particular, extracting and reformatting and calculating based on contents of text files (or based on text strings 'piped' to the 'awk' command).

However, many of those 'snippet' guides leave one at a loss as to how the 'awk' code can be used within a larger script that contains code leading up to the awk-usage and code that follows up on the awk-usage.

This page is meant to present script examples that amount to 'complete scripts' that make use of 'awk' --- often in conjunction with other commands such as 'grep' and 'sort' and 'sed' and 'ls' and 'find' and 'df' and 'du'.

Many of these 'complete scripts' show how the awk output is presented in a GUI text file displayer --- such as a GUI text editor.


Some 'complete' awk scripts :

The code samples on 'Freedom Environment' script-samples pages --- such as the 'feNautilusScripts' Code Samples pages --- may be, in some cases, too large for tutorials.

But they have much to offer in the way of examples of working (that is, de-bugged ... to a pretty good extent) shell scripting code for shell-script developers 'of all stripes'.

I have extracted to this page those 'FE Nautilus Scripts' that use the 'awk' command.


The 'list-of-scripts' section below provides a means to see copies of code samples from the 'FE Nautilus Scripts' software system, where FE = Freedom Environment.

Click on a link to see a page presenting a 'complete' awk script.

If you want to save the code to a file on your computer, you can RIGHT-click on the link and use a popup menu option such as 'Save Link Target As ...' to save the file.


You can use the Find-text option of your web browser to look for scripts with certain features. Examples:

If you want to find scripts that use the built-in 'substr' function, search this page for 'substr'.

If you want to find scripts that use the built-in 'index' function, search this page for 'index'.

If you want to find scripts that use a 'BEGIN' or 'END' section, search this page for 'begin' or 'end'.

If you want to find scripts that use the '==' comparison operator, search this page for '=='.

If you want to find scripts that use the '~' (twiddle) string-match operator, search this page for '~'.

List of Scripts:

  • ./EXAMINEfiles/00_1file_LineLENGTHS-Summary_awk.sh.txt
    This script contains an 'awk' program that reads a (text) file and shows a summary at the end of reading the file. The summary shows the max and min line lengths --- and number of records read --- and average number of characters per record.
    The 'awk' program uses 'BEGIN' and 'END' sections in addition to the 'body' section.

  • ./EXAMINEfiles/00_1file_LineLENGTHS_awk.sh.txt
    This script contains an 'awk' program that reads a (text) file and shows the length of each line --- with a summary at the end that shows the max and min line lengths --- and number of records read --- and average number of characters per record.
    Uses 'BEGIN' and 'END' sections in addition to the 'body' section.
    NOTE: On huge files, this could generate a long list of record lengths.

  • ./EXAMINEfiles/04a_1file_SHOW-STR-MATCH-LINES_awk.sh.txt
    This script contains a *MANY-LINE* 'awk' program that reads a (text) file and shows the lines that contain a user-specified string (or strings).
    Multiple-strings can be user-specified by separating the multiple strings by vertical-bars (|) --- like the 'egrep' command.
    The output shows PLUS-OR-MINUS N lines before and after each 'match-line',
    where N is hard-coded to 4.
    Three '-v' options are used to pass 3 parameters to the awk program.
    This awk program uses such items as 'BEGIN', 'for ( i = 1 ; i <= N ; i++ )',
    a array 'prev[i]', 'if', '==', 'toupper', 'split', 'index', '!=',
    'printf', 'print', 'for ( i = N ; i > 0 ; i-- )', '&&', and 'END'.

    This 'SHOW-STR-MATCH-LINES' script shows quite a useful variety of 'awk' processing options.

  • ./EXAMINEfiles/04b_1file_SHOW-RANGE-OF-LINES_awk.sh.txt
    Two '-v' options are used to pass 2 parameters to the awk program ---
    a 'start' line number and an 'end' line number.
    This awk program uses such items as 'BEGIN', '+=', 'if', '>=', '&&',
    '<=', 'print', 'exit', 'END', 'printf'.

  • ./FINDlists/05_anyfile4Dir_findFILES-BIGGERthanNmeg_allLEVS_ find-f-size-ls-sort-awk.sh.txt
    This script pipes output from 'find' and 'ls' through 'sort and into 'awk'.
    The awk program uses such items as 'BEGIN', 'index', '=', 'printf',
    'substr', '+', and 'END' to print a column of floating-points numbers
    along with some columns of strings.

  • ./FINDlists_TEST/.findANDshow_strings1and2_over2linesINfile_ awk.sh.txt
    This script uses 4 '-v' options to pass 4 parameters to the awk program ---
    This awk program uses such items as 'BEGIN', 'if', '==', 'toupper', 'index',
    'getline', 'printf'.
    This script uses 'getline' to get 2nd lines in the process of finding the match lines in the file which contain a match to the string1 in the 1st of a pair of successive lines and string2 in the 2nd line of the pair of lines.

  • ./FINDlists_TEST/05_1file_findSTRover2lines_ awk.sh.txt
    This script passes a single filename to the 'hidden' script '.findANDshow_strings1and2_over2linesINfile_awk' above.

  • ./FINDlists_TEST/05_anyfile4Dir_findSTRover2lines_ in_dirMASKFILS_1LEV_awk.sh.txt
    This script uses 'find' with '-maxdepth 1' to pipe filenames into the 'hidden' script '.findANDshow_strings1and2_over2linesINfile_awk' above.

  • ./FINDlists_TEST/05_anyfile4Dir_findSTRover2lines_ in_dirMASKFILS_allLEVS_awk.sh.txt
    This script uses 'find' to pipe filenames into the 'hidden' script '.findANDshow_strings1and2_over2linesINfile_awk' above.

  • ./MAPtools/10_oneSVGfile_EXTRACTpath-etcXYdata_ egrep-fold-awk-sed-txteditor.sh.txt
    This script uses the 'fold' command to pipe the shortened (folded) lines
    of an SVG file into an awk command.
    This awk program uses such items as 'BEGIN', '=', 'if', '==', 'index',
    'print', 'next', 'else', '!='.
    The output from the awk program is piped through about 35 'sed' substitution commands and a final 'fold' command.

  • ./SPACElists/00_anyfile_SPACE-in-FILESYSTEMS_ df-k-awk.sh.txt
    This script pipes the output from 'df -k' through a simple 'sed' command and
    a 'sort' command into the 'awk' command'.
    The awk command processes a single 'printf' statement that uses some simple arithmetic to calculate some 3 columns of numbers and prints out the three columns of numbers along with several columns of strings.

  • ./SPACElists/01a_anyfile4Dir_SPACE-USEDby-SUBDIRS_nLEVS_ SIZEsort_find-d-maxdepth-du-s-awk.sh.txt
    This script uses the 'find' command and the 'du -k' command piped through a 'sort' command into the 'awk' command.
    The 'awk' command processes a simple 2-line program that uses 'index' and 'substr' and 'printf' and some simple arithmetic to printout two columns. One column is a floating-point number and the other column is a string.

  • ./SPACElists/01b_anyfile4Dir_SPACE-USEDby-SUBDIRS_allLEVS_ SIZEsort_du-awk.sh.txt
    This script uses the 'du -k' command piped through a 'sort' command into the 'awk' command.
    The 'awk' command processes a simple 2-line program that uses 'index' and 'substr' and 'printf' and some simple arithmetic to printout two columns. One column is a floating-point number and the other column is a string.

  • ./SPACElists/03_anyfile4Dir_SPACE-USED-by-FILES_1LEV_ SIZEsort_ls-awk.sh.txt
    This script uses the output of an 'ls -la' command piped through simple 'sed' and 'grep' and 'sort' commands into the 'awk' command.
    The 'awk' command processes a simple 2-line program that uses 'index' and 'substr' and 'printf' and some simple arithmetic to printout seven columns. One column is a floating-point number and the other 6 columns are strings.

  • ./SPACElists/04_anyfile4Dir_SPACE-USEDby-FILES_1LEV_ AGEsort_ls-awk.sh.txt
    This script uses the output of an 'ls -latr' command piped through simple 'sed' and 'grep' commands into the 'awk' command.
    The 'awk' command processes a simple 2-line program that uses 'index' and 'substr' and 'printf' and some simple arithmetic to printout seven columns. One column is a floating-point number and the other 6 columns are strings.

  • ./SPACElists/05a_anyfile4Dir_SPACE-USEDby-FILES_allLEVS_ SIZEsort_find-ls-awk.sh.txt
    This script uses the output of 'find' and 'ls -l' commands piped through a simple 'sort' command into the 'awk' command.
    The 'awk' command processes a simple 2-line program that uses 'index' and 'substr' and 'printf' to printout three columns that are strings. That output is piped into a 'sort'.

  • ./SPACElists/05b_anyfile4Dir_SPACE-USEDby-BIGGESTfiles_ allLEVS_find-ls-awk.sh.txt
    This script uses the output of 'find' and 'ls -l' commands piped through a simple 'sort' command into the 'awk' command. The 'find' command uses a '-size' option.
    The 'awk' command processes a simple 2-line program that uses 'index' and 'substr' and 'printf' and some simple arithmetic to printout seven columns. One column is a floating-point number and the other 6 columns are strings.

  • ./SPACElists/06_anyfile4Dir_COUNT-FILESinSUBDIRS_allLEVS_ find-d-ls-grep-wc-awk.sh.txt
    This script uses the 'find' command with the '-type d' option and an '-exec' option to run a 'hidden' script named '.filecnt_4dir_1level_2stdout.sh' on each directory found.
    The output is piped into an 'awk' command.
    The 'awk' command processes a simple 2-line program that uses 'index' and 'substr' and 'printf' and some simple arithmetic to printout seven columns. One column is a floating-point number and the other 6 columns are strings.

  • ./SPAMEXAMtools/00a_1mailFolderFile_COUNT-Emails_ awk.sh.txt
    This script feeds a mail-folder-file into an 'awk' program about 7 lines long.
    The 'awk' program uses 'BEGIN', '=', 'if', '~' (for matching a simple regular expression), 'END' and 'printf'. The 'END' section prints out a count of the number of emails in the 'folder file', which were counted by finding 'From' lines.

  • ./SPAMEXAMtools/01a_1mailFolderFile_SHOW_FROM-IPaddressLINES_ awk.sh.txt
    This script feeds a mail-folder-file into an 'awk' program about 22 lines long.
    The 'awk' program uses 'BEGIN', '=', 'if', '~' (for matching a simple regular expression), 'index', 'substr', '!=', 'END' and 'print'.
    This awk program prints out at least 2 lines from each email --- 'From -' and 'Received: from' lines. The 'Received' lines may contain IP addresses of mail servers.
    'Date:' and 'Subject:' lines are also printed.

  • ./SPAMEXAMtools/01b_1mailFolderFile_SHOW_FROM-IPaddresses_ awk.sh.txt
    This script feeds a mail-folder-file into an 'awk' program about 22 lines long.
    The 'awk' program uses 'BEGIN', '=', 'if', '~' (for matching a simple regular expression), 'index', 'substr', '!=', 'END' and 'print'.
    This awk program prints out at least 2 lines from each email --- 'From -' and at least one IP address extracted from a 'Received: from' line.
    'Date:' and 'Subject:' lines are also printed.

  • ./SPAMEXAMtools/01c_1mailFolderFile_SHOW_ALL-IPaddressLINES_ awk.sh.txt
    This script feeds a mail-folder-file into an 'awk' program about two lines long.
    The 'awk' program uses 'if' and '~' (for matching a simple regular expression), along with 'print'.
    This awk program prints out all 'From -' and 'Received: from' lines --- to show essentially all IP addresses in the folder file.

  • ./SPAMEXAMtools/01d_1mailFolderFile_SHOW_All-HEADERlines_ awk.sh.txt
    This script feeds a mail-folder-file into an 'awk' program about seven lines long.
    The 'awk' program uses 'if' and '~' (for matching a simple regular expression), along with 'print'.
    This awk program prints out all lines containing:
    - 'From - '
    - 'Return-Path: '
    - 'Received: from '
    - 'From: '
    - 'To:'
    - 'Subject:'
    - 'Date:'
    to show all mail 'header' lines in the folder file.

  • ./USER-GROUPtools/00_anyfil_SHOW-GROUPS_Names-Numbers-and-their-Users_ etc-groups-awk-sort.sh.txt
    Uses a one-line 'awk' program on the '/etc/groups' file to printout columns 1, 3, and 4 (which were separated by a colon character) in nice, aligned columns. The output is piped into a 'sort' command.

  • ./XWINCOLORtools/00_anyfile_SHOW-XCOLORS-BRIGHTNESSorRorGorB-SORTED_ showrgb-grep-awk-sort.sh.txt
    This script pipes the output of the 'showrgb' program through simple 'tr', 'grep', 'sort' and 'uniq' commands and then into two 'awk' programs.
    The first 'awk' program is about ten lines long and uses 'BEGIN', '=', 'if', '==', 'next', and 'print'.
    The second 'awk' program is 2 lines long and uses 'index', 'substr', and 'printf' along with some simple arithmetic.
    The final output is piped through a 'sort' command. The type of 'sort' is specified by the user from about 13 sort options offered by a 'zenity' GUI prompt.

  • ./XWINCOLORtools/00_anyfile_SHOW-XCOLORS-MORE-RorGorB-SORTED_ showrgb-grep-awk-sort.sh.txt
    This script pipes the output of the 'showrgb' program through simple 'tr', 'grep', 'sort' and 'uniq' commands and then into two 'awk' programs.
    The first 'awk' program is about ten lines long and uses 'BEGIN', '=', 'if', '==', 'next', and 'print'.
    The second 'awk' program is actually a choice from 6 different 2-line awk programs that use 'if', '<=', '&&', '>=', and 'print'.
    The final output is piped through a 'sort' command.
    The type of 2nd-awk-program and the type of 'sort' is specified by the user from 6 R-G-B select-and-sort options offered by a 'zenity' GUI prompt.

  • ./XWINCOLORtools/00_anyfile_SHOW-XCOLORS-NEAR-RGB_ showrgb-grep-nawk-sort.sh.txt
    This script pipes the output of the 'showrgb' program through simple 'tr', 'grep', 'sort' and 'uniq' commands and then into one 'awk' program.
    The 'awk' program is about seven lines long and uses '=', subraction, 'sqrt', multiplication, addition, 'index', 'subsrt', and 'printf' to print out 8 columns of numbers and names.

End of this List of 'FE Nautilus Scripts' that use 'awk'.

Bottom of this
'awk' Script Examples from 'FE Nautilus Scripts' page.

To return to a previously visited web page location, click on the Back button of your web browser a sufficient number of times. OR, use the History-list option of your web browser.
OR ...

< Go to Table of Contents, above. >

< Go to Top of Page, above. >

Page was created 2018 Jun 16.
Page was changed 2018 Jun 17. (Added info on awk code in 4 scripts.)
Page was changed 2018 Jun 21. (Added info on awk code in about 20 more scripts.)
Page was changed 2018 Sep 01. (Added css and javascript to try to handle text-size for smartphones, esp. in portrait orientation.)