Linux Fundamentals 2

Contents

Let’s learn the practical use of pipes and filter.

1
ls -l ~ | grep -v r$ | awk '{print $8}'|sort -nr

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, -v is used to invert the match.
    • $ is used to match the end of the line. ^ is used to match the beginning of the line.
  • awk '{print $8}': Prints the 8th column of the output.

    • awk is 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:

1
2
3
4
Name, Age, Address
Alice, 25, New York
Bob, 30, Los Angeles
Charlie, 22, Chicago

To print the first column of the file, you can use the following awk command:

1
awk -F, '{print $1}' file.txt
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.

Related Content

0%