Technical skill

Linux commands group: text processors

Subject: .../LINUX

Turn lines of text into useful answers

A text file is often a list of lines: a log, a report, or a set of records. Small command-line tools can select lines, change text, sort results, and count repetitions. grep keeps lines that match a pattern. sed applies editing commands to text. awk reads each line as a record with fields. sort orders lines, and uniq combines adjacent duplicates. Standard input is what a command reads, and standard output is what it prints; these let one tool pass results to another.

Suppose a report has a service name followed by an HTTP status code, and you want to count server errors. This pipeline prints the name for each status of 500 or above, sorts the names together, then counts them:

printf '%s\n' 'api 200' 'web 503' 'api 503' 'web 503' |
  awk '$2 >= 500 { print $1 }' |
  sort |
  uniq -c

The awk rule checks the second field and prints the first. sort places identical names next to one another so uniq -c can count them. Before using this on a real report, verify that fields are separated the way the command expects. A different delimiter or text containing spaces may need a different parser.

Quote patterns and check results

A shell variable is a named value. If it contains search text, quote it so the shell passes it unchanged. GNU grep -F -- "$needle" file.log treats the value as fixed text: -F turns off regular-expression rules, and -- tells grep that later words are not options. grep returns status 0 when it finds a match, 1 when it finds none, and 2 for an error. A script should handle “no matches” separately from a failed search. By default, a shell pipeline reports the status of its last command. Bash's set -o pipefail makes a failure in an earlier command affect the pipeline result too.

sed 's/old/new/g' input.txt writes the changed text to standard output. Redirect it to a new file when you want to inspect the result before replacing the original. GNU sed -i edits in place. Use it only when that change is intended and a backup or version-controlled copy is available; a broad substitution may change configuration values on other lines.

Use a parser for structured files

CSV fields can contain commas inside quotation marks, so splitting on every comma with awk -F, can break valid data. JSON includes nesting and escape rules that regular expressions cannot reliably parse. Use a tool that understands the file format when those structures matter. Large input can also change performance: sort may need temporary disk space, and some filters keep data in memory before writing results. Keeping the source file unchanged while checking intermediate output makes a mistaken pattern or delimiter easier to spot.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Linux for backend developer preview
image descriptionLinux for backend developer
by Artem Naumenko (fb)
16 users
Skillset Skyeng SRE preview
image descriptionSkyeng SRE
by Yuliya Marochkina
12 users
Skillset Skyeng Senior PlatOps preview
image descriptionSkyeng Senior PlatOps
by Artem Naumenko (slack2)
7 users
Skillset Skyeng Ops (for DevOps) preview
image descriptionSkyeng Ops (for DevOps)
by Roman Burnashev
2 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful