File manipulation commands such as sed
(stream editor) and awk
(text processing tool) are powerful tools for performing complex operations on text files. This tutorial will guide you through the usage of these commands in a Linux environment.
The basic syntax of the sed
command is as follows:
sed OPTIONS 'COMMAND' FILE
Here, OPTIONS
are various options you can pass to sed
, COMMAND
is the operation you want to perform, and FILE
is the input file.
To replace occurrences of a specific string in a file, use the s
(substitute) command:
sed 's/old_text/new_text/g' input.txt > output.txt
This replaces all occurrences of old_text
with new_text
in input.txt
and writes the result to output.txt
.
To delete lines matching a specific pattern, use the d
(delete) command:
sed '/pattern/d' input.txt > output.txt
This deletes all lines containing the specified pattern
from input.txt
and writes the result to output.txt
.
To add or append text to specific lines, use the a
(append) and i
(insert) commands:
sed '/pattern/a\
new_text' input.txt > output.txt
This appends new_text
after lines containing pattern
.
The basic syntax of the awk
command is as follows:
awk 'PATTERN { COMMAND }' FILE
Here, PATTERN
is a condition that triggers the command, COMMAND
is the action to be performed, and FILE
is the input file.
To print specific columns from a file, use the { print }
statement:
awk '{ print $2, $4 }' input.txt
This prints the second and fourth columns of each line in input.txt
.
To print lines based on a condition, use an if
statement:
awk '{ if ($3 > 50) print $0 }' input.txt
This prints the entire line if the value in the third column is greater than 50.
To calculate the sum or average of a column, use variables and update them for each line:
awk '{ sum += $2 } END { print "Sum:", sum, "Average:", sum/NR }' input.txt
This calculates the sum and average of the values in the second column and prints the result at the end.
sed
and awk
are powerful tools for file manipulation in Linux scripting. Whether you need to perform text substitutions, deletions, or complex text processing, these commands provide efficient solutions. Experiment with the examples provided to master the usage of sed
and awk
in your Linux scripts.