Linux Fundamentals 2
Contents
Let’s learn the practical use of pipes and filter.
|
|
The above command will list all the files in the home directory, exclude the files ending with ‘r’, print the 8th column, and sort the output in reverse order.
Let’s break down the command:
-
ls -l ~: Lists all the files in the home directory with detailed information. -
grep -v r$: Excludes the files ending with ‘r’.- Here,
-vis used to invert the match. $is used to match the end of the line. ^ is used to match the beginning of the line.
- Here,
-
awk '{print $8}': Prints the 8th column of the output.awkis a powerful text processing tool.{print $8}is the action to be performed on the input.
-
sort -nr: Sorts the output in reverse order.
awk
awk is a powerful text processing tool that allows you to manipulate text files. It is particularly useful for processing structured text files, such as CSV files.
file.txt:
|
|
To print the first column of the file, you can use the following awk command:
|
|
| Option | Description |
|---|---|
-F, |
Specifies the field separator as a comma. Default is whitespace. |
{print $n} |
Prints the n-th field. $0 represents the entire line. $1, $2, etc., represent the first, second, etc., fields. |