Search Results for: awk

AWK substr function explained with examples

We already writtn one excellent string manipulation post for shell. This post is in response to the code which I seen in my office for greping IP address from ifconfig command. The command is.. ifconfig eth0 | grep 'inet addr' | cut -d: -f 2 | awk '{print $1}' Above command explanation: Grep command will filter ifconfig command output to list only ip address line. Cut command then cuts out that line to print only ip address and a word Bcast Then awk command just prints first column of the output. This will get the IP address of the network interface. I don't blame or point out the flaw in this command. But I want to show the beauty of awk command which is good at text parsing and even sub strings as well. In awk there is a search function which we can accomplish with // as shown below ifconfig eth0 | awk '/inet addr/ {print}' Output:           inet addr:192.168.1.135  Bcast:192.168.1.255  Mask:255.255.255.0 Now how to get IP address part from this line? Use below command ifconfig eth0 | awk '/inet addr/{print $2}' Output: addr:192.168.1.135 But how can I remove that addr: from the output? There is an builtin function in AWK which is useful to print sub strings. The syntax for this is as follows. awk '{print substr(column-number,start-point,end-point)}' Note: End-point is an optional argument. So now you want...

Read More

AWK Scripting: Learn AWK Built-in variables with examples

AWK inbuilt variables: FS, OFS, RS, ORS, NR, NF, FNR, FILENAME AWK is supplied with good number of built-in variables which come in handy when working with data files. We will see each AWK built-in variables with one or two examples to familiarize with them. Without these built-in variables it’s very much difficult to write simple AWK code. These variable are used to format output of an AWK command, as input field separator and even we can store current input file name in them for using them with in the script. Some of the AWK concepts already covered are. AWK scripting: What is an AWK and how to use it? AWK scripting: 14 AWK print statement examples AWK scripting: 8 AWK printf statements examples AWK scripting: 10 BEGIN and END block examples AWK Scripting: How to define awk variables AWK built-in variables: NR: Current count of the number of input records. NF: Keeps a count of the number of fields FILENAME: The name of the current input-file. FNR: No of records in current filename FS: Contains the “field separator” character RS: Stores the current “record separator” or Row Separator. OFS: Stores the “output field separator”. ORS: Stores the “output record separator” or Output RS. Our sample DB file for this post is db.txt cat db.txtJohn,29,MS,IBM,M,MarriedBarbi,45,MD,JHH,F,SingleMitch,33,BS,BofA,M,SingleTim,39,Phd,DELL,M,MarriedLisa,22,BS,SmartDrive,F,Married In order to make it simple we can divide above  inbuilt variables in to groups on...

Read More

AWK Scripting: How to define awk variables

AWK variables: This is our ongoing tutorials on AWK scripting. As we mention earlier AWK is a full pledged language with all statements, arrays, control structures, functions etc. Today we will see how to define a variable in AWK and use it when it’s required. We already covered following AWK concepts AWK scripting: What is an AWK and how to use it?AWK scripting: 14 AWK print statment examplesAWK scripting: 8 AWK printf statements examplesAWK scripting: 10 BEGIN and END block examples What is a Variable? A variable is defined as storage location to store some value in it so that we can use this in a program. Variables will protect us from varying values in the storage location. This will help us to avoid hardcode a value in to program where ever it’s used. We can define a variable at the start of program and use the variable across the program, if we want to change the value of it, we can change it where we define it and this value will be updated where ever we use that variable. Defining a variable in AWK We can define variable in AWK where ever we require. Variables can be used forInitializations for valuesArithmetic operations And many more.For this concept we will use below db.txt file. cat db.txt Jones 2143 78 84 77Gondrol 2321 56 58 45RinRao 2122234 38 37Edwin 253734 87...

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.