Format Command Output In Linux
The short answer
In Unix-like operating systems such as Linux and macOS, you can scan patterns and process the output of commands using the [.inline-code]awk[.inline-code] command as follows:
Where:
- [.inline-code]<command>[.inline-code] is the command whose output you want to process or filter.
- [.inline-code]<pattern>[.inline-code] is a text string or a regex pattern used to filter the output of the specified command.
- [.inline-code]<action>[.inline-code] is the action to perform on the lines matching the specified pattern.
Alternatively, you can also pass one or more file paths as arguments:
For example, the following command will print to the standard output all the lines of the [.inline-code]server.log[.inline-code] file containing the [.inline-code]error[.inline-code] string:
And the following command will print to the standard output all the lines of the [.inline-code]server.log[.inline-code] file starting with the [.inline-code]2024-05[.inline-code] string:
[#split-the-output-into-columns] Splitting the output into columns [#split-the-output-into-columns]
By default, the [.inline-code]awk[.inline-code] command splits every line of its input into multiple columns, using the space character as a delimiter.
To print one or more specific columns instead of the entire line, you can use the following syntax:
Where:
- [.inline-code]$<column>[,$<column>][.inline-code] is a list of comma-separated column numbers prefixed with a dollar sign ([.inline-code]$[.inline-code]).
For example, this command will only print the first column of the output generated by the [.inline-code]ps[.inline-code] command:
And this command will print the first and fourth columns of the output generated by the [.inline-code]ps[.inline-code] command:
Note that you can quickly reference the last column using the pre-defined [.inline-code]$NF[.inline-code] variable as follows:
[#use-custom-column-delimiters] Using custom delimiters [#use-custom-column-delimiters]
To split the output of commands into columns based a specific delimiter instead of the default space character, you can use the [.inline-code]-F[.inline-code] flag as follows:
Alternatively, you can specify multiple delimiters at once by encapsulating them in square brackets as follows:
For example, considering the following [.inline-code]data.csv[.inline-code] file:
The following command will use the [.inline-code],[.inline-code] character to split each line of the [.inline-code]data.csv[.inline-code] file into separate columns and only output the third column:
Summing up and counting columns
[#count-pattern-occurrences] Counting the occurrences of a pattern [#count-pattern-occurrences]
To count the occurrences of a specified pattern with [.inline-code]awk[.inline-code], you can increment a variable using the [.inline-code]++[.inline-code] operator as follows:
For example, considering the following [.inline-code]server.log[.inline-code] file:
This command will increment the [.inline-code]count[.inline-code] variable every time the [.inline-code]"ERROR"[.inline-code] string is found in the [.inline-code]server.log[.inline-code] file:
[#sum-up-column-values] Summing up the data of columns [#sum-up-column-values]
To add up the values of a column and print their sum with [.inline-code]awk[.inline-code], you can use the following syntax:
Where:
- [.inline-code]<variable>[.inline-code] is the name of the variable the sum of the specified column will be accumulated in.
- [.inline-code]<column>[.inline-code] is the column number to add up.
- [.inline-code]END[.inline-code] is used to specify a command to execute once all the lines are processed.
For example, considering the following [.inline-code]data.csv[.inline-code] file:
This command will print the sum of the values of the second column:
Where:
- [.inline-code]cat data.csv[.inline-code] is used to output the file's content.
- [.inline-code]tail -n +2[.inline-code] is used to remove the first line of the output.
- [.inline-code]awk -F ','[.inline-code] is used to split each line of the output into columns using [.inline-code],[.inline-code] as a separator.
- [.inline-code]{sum += $2}[.inline-code] is used to add the value of the second column to the [.inline-code]sum[.inline-code] variable.
- [.inline-code]END {print sum}[.inline-code] is used to print the [.inline-code]sum[.inline-code] variable.
Manipulating text
[#extracting-a-substring] Extracting a substring [#extracting-a-substring]
To extract a substring from a line with [.inline-code]awk[.inline-code], you can combine the [.inline-code]print[.inline-code] and [.inline-code]substr[.inline-code] functions as follows:
Where:
- [.inline-code]<column>[.inline-code] is the column number of the string you want to extract a substring from. Note that the full input string can be accessed through the column number [.inline-code]0[.inline-code].
- [.inline-code]<start>[.inline-code] is a number representing the starting position within the specified string.
- [.inline-code]<length>[.inline-code] is a number representing the length of the substring.
For example, this command will take as input the entire [.inline-code]"Hello World"[.inline-code] string and print [.inline-code]5[.inline-code] characters starting at position [.inline-code]7[.inline-code]:
And this command will take as input the second column and print [.inline-code]5[.inline-code] characters starting at position [.inline-code]0[.inline-code]:
[#replace-pattern-occurrences] Replacing the occurrences of a string or pattern [#replace-pattern-occurrences]
To replace all the occurrences of a string or pattern with [.inline-code]awk[.inline-code], you can use the [.inline-code]gsub[.inline-code] function as follows:
Where:
- [.inline-code]<pattern>[.inline-code] is the string or pattern you want to replace.
- [.inline-code]<string>[.inline-code] is the string you want to replace the matched [.inline-code]pattern[.inline-code] with.
For example, this command will replace all the occurrences of the letter [.inline-code]l[.inline-code] with [.inline-code]x[.inline-code]:
[#easily-recall-syntax-with-ai] Easily retrieve this command using the Warp’s AI Command Suggestions [#easily-recall-syntax-with-ai]
If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:
Entering [.inline-code]awk replace all occurrences[.inline-code] into the AI Command Suggestions will prompt a [.inline-code]awk[.inline-code] command that can then be quickly inserted into your shell by doing [.inline-code]CMD+ENTER[.inline-code].
[#conditional-statements] Conditional statements [#conditional-statements]
To execute an action based on the evaluation of a condition with [.inline-code]awk[.inline-code], you can use the following syntax:
For example, let's consider the following [.inline-code]students.txt[.inline-code] file:
This command will check if the value of the second column is greater or equal to [.inline-code]40[.inline-code] and print the first column with [.inline-code]“Passed”[.inline-code] if true, or [.inline-code]“Failed”[.inline-code] otherwise: