Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for AWK Basics Tutorial
Leapcell
Leapcell

Posted on

     

AWK Basics Tutorial

Image description

Leapcell: The Next-Gen Serverless Platform for Web Hosting, Async Tasks, and Redis

A Concise AWK Tutorial

I. Basic Concepts

AWK is a built-in text processing tool in Linux systems, specializing in handling structured text (e.g., logs, CSV files). It reads files line by line, processes data by fields, and supports simple programming logic.

II. Basic Syntax

1. Fundamental Format

awk[options]'actions' filename
Enter fullscreen modeExit fullscreen mode

2. Simplest Examples

# Print entire file contentawk'{print $0}' demo.txt# Process standard input via pipeecho'this is a test' |awk'{print $0}'
Enter fullscreen modeExit fullscreen mode

3. Field Handling

  • $1: First field
  • $2: Second field
  • $0: Entire line
  • NF: Total number of fields in current line
  • $NF: Last field
# Extract third fieldecho'this is a test' |awk'{print $3}'# Output: a# Extract second-to-last fieldecho'a,b,c,d' |awk-F',''{print $(NF-1)}'# Output: c
Enter fullscreen modeExit fullscreen mode

III. Core Functions

1. Field Separator

# Specify colon as separatorawk-F':''{print $1}' /etc/passwd
Enter fullscreen modeExit fullscreen mode

2. Built-in Variables

Variable NameDescriptionExample
NRCurrent line numberawk '{print NR}' file
FSInput field separator (default space)awk -v FS=: '{print $1}'
OFSOutput field separator (default space)awk -v OFS=, '{print $1,$2}'
FILENAMECurrent file nameawk '{print FILENAME}' file

IV. Advanced Operations

1. Conditional Filtering

# Regular expression match: Print lines containing "usr"awk-F':''/usr/ {print $1}' /etc/passwd# Numeric comparison: Print content after line 3awk-F':''NR > 3 {print $1}' /etc/passwd# Combined conditionsawk-F':''$1 == "root" || $3 > 1000' /etc/passwd
Enter fullscreen modeExit fullscreen mode

2. Built-in Functions

Function NameFunctionExample
toupper()Convert to uppercaseawk '{print toupper($1)}'
length()String lengthawk '{print length($1)}'
substr()Substring extractionawk '{print substr($1,3,5)}'
rand()Generate random numberawk '{print int(rand()*100)}'

V. Control Statements

1. Single-line Conditions

# Process odd-numbered linesawk'NR % 2 == 1 {print "Line", NR}' file# Field comparisonawk-F':''$3 > 1000 {print $1}' /etc/passwd
Enter fullscreen modeExit fullscreen mode

2. Multi-line Logic

awk-F':''{  if ($1 > "m") {    print "High:", $1  } else {    print "Low:", $1  }}' /etc/passwd
Enter fullscreen modeExit fullscreen mode

VI. Practical Tips

  • Formatted Output: Useprintf instead ofprint
awk-F':''{printf "%-10s %s\n", $1, $3}' /etc/passwd
Enter fullscreen modeExit fullscreen mode
  • Large File Handling: Memory-friendly line-by-line processing
  • Tool Integration: Combine withgrep/sed

VII. Quick Reference

# Common command combinationsawk-F':''/^root/ {print $1}'# Lines starting with rootawk-F':''!/nologin/ {print $1}'# Exclude lines containing nologinawk-F':''$3 ~ /[0-9]{4}/'# Match 4-digit fields
Enter fullscreen modeExit fullscreen mode

Optimization Notes:

  1. Hierarchical heading structure
  2. Variable/function tables for clarity
  3. Code block/output result contrast
  4. Practical tips and quick reference added
  5. Learning curve enhanced through logical ordering
  6. Improved readability with proper spacing and indentation

Leapcell: The Next-Gen Serverless Platform for Web Hosting, Async Tasks, and Redis

Finally, I recommend the best platform for deployment:Leapcell

Image description

1. Multi-Language Support

  • Develop with JavaScript, Python, Go, or Rust.

2. Deploy unlimited projects for free

  • Pay only for usage — no requests, no charges.

3. Unbeatable Cost Efficiency

  • Pay-as-you-go with no idle charges.
  • Example: $25 supports 6.94M requests at a 60ms average response time.

4. Streamlined Developer Experience

  • Intuitive UI for effortless setup.
  • Fully automated CI/CD pipelines and GitOps integration.
  • Real-time metrics and logging for actionable insights.

5. Effortless Scalability and High Performance

  • Auto-scaling to handle high concurrency with ease.
  • Zero operational overhead — just focus on building.

Image description

Explore more in the documentation!

Leapcell Twitter:https://x.com/LeapcellHQ

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

leapcell.io: serverless web hosting / async task / redis
  • Location
    California
  • Joined

More fromLeapcell

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp