import { addBanner, addArticle, addTitle, addHeader, addParagraph, addSubHeader } from '/scripts/article.js'; import { addInset, addInsetList, addInsetCodeListing, addInsetBulletList } from '/scripts/inset.js'; import { addImageWithCaption, addButtonGroup } from '/scripts/visuals.js'; import { addSidebar} from '/scripts/sidebar.js'; import { addSyntax } from '/scripts/code.js'; import { menu } from '/scripts/web_dev_buttons.js'; import { global_menu } from '/scripts/grid_layout1.js'; import { local_menu } from '/scripts/linux.js'; const heading = document.querySelector(".heading"); const global = document.querySelector(".global_menu"); const local = document.querySelector(".local_menu"); const sidebar = document.querySelector(".sidebar"); const main = document.querySelector(".main_content"); heading.append(addTitle("AWK Essential Training")); heading.append(addParagraph("David D. Levine - LinkedIn Learning - November 2022")); heading.append(addParagraph("Chapter 1 - AWK Command Line Basics")); main.append(addHeader("What is AWK?")) main.append(addParagraph("It was originally designed to help with filtering and manipulating data in text files although it has grown to be more powerful than that. It is pre-installed on most modern Linux systems and can be invoked with the Command AWK.")) main.append(addParagraph("On Windows, you can install cygwin which is a Unix shell environment. You can also use Unix Utils which provides a collection of Unix utils that you can run from a command prompt. Although it isn't mentioned in the course videos, you can use one of the shells in Windows Subsystem for Linux. There are other options as well including both free and paid-for options.")) main.append(addParagraph("AWK is designed to work with files that are broken down in to lines and where each line can be broken down into fields but these don't have to be consistent. However, if the data is consistent and structured, there are more things you can do with it.")) main.append(addParagraph("An AWK program can be a simple one-liner input from the CL and even that can do some useful things such as finding a specific record, outputting particular fields in the record or you can also use it to change the order of the fields in a file.")) main.append(addParagraph("It doesn't work so well with binary files but in some cases, you may be able to convert these into a format that will work with AWK. It can be used to generate HTML but is not very good at parsing it. I guess I could use it for generating the .js files for my website by putting this text for example into a file and then having AWK read the file a line at a time and outputting it with the appopriate additions, eg ")) main.append(addSyntax("This line of text")) main.append(addParagraph("becomes")) main.append(addSyntax("This line of text”))")) main.append(addParagraph("Equally, I could use this for converting the file to HTML which would remove the need for me to use Javascript To generate my HTML and probably would mean that I wouldn't need to use Dreamweaver.")) main.append(addParagraph("I think it would make sense to use something other than a space as a divider because that allows you to have whole lines of text that you can treat as single fields.")) main.append(addParagraph("So as I do this course, creating a program that can do that will be a little side-project for me to help me consolidate my learning.")) main.append(addSubHeader("Versions of AWK")) main.append(addSyntax("Original AWK - developed in 1978 for UNIX v7")) main.append(addSyntax("New AWK (nAWK) - developed in 1985 for UNIX System V")) main.append(addSyntax("GNU AWK (gAWK ) - developed for Linux in the 90s")) main.append(addParagraph("If you are using AWK on a modern system, the version you are using is gAWk.")) main.append(addHeader("WRITING AN AWK PROGRAM")) main.append(addParagraph("As was mentioned before, the simplest version of an AWK program is a single line input at the CH. As an example of this, let's say you have a file called names.txt where each line is a name in the format")) main.append(addSyntax("first_name surname")) main.append(addParagraph("and you want to output these in reverse order")) main.append(addSyntax("surname first_name")) main.append(addParagraph("You can do that with")) main.append(addSyntax("awk '{print $2, $1}' names txt")) main.append(addParagraph("We use single quotes to enclose the awk program because AWK uses some characters that are special to the shell. The curly braces indicate the start and end of the AWK program and $2 and $1 reference, respectively, field 2 and field 1 in the file. Finally, we have the name of the file we want read data from.")) main.append(addParagraph("Note that the comma in that AWK program is just a separator for the fields so you won't see it in the output . We are using print with two parameters which are, in this case, AWK fields and print is simple concatenating them so a couple of things here")) main.append(addSyntax("the parameters don't have to be AWK fields.")) main.append(addSyntax("the comma is a separater that adds a space between the fields.")) main.append(addParagraph("If you omit the comma you will see that the surname and first name fields are output without a space between but we can use the fact that AWK is concatating the output by putting a string literal between those fields, Let's say we want a comma and a space betwen the two fields. The command would then become")) main.append(addSyntax("awk ' {print $1, ", " $2} ' names.txt")) main.append(addImageWithCaption("./images/names.jpg", "Figure 1 - the output we get from the names.txt file.")) main.append(addParagraph("In figure 1, I have run the awk commands to first output the contents of the names.txt file in the same format as the file itself. I have then output the contents in reverse order and then reverse order with a comma and a space between the fields in the output. For reference and as a first step in the my side-project, if we wanted to convert these lines to a format that be used in an HTML list, we would use the command")) main.append(addSyntax("awk '{print \"<li>\" $1 \", \" $2 \"</li>\"} ' names.txt")) main.append(addImageWithCaption("./images/names_list.jpg", "Figure 2 - the output from converting the names.txt file to an HTML file where each name becomes a list item.")) addSidebar("linux");