Search Results for: awk

AWK scripting: 10 BEGIN and END block examples

BEGIN and END statements are help full in displaying information before and after executing actual AWK script. BEGIN block Uses: Declaring variables. Initialization variables for doing increment/decrements operations in main AWK code. Printing Headings/info before actual AWK code output. END block Uses: Printing final results, after doing operations in main AWK block. Printing Completion/info after actual AWK code output. AWK tool is mainly used for reporting some data in useful manner. With out these BEGIN and END blocks the output will be meaning less. In this post we will see different ways to use BEGIN and END blocks. For this post the example file is db.txt which contains below data: Jones 2143 78 84 77 Gondrol 2321 56 58 45 RinRao 2122234 38 37 Edwin 253734 87 97 95 Dayan 24155 30 47 What you get from above data? They are just names and followed by some numbers separated by spaces. If we can give some information at start of the file, such as what each column corresponding to etc as shown below Names total PPT Doc xls Jones 2143 78 84 77 Gondrol 2321 56 58 45 RinRao 2122234 38 37 0 Edwin 253734 87 97 95 Dayan 24155 30 47 0 This is more helpful to understand what actually this data is about. This type of output can be achieved by using BEGIN block. In this post we will see how to use BEGIN and END blocks with examples. AWK BEGIN block This...

Read More

AWK scripting: 8 AWK printf statements examples

This is our third post on AWK scripting. In this post we will cover printf statement which very much use full for formatting output as the padding spaces before and after the column entries etc. Already covered topics in this series are AWK scripting: What is an AWK and how to use it? AWK scripting: 14 AWK print statment examples printf is similar to AWK print statement but the advantage is that it can print with formatting the output in a desired manner. So before learning printf command I suggest you to learn about print command and then come to this printf statement. printf syntax is similar to Bash, C type printf statement. Syntax: awk '{printf "format", Arguments}' filename For example you want to print decimal values of column 3 then the example will be. awk '{printf "%d", $3}' example.txt Printf can do two things which AWK print command can’t 1)Defining type of Data. 2)Padding between columns. AWK printf supported data types The printf can be useful when specifying data type such as integer, decimal, octal etc. Below are the list of some data types which are available in AWK. %i or d...

Read More

AWK scripting: 14 AWK print statment examples

In our last post we covered about AWK and how is it useful. In this post we will see different print statement examples. We will learn AWK print statement with examples as print statement is one of the most used AWK command. For this we taken a test file as below cat db.txt Ip,Henry 29 32 23 27Frank,Joe 26 29 22 28Gill,Marry 25 30 20 25Avery,Adam 25 31 19 21Chow,Samuel 23 29 19 25Dible,Liz 22 30 20 22Warn,Suzanne 23 29 19 23Dow,Juila 24 29 20 20Low,juila 22 21 19 18Joe,Sarah 19 21 18 20 Example1: Search for Dow word in the file and print those lines. This is like simulate grep using AWK awk ‘/Dow/’ db.txt Sample output:Dow,Juila 24 29 20 20 Example2: Search for a word and print the lines which contain either Juila or juila in the given file. awk ‘/[Jj]uila/’ db.txt Output:Dow,Juila 24 29 20 20Low,juila 22 21 19 18 In the above example we used regular expression to match both Juila and juila. Know more about RegExp here. Example3: Print particular column from db.txt file. awk ‘{print $2}’ db.txt Output:29262525232223242219 Exampl4: Print multiple columns from a given file. awk ‘{print $2,$4}’ db.txt Output:29 2326 2225 2025 1923 1922 2023 1924 2022 1919 18 Example5: Print multiple columns with a tab between columns as separator awk ‘{print $2,”t”,$4}’ db.txt  Output:29 2326 2225 2025 1923 1922 2023 1924 2022 1919 18 Example6: Search for Chow...

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.