[フレーム]

Bash awk - Pattern Scanning and Processing Language


Using the awk Command

The awk command is used for pattern scanning and processing language.

It's useful for handling text files and used for data extraction and reporting.


Basic Usage

The awk command is powerful for text processing.

For example, you can use it to extract specific fields from a file or perform calculations.

All examples below use the example_data.csv file:

id,Created,Amount,Currency,Description,Customer
1,2024年11月01日,100,USD,Payment,John Doe
2,2024年11月02日,200,EUR,Refund,Jane Smith
3,2024年11月03日,150,USD,Purchase,Emily Davis
4,2024年11月04日,175,GBP,Subscription,Michael Brown

To print the first column of a file, use awk -F"," '{print 1ドル}' filename:

Example: Print First Column

awk -F"," '{print 1ドル}' example_data.csv
# Output:
# id
# 1
# 2
# 3
# 4


Options

The awk command has options to change how it works:

  • -F - Set what separates the data fields
  • -v - Set a variable to be used in the script
  • -f - Use a file as the source of the awk program

Field Separator

The -F option allows you to define the field separator for processing data.

This is useful when dealing with CSV files or data with specific delimiters.

Example: Field Separator

awk -F"," '{print 1ドル}' example_data.csv
# Output:
# id
# 1
# 2
# 3
# 4

Assign Variable

The -v option lets you assign a variable to be used within the awk script.

This is helpful for passing external values into the script.

Example: Assign Variable

awk -F"," -v var="Amount:" '{print var, 3ドル}' example_data.csv
# Output:
# Amount: Amount
# Amount: 100
# Amount: 200
# Amount: 150
# Amount: 175

Using awk for Data Manipulation

Awk can perform complex data manipulation tasks.

For example, awk '{sum += 3ドル} END {print sum}' example_data.csv calculates the sum of the Amount column.

Example: Data Manipulation

awk -F"," '{sum += 3ドル} END {print sum}' example_data.csv
# Output:
# 625

Common Errors and Troubleshooting

When using awk, you might encounter errors such as:

  • "awk: syntax error" - Check your command syntax.
  • "awk: cannot open file" - Ensure the file path is correct and accessible.

Debugging tips include using print statements to check variable values and logic flow.



New

W3Schools Adventure App

Coding fundamentals as a game. Bite-sized lessons and challenges.

Ready to start your journey? Your streak is waiting.
+60 XP
W3Schools Adventure tutor
W3Schools Adventure map
×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

FORUM ABOUT ACADEMY
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

AltStyle によって変換されたページ (->オリジナル) /