Skip to main content

Posts

Showing posts from October, 2020

LINUX AWK Tutorial Series | Chapter 7

LINUX AWK Tutorial Series | Chapter 7 Hello and welcome to AWK tutorial series, Hope till now you would have found the series interesting. In this chapter, I am going to discuss loops in awk. Looping Looping helps to do a particular set of actions repeatedly until a certain condition is met. Types of Loop in AWK while do .. while for Syntax While Loop: until the condition is true the body of the while will be executed. while (condition) { body } Do While Loop:  Do until the condition is not met. So this means the code body will always be executed once every time. do { body } while (condition) For Loop:  Condition and variable initialization is done at the time of the start of the loop. for (init;condition;increment) { body } Remember that the loop will be executed on all records one by one. Some important looping constructs are  break continue exit Example 1: From the employee data file, I want to print only 2 fields for each line twice every time. I can use while loop in...

LINUX AWK Tutorial Series | Chapter 6

LINUX AWK Tutorial Series | Chapter 6 Hello Guys.. Welcome In this series, we are in chapter 6. Now I am going to discuss the conditional statements in AWK. Now, what are these conditional statements?  Conditional statements  help to process a set of activities based on a certain condition which is defined. The type of conditional statements which we see across the most programming language are here as well if else else if Sample syntax of if else if (conditon true) then actions; else if (condition1 true) then actions; else actions Now let's see how to use them in AWK. Example 1: Now let's say I want to give a 10% appraisal to all employees having a salary of less than 30000. I am going to use my Employee data csv file. If you understand the previous chapters it would be easy to find what each part is doing. The only addition here is that I have put if (condition) and based on that printing my data. Try at Home: Try to do the same with AWK  scripts Example 2: Now let's s...

LINUX AWK Tutorial Series | Chapter 5

LINUX AWK Tutorial Series | Chapter 5 Hello Welcome... to this series. In this chapter, I am going to discuss the built-in variables in AWK Built-In Variables These variables are pre-defined and we can directly use them. Also, remember don't give user-defined variables the same as built-in variables. Below are the built-in variables available in awk. RS: record separator in the file being processed. The default value is a new line. FS: Field separator. The default value is white space. ORS: output record separator. The default value is a new line. OFS: Output field separator. The default value is white space. NR: Number of records processed by awk NF: Number of fields in the current record FNR: Current record number in each file. It is incremented each time a new record is read. It will be initialized to 0 once a new file is read FILENAME: Name of file being read ARGC : Number of arguments provided at the command line. ARGV : Array that store command line arguments. ENVIRON...

LINUX AWK Tutorial Series | Chapter 4

LINUX AWK Tutorial Series | Chapter 4 Hello Guys, Hope you are enjoying the series. Remember practice will only help you grow and learn. So keep practicing!! In this post, I am going to discuss the Variables and Operators in AWK command. Variables and Operators 1) User Define Variables So let's discuss the User-Defined Variables, As mentioned earlier AWK is a combination of a filtering tool and a programming language. So same as other programming languages it will also support Variables, Constants, operation, loops, etc.. Variable is a representation of a value referring to a variable name. We will see both built-in and user-defined variables.  I am going to focus on user-defined variables here. Important Note: No variable declaration is required in AWK same as shell scripting. Variables will be initialized to null string or zero automatically. Variable should begin with letter and can be followed by letters, numbers, underscores. Variable are case sensitive. So be careful. Exam...

LINUX AWK Tutorial Series | Chapter 3

LINUX AWK Tutorial Series | Chapter 3 Hello Everyone, Welcome Back... In this chapter, let's get our hands dirty and start doing the basics. Journey Begins AWK will work on the field level of a record and that will help to do any operations on the individual fields. example 1: In this example, we are searching for a pattern "F" (Female Employees) from the Employee Data file and doing an action item which is "print" Please observe that whatever search/action is performed those are on single quotes '' If you remember the first chapter, that's how the AWK work. Read file> Process lines > Search Pattern > Performs action example 2: Now AWK also can process records based on a regular expression. Let's say I want to find all employee name starting with "Rob" we are searching the pattern here which start with " Rob* ". * means 0 or more characters. example 3: Now I want to find multiple patterns in a single file then we can...

LINUX AWK Tutorial Series | Chapter 2

LINUX AWK Tutorial Series | Chapter 2 Hello Everyone, now in this chapter I am going to share the keywords and topics covered in the series of AWK. Let's learn some basic concepts which will be used a lot in this tutorial series. AWK itself is like a full processing language and uses almost most of the concepts of any similar programming languages. Important topics which would be covered in this series: Delimiter: It is used to separate fields in AWK. By default delimiter for a file in AWK is space or white space, but we can use any other delimiter as well for separating the fields and processing based on the same. In simple words, we can say it helps in separating two words. Variables : Variables can be of the following types: 1) User Defined: It is defined by the user in AWK. 2) Built-in Variables: They are predefined in AWK and should not be used as user-defined variables Example: RS, FS,ORS,OFS,NR,NF,ARGV, ARGC Conditional Statements: How to check specific conditions in AWK usi...

LINUX AWK Tutorial Series | Chapter 1

LINUX AWK Tutorial Series | Chapter 1 Hello Everyone, This is my tutorial series on Linux AWK. I will try to cover the AWK concepts chapter wise in this series and will try to explain in an easy method. Introduction to Linux AWK Linux AWK  is a language for processing text files. AWK is typically used as data extraction and reporting tool. It is a standard feature of most Unix-like operating systems. It consists of a set of actions to be taken against streams of textual data for purposes of extracting or transforming text, for eg: producing formatted reports. The language uses the string datatype, associative arrays, and regular expressions. AWK was created at Bell Labs in the 1970s and its name is an acronym derived from the surnames of its authors—Alfred Aho, Peter Weinberger, and Brian Kernighan. Benefits: AWK is used for searching and extracting data from a file. ​ It can also be used for manipulating the data and generate reports WHAT CAN WE DO  WITH AWK​ AWK is like an i...

Understanding Linux Log Files

Understanding Linux Log Files Log files are a lot of records that Linux keeps up for the sysadmins to monitor the significant and important events in the system. They contain messages about the kernel, services, and applications running on it. The log files are found in  /var/log directory. The log documents created in a Linux environment can commonly be characterized into four unique classes: 1)Application Logs 2)Event Logs 3)Service Logs 4) System Logs Role of  Linux log files Log is a fundamental aspect of any sysadmin duty.  By observing Linux log files, you can increase a definite understanding of kernel execution, security, error messages, and warning issues. In the event that you need to take a proactive versus a receptive way to deal with the errors. For a sysadmin standard log record examination is 100% required.  To put it plainly, log records permit you to envision forthcoming issues before they really happen. Important Linux log files to keep an eye on M...