AWK command

Actions:

This command is mostly used for pattern scanning and processing. It allows us to use variables, logical operations, numeric and string functions, etc.

Syntax:

awk ‘pattern { actions } ‘ inputfile

Some built-in variables :

NR – Keeps the current count of the number of records
NF – Keeps the current count of the number of fields within the current input field
FS – Field Separator
RS – Record separator
ORS – Output Record Separator (Default ORS is newline)
OFS – Output Field Separator (Default ORS is blank space)

Below are some commands using awk:

awk ‘{print}’ testfile.txt

awk ‘/TCS/{print}’ testfile.txt

awk ‘/TCS/{print $1}’ testfile.txt

awk ‘/TCS/{print $0}’ testfile.txt

awk ‘/TCS/{print NR,$0}’ testfile.txt

awk ‘{print NR,$0}’ testfile.txt

awk ‘{if($4==”TCS”)}{print NR,$0}’ testfile.txt

awk ‘{if($4==”TCS”)} END{print NR,$0}’ testfile.txt

awk ‘{print $1,NF}’ testfile.txt

awk ‘{print $1,$NF}’ testfile.txt

awk ‘{print $1,$(NF-1)}’ testfile.txt

awk ‘NR==3, NR==4{print $1,$(NF-1)}’ testfile.txt

awk ‘NR==3, NR==6{print $1,$(NF-1)}’ testfile.txt

awk ‘{print $1 “:”$(NF-1)}’ testfile.txt

awk ‘{print $1,$(NF-1)}’ testfile.txt

awk ‘BEGIN{OFS=”:”;}{print $2,$NF}’ testfile.txt

awk ‘BEGIN{ORS=”:”;}{print $2,$NF}’ testfile.txt

awk ‘BEGIN{ORS=”nnn”;}{print $2,$NF}’ testfile.txt

awk ‘{print $2,$NF}’ testfile.txt

======================
Important playlists
======================
Data Structures
Linux:
SonarQube:

======================
You can follow me on:
======================

Instagram:
LinkedIn:
Github:
Medium:

=============================================================
|| Subscribe and hit the bell button to get latest content ||
=============================================================

Nguồn: https://codeigniterbrasil.com

Xem thêm bài viết khác: https://codeigniterbrasil.com/lap-trinh-linux/

14 Comments

Engineer

May 6, 2020

I have re-upload this video with better fonts. Please watch it using this link: https://youtu.be/AZ0El_xoMA8

Reply

Raman kumar

May 6, 2020

Nothing is visible sir

Reply

Ravi

May 6, 2020

You need to zoom the screen. Unable to see

Reply

Terry Alleyne

May 6, 2020

very good video. if i had a single field called name which had a first name and last name separated by a space in a file where the fields are separated by colons. how would i print just the last name from that field.

Reply

Raja

May 6, 2020

Too much small font😖😖😖

Reply

Barath Kawin

May 6, 2020

Speak loud and increase font

Reply

Kyle Clark

May 6, 2020

on 13:24 you use a variable $max to store the largest length file, so is the rule that you do not need to declare/assign a value to a variable within an awk command prior to using it (i.e. awk creates the variable on the fly?)?

Reply

Raj Kumar

May 6, 2020

Font size to be increased

Reply

Jagatjita Panda

May 6, 2020

THANK U SIR.ONLY MAXIMISE THE SIZE OF THE TEXT

Reply

Gyan Prakash

May 6, 2020

really helpful

Reply

Karthikeyan Udayakumar

May 6, 2020

Words not visible please enlarge and put video …thanks 🙏🏻

Reply

Barb OBrien

May 6, 2020

Hello and thanks for the video. It would be very helpful if you added Closed Captions since the sound quality is not clear. Thanks.

Reply

Raja sekhar

May 6, 2020

Hi…sir I have one question how to get particular date files and sum of all sizes?

I was trying like this

ls -ltr | grep -i "Jun 15" | awk -F" " begin{sum=0} { sum+=$5}end {print sum} it displayed nothing then we were added du command where the output of awk command it was showing total disk size not for particular date files,

Is there anything wrong with awk and du combination like pipe of between them, can you suggest me how to get the output of above requirement

Reply

BRAJESH PATEL

May 6, 2020

Thanks a lot for sharing knowledge…

Reply

Leave a Reply