awk command explained with practical examples part -2

Awk command is powerful tool as we learned few awk command / scripting methods in Previous article, in this article i am going to explain few more. Let’s see what are they

1. How to replace Nth occurrence of matched pattern using awk command

For this example we are going to use below mentioned file content. We are going to replace 3rd occurrence of ‘SSSS’ with ‘YYYY’.

[root@TechTutorial ~]# cat replace.txt
1. RRRR1
SSSS
TTTT1
WWWW1
2.RRRR1
SSSS
TTTT1
WWWW1
3. RRRR1
SSSS  
TTTT1
WWWW1
4. RRRR1
SSSS
TTTT1
WWWW1
5. RRRR1
SSSS
TTTT1
WWWW1

As i said above 3rd occurrence will be replaced with YYYY in the file. Let’s see how the output will be

[root@TechTutorial ~]# awk 'BEGIN {count=0}
 {
  if($1 == "SSSS")
  {
   count++
  }
  if(count == 3)
  {
   sub("SSSS","YYYY",$1)
  }
 }
 {
  print $0
 }' replace.txt
1. RRRR1
SSSS
TTTT1
WWWW1
2.RRRR1
SSSS
TTTT1
WWWW1
3. RRRR1
YYYY  <<-- Replaced matched pattern
TTTT1
WWWW1
4. RRRR1
SSSS
TTTT1
WWWW1
5. RRRR1
SSSS
TTTT1
WWWW1

in above awk script what we are saying is if $1 whatever the first argument equal to SSSS then increment by adding +1 when increment is 3 then substitute the value with new mentioned value

2. Adding New line after every 3 lines

Here we may get an requirement that add new line after every Nth line. In this case we are adding new line after every 3rd line.

Example file content:

[root@TechTutorial ~]# cat newline
1. FIRST LINE
2. SECOND LINE
3. THIRD LINE
1. FIRST LINE
2. SECOND LINE
3. THIRD LINE
1. FIRST LINE
2. SECOND LINE
3. THIRD LINE

See the below awk script which adds an new line after every 3rd line

[root@TechTutorial ~]# awk '{
 if(NR%3 == 0)
 {
  print $0"\nFourth Line 4";
 }
 else
 {
  print $0
 }
     }' newline
1. FIRST LINE
2. SECOND LINE
3. THIRD LINE
Fourth Line 4  <<-- New Line
1. FIRST LINE
2. SECOND LINE
3. THIRD LINE
Fourth Line 4 <<-- New Line
1. FIRST LINE
2. SECOND LINE
3. THIRD LINE
Fourth Line 4 <<-- New Line

In above example we are using NR = Number of records. If NR equal to 3 then print \n=NEW Line and print mentioned text

3. Print Fibonacci number sequence using awk command

The Fibonacci sequence is named after Italian mathematician Leonardo of Pisa, known as Fibonacci. Now we will print the Fibonacci sequence number using below awk script

[root@TechTutorial ~]# awk ' BEGIN{
>  for(i=0;i<=10;i++)
>  {
>   if (i <=1 )
>   {
>    x=0;
>    y=1;
>    print i;
>   }
>   else
>   {
>    z=x+y;
>    print z;
>    x=y;
>    y=z;
>   }
>  }
>     }'
0
1
1
2
3
5
8
13
21
34
55

4. Find & SUM of even and odd numbers separately

We have one file with alpha numeric as mentioned below

[root@TechTutorial ~]# cat even-odd
A 1
B 2
C 3
D 4
E 5
F 6
G 7

using above file content match the even and odd numbers then SUM even numbers and ODD numbers separately. Below is the awk command example

[root@TechTutorial ~]# awk '{
 if(NR%2 == 1)
 {
  sum_e = sum_e + $2
 }
 else
 {
  sum_o = sum_o + $2
 }
      }
      END { print sum_e,sum_o }' even-odd
16 12

1+3+5+7 = 16. 2+4+6 = 12.

it means we achieved required output

5.  Print first character of the each line

Example file is containing

[root@TechTutorial ~]# cat leadingZero
0First Line
0Second Line
0 Third Line
0 Fourth Line
1 Fifth Line

To print first character of each line in the file use below awk commands

[root@TechTutorial ~]# awk '{print $1 + 0}' leadingZero
0
0
0
0
1
[root@TechTutorial ~]# awk '{printf "%d\n",$0}' leadingZero
0
0
0
0
1

That’s it about this awk command part -2 article. We will see more and more awk scripting tutorial in up coming articles.

Stay Tuned ….

Please provide your feedback if it helpful for you….

Keywords:

awk command in linux with examples,awk command in linux with examples pdf,awk command in linux pdf,awk command in linux wikipedia,awk command in linux ppt,awk command in linux with options,awk command in linux script,awk command in linux shell script,awk in linux,awk command in unix and linux,about awk in linux,sed and awk command in linux,grep and awk command in linux,cut and awk command in linux

Related Articles

awk scripting explained with extraordinary examples

sed command with practical examples

Thanks for your wonderful Support and Encouragement

Ravi Kumar Ankam

My Name is ARK. Expert in grasping any new technology, Interested in Sharing the knowledge. Learn more & Earn More

Leave a Reply

Your email address will not be published. Required fields are marked *