Rakesh Jain Profile picture
Sep 2 β€’ 31 tweets β€’ 5 min read Twitter logo Read on Twitter
Mastering Linux AWK command!

A Powerful Text-Processing Tool!

A Thread with 15 important examples of awk for Linux admins or DevOps EngineersπŸ‘‡ Image
1/4 - What is Awk? 🧡

awk is a powerful text-processing tool and programming language. It's designed for working with structured text data like CSV files, log files, and more.
2/4 - How Does it Work?

Awk operates on a line-by-line basis, reading input data and applying user-defined actions to lines that match specified patterns. This makes it great for filtering, transforming, and extracting data.
3/4 - Key Features 🧡

Pattern-Action Model:
Awk uses a pattern-action model. When a line matches a pattern, a specified action is executed. This allows for conditional processing.
Fields:
Awk treats each line as a collection of fields, which can be accessed and manipulated easily. Fields are often separated by whitespace or custom delimiters.
Variables:
Awk supports variables and provides built-in variables like NR (line number), NF (number of fields), and more for data manipulation.

Functions:
It has a rich set of built-in functions for text manipulation, arithmetic, and string handling.
4/4 - Use Cases

Awk is incredibly versatile and handy for various tasks:

- Data extraction and reporting
- Data cleaning and transformation
- Log file analysis
- CSV/TSV processing
- Text formatting and reformatting
Here are 15 important examples of awk commands with proper explanation πŸ‘‡
1/15 - Disk Space Usage 🧡
Use df to list disk usage, and awk to extract the percentage of used space:

df -h | awk '$NF == "/" {print "Usage:", $5}'
# Extracts disk usage percentage for the root file system
2/15 - List Running Processes 🧡
List top CPU-consuming processes using ps and awk:

ps aux | awk '{if ($3 >= 10) print $0}'
# Lists processes using more than 10% CPU
3/15 - Users and Login Times 🧡
Check who is logged in and their login times with who and awk:

who | awk '{print "User:", $1, "Logged in at:", $3, $4}'
# Displays logged-in users and login times
4/15 - Find Large Files 🧡
Find large files in a directory with find and awk:

find /path/to/dir -type f -exec ls -lh {} + | awk '$5 >= 100M {print $5, $9}'
# Lists files larger than 100MB
5/15 - Process Monitoring 🧡
Monitor CPU and memory usage of a specific process with top and awk:

top -p PID -n 1 | awk '/PID/{getline; print "CPU:", $9, "MEM:", $10}'
# Monitors CPU and memory usage of a process by PID
6/15 - User Account Summary 🧡
Generate a summary of user accounts with awk and /etc/passwd:

awk -F':' '{ print "User:", $1, "Home:", $6 }' /etc/passwd
# Lists user accounts and their home directories
7/15 - Log File Analysis 🧡
Analyze log files and extract specific information:

awk '/error/ {print $0}' /var/log/syslog
# Extracts lines containing "error" from syslog
8/15 - Network Interface Info 🧡
Get network interface information using ifconfig and awk:

ifconfig | awk '/inet addr/{print "Interface:", $1, "IP:", $2}'
# Displays network interfaces and their IP addresses
9/15 - Service Status 🧡
Check the status of a service with systemctl and awk:

systemctl is-active service-name | awk '{print "Service Status:", $0}'
# Checks if a service is active or not
10/15 - System Uptime 🧡
Display system uptime using uptime and awk:

uptime | awk '{print "Uptime:", $3, $4}'
# Shows system uptime
11/15 - User Resource Usage 🧡
View resource usage of a specific user with top and awk:

top -b -n 1 -u username | awk '/username/{print "CPU:", $9, "MEM:", $10}'
# Monitors CPU and memory usage of a specific user
12/15 - Check Memory Usage 🧡
Check memory usage using free and awk:

free -m | awk '/Mem/{print "Total Memory:", $2 "MB", "Used Memory:", $3 "MB"}'
# Displays total and used memory
13/15 - List Open Ports 🧡
List open ports using netstat and awk:

netstat -tuln | awk '/LISTEN/{print "Port:", $4}'
# Lists open ports in listening state
14/15 - Find Largest Directories 🧡
Find the largest directories using du, sort, and awk:

du -h /path | sort -rh | awk 'NR<=5{print $2, $1}'
# Lists top 5 largest directories
15/15 - Backup and Rename Files 🧡
Create backups of files and append a timestamp using cp, awk, and date:

cp filename.txt filename_$(date +%Y%m%d%H%M%S).bak
# Creates a backup file with a timestamp
Time for Bonus πŸ’°πŸ˜ Image
16/20 - Check CPU Information 🧡
Retrieve detailed CPU information from /proc/cpuinfo with awk:

awk -F: '/model name/ {print "CPU Model:", $2}' /proc/cpuinfo
# Displays CPU model information
17/20 - Monitor Disk I/O 🧡
Monitor disk I/O activity in real-time using iostat and awk:

iostat -x 1 | awk '$1=="sda" {print "Read:", $6 "KB/s, Write:", $7 "KB/s"}'
# Monitors disk I/O for a specific disk (e.g., sda)
18/20 - List Active Network Connections 🧡
List active network connections and their states with ss and awk:

ss -tuln | awk 'NR>1 {print "Protocol:", $1, "State:", $2, "Local Address:", $4}'
# Lists active network connections and their states
19/20 - Check System Load 🧡
Monitor system load averages using uptime and awk:

uptime | awk -F'[a-z]:' '{print "Load Average (1/5/15 min):", $2, $3, $4}'
# Displays system load averages
20/20 - System Performance Snapshot 🧡
Take a snapshot of CPU and memory usage with top and awk:

top -b -n 1 | awk '/Cpu/{print "CPU Usage:", $2 "%"}; /KiB Mem/{print "Memory Usage:", $8}'
# Captures CPU and memory usage snapshot
These awk examples are essential for Linux system administration tasks and can help you efficiently manage and monitor your system. πŸ§πŸ” #Linux #SystemAdmin #AwkCommands
Repost the thread if you find it useful. Thanks!

β€’ β€’ β€’

Missing some Tweet in this thread? You can try to force a refresh
γ€€

Keep Current with Rakesh Jain

Rakesh Jain Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @devops_tech

Sep 4
Simple Examples of 25 Essential Linux Commands for DevOps Pros!" πŸ”πŸš€

A Quick Glance!πŸ‘‡

#DevOps #LinuxCommands πŸ”₯🐧 Image
#1: ssh πŸš€

# Securely access remote servers
ssh user@server
#2: scp/rsync πŸ“

#Copy files and directories between local and remote systems or between remote systems.

scp local_file username@remote_server:/path/to/destination

rsync -avz local_dir/ username@remote_server:/path/to/destination/
Read 28 tweets
Aug 31
SAML vs. AD vs. LDAP!

A Thread πŸ‘‡ Image
1/ Hey, everyone! 🌟 Let's unravel the differences between SAML, AD (Active Directory), and LDAP (Lightweight Directory Access Protocol).

Buckle up for a quick rundown! πŸ”πŸ”’ #SAMLvsADvsLDAP

#Authentication #infosec
2/ πŸ”‘ SAML: Security Assertion Markup Language, a protocol for enabling Single Sign-On (SSO) across web apps.
AD: Active Directory, a Microsoft identity management solution.
LDAP: Lightweight Directory Access Protocol, a protocol for accessing directory services.

#SAMLvsADvsLDAP
Read 17 tweets
Aug 29
Mastering Linux find command!

A thread with 15 practical Linux find command examples with explanations πŸ‘‡ Image
What is Linux find command?

The Linux find command is used to search for files and directories based on specified criteria such as file names, sizes, permissions, modification times, and more within a file system.

πŸ§πŸ” #LinuxCommands #Find #DevOps
Let's grasp its usage through examples πŸ‘‡ Image
Read 26 tweets
Jul 30
Mastering Docker File 🐳

A thread explaining everything about a Docker File πŸ‘‡ Image
πŸ“Œ Welcome to a thread on mastering Dockerfile! πŸš€ In this thread, I'll explain each parameter with an example of a Dockerfile.

Dockerfiles are key to building Docker images, so let's dive in! #Docker #Dockerfile #Containerization Image
1️⃣ FROM: This sets the base image for your Docker image. It's like the starting point for your container. For example:

FROM ubuntu:20.04

This uses the official Ubuntu 20.04 image as the base.
Read 24 tweets
Jul 29
Mastering kubectl! πŸŒβš™οΈ

A thread explaining 20 imp kubectl commands with brief explanations πŸ‘‡ Image
What is kubectl?

kubectl is a command-line tool used for interacting with Kubernetes clusters.

It serves as the primary interface for administrators, developers, and DevOps engineers to interact with Kubernetes clusters. Image
Here are 20 kubectl commands with brief explanations πŸ‘‡ Image
Read 25 tweets
Jul 28
Mastering SQL!

A Thread 🐦 with 20 Crucial SQL Commands with Explanations πŸ‘‡ Image
SELECT πŸ“₯ Select data from a database table. It retrieves rows that match specified conditions.

Example:

"SELECT column1, column2 FROM table_name WHERE condition;"
INSERT πŸ“€ Add new rows to a table.

Use "INSERT INTO table_name (column1, column2) VALUES (value1, value2);" to add data.
Read 29 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(