Domain 1Essential ToolsCORE

Text File Manipulation

Essential Linux command-line tools, file system navigation, text manipulation with grep, file permissions, tar archiving, and remote SSH access.

Domain: Domain 1: Understand and use essential tools•Updated: 2026-08-29
#CLI#Essential Tools#File Operations#Permissions#Grep#Tar#SSH

Text File Manipulation

This section covers text processing commands, stream filtering utilities (grep, sed, awk, cut), pagination mechanisms, regular expression parsing, and command output sorting for RHCSA preparation.


Theory Questions

1. How do you show only the first ten lines of a text file?

Use the head command specifying ten lines with the -n option.

  • Command(s): head -n 10 filename (or head filename)
  • Why: -n 10 restricts the output stream to the first 10 lines of the target file. Standard head defaults to 10 lines if no argument is supplied.
  • Gotchas: Do not confuse -n (line count) with -c (byte/character count).
  • Verify: wc -l <(head -n 10 filename)

2. How do you count the number of words in a text file?

Pass the -w option to the wc (word count) command.

  • Command(s): wc -w filename
  • Why: -w counts whitespace-delimited words instead of lines (-l) or character bytes (-c).
  • Gotchas: Running wc without options outputs line, word, and byte counts together, which may break scripts expecting a single integer value.
  • Verify: wc -w filename

3. How do you navigate to the last line of a file while viewing it in less?

Press G (Shift + g) while in interactive mode.

  • Command(s): Press G (Shift + g)
  • Why: G jumps directly to the end of the active file buffer in less.
  • Gotchas: Pressing lowercase g navigates to the very first line of the file instead of the end.
  • Verify: The terminal cursor lands on the final line of the document buffer.

4. How do you filter the first field from /etc/passwd using : as the delimiter?

Extract field 1 using cut with the field and delimiter flags specified.

  • Command(s): cut -d: -f1 /etc/passwd
  • Why: -d: defines the colon character as the field delimiter, and -f1 extracts the first column (the username).
  • Gotchas: Omitting -d: causes cut to fall back to its default tab delimiter, which fails to parse colon-delimited files like /etc/passwd.
  • Verify: cut -d: -f1 /etc/passwd | head -n 5

5. How do you sort ps aux output by CPU utilization in descending order?

Pipe process output into sort targeting column 3 numerically and in reverse order.

  • Command(s): ps aux | sort -k3 -n -r (or ps aux --sort=-%cpu)
  • Why: -k3 targets the %CPU column, -n performs a numerical sort, and -r reverses output so highest values appear first.
  • Gotchas: Omitting -n sorts text lexicographically, causing values like 9.0 to sort above 10.0.
  • Verify: ps aux | sort -k3 -n -r | head -n 5

6. How do you display only lines in /etc/passwd that start with a specific string?

Use grep with the start-of-line anchor (^).

  • Command(s): grep '^specific_text' /etc/passwd
  • Why: The ^ regex anchor binds the search pattern strictly to the beginning of each line.
  • Gotchas: Omitting ^ matches the target string anywhere inside a line rather than strictly at line starts.
  • Verify: grep '^root' /etc/passwd

7. Which regular expression syntax makes the preceding character optional?

Use ? in Extended Regular Expressions (-E) or \? in Basic Regular Expressions.

  • Command(s): grep -E 'colou?r' file or grep 'colou\?r' file
  • Why: The ? quantifier matches zero or one occurrence of the preceding character.
  • Gotchas: Using * matches zero or more occurrences (e.g., matching “colouuur”), whereas ? matches at most one.
  • Verify: grep -E 'colou?r' <<< $'color\ncolour\ncolouur' (matches only color and colour).

8. Which regular expression requires the preceding character to occur at least once?

Use + in Extended Regular Expressions (-E) or \+ in Basic Regular Expressions.

  • Command(s): grep -E 'go+d' file or grep 'go\+d' file
  • Why: The + quantifier matches one or more consecutive occurrences of the preceding element.
  • Gotchas: Confusing + (one or more) with * (zero or more). * will match “gd”, whereas + requires at least one ‘o’.
  • Verify: grep -E 'go+d' <<< $'gd\ngod\ngood' (matches god and good, skips gd).

9. How do you print a specific column from lines matching a pattern using awk?

Combine pattern filtering with field printing actions in awk.

  • Command(s): awk '/pattern/ {print $2}' filename
  • Why: /pattern/ selects matching lines, and {print $2} prints the second whitespace-delimited field.
  • Gotchas: Omitting the enclosing curly braces {} around print generates syntax errors in awk.
  • Verify: awk '/root/ {print $1}' /etc/passwd

10. How do you display lines that do NOT match a given regular expression?

Invert output using the -v flag in grep.

  • Command(s): grep -v 'regex' filename
  • Why: -v instructs grep to invert matching logic and print lines that fail to match the pattern.
  • Gotchas: Forgetting -v outputs matching lines instead of excluding them.
  • Verify: grep -v 'root' /etc/passwd

11. How do you view ps aux output in a scrollable, paginated view?

Pipe standard process output directly into less.

  • Command(s): ps aux | less
  • Why: Piping output to less provides terminal buffer navigation controls (arrow keys, search via /, page jumps).
  • Gotchas: Using more limits backward navigation capabilities compared to less.
  • Verify: ps aux | less (press q to return to shell).

12. How do you output the last five lines of a file?

Execute tail with the line limit option -n 5.

  • Command(s): tail -n 5 filename
  • Why: -n 5 restricts the output stream to the trailing 5 lines of the target file.
  • Gotchas: Running tail without options defaults to displaying 10 lines.
  • Verify: tail -n 5 /etc/passwd

13. How do you count words within a specific file?

Query word counts using wc -w.

  • Command(s): wc -w filename
  • Why: Counts and returns the total number of whitespace-delimited words.
  • Gotchas: Ensure target path points to a valid text file to avoid binary stream errors.
  • Verify: wc -w /etc/hosts

14. How do you interrupt and stop continuous output monitoring in tail -f?

Send an interrupt signal to the foreground job by pressing Ctrl + C.

  • Command(s): Press Ctrl + C
  • Why: Ctrl + C sends SIGINT (Signal Interrupt) to end the running process and return control to the shell prompt.
  • Gotchas: Pressing Ctrl + Z sends SIGTSTP, suspending the command in the background rather than terminating it.
  • Verify: The terminal returns to an active command prompt.

15. How do you exclude lines starting with # or ; using grep?

Filter comments out using an inverted regex search matching leading symbol characters.

  • Command(s): grep -v '^[#;]' filename
  • Why: ^ anchors to line starts, [#;] defines a character set containing # or ;, and -v suppresses matches.
  • Gotchas: Omitting character set brackets [] and writing ^#|; without -E matches literal strings instead of pattern choices.
  • Verify: grep -v '^[#;]' /etc/nsswitch.conf

16. How do you match one or more occurrences of a character using Extended Regular Expressions?

Use the + quantifier paired with grep -E.

  • Command(s): grep -E 'a+' filename
  • Why: grep -E enables Extended Regular Expressions, interpreting + as matching one or more preceding characters.
  • Gotchas: Using standard grep without -E treats + as a literal character unless escaped as \+.
  • Verify: grep -E 'a+' filename

17. How do you search for text patterns regardless of character capitalization?

Enable case-insensitive matching with the -i flag.

  • Command(s): grep -i 'text' filename
  • Why: -i instructs grep to ignore uppercase and lowercase distinctions when processing patterns.
  • Gotchas: Writing complex set patterns like [tT][eE][xX][tT] adds unnecessary complexity when -i handles all permutations automatically.
  • Verify: grep -i 'PATH' /etc/environment

18. How do you display matching pattern lines along with five preceding context lines?

Pass the context flag -B 5 to grep.

  • Command(s): grep -B 5 '^PATH' filename
  • Why: -B 5 (Before) includes the 5 lines preceding every matched pattern in the output stream.
  • Gotchas: Using -A (After) outputs trailing context lines, while -C (Context) outputs lines both before and after.
  • Verify: grep -B 5 '^PATH' /etc/profile

19. How do you display a single specific line number using sed?

Suppress default output printing using -n and append the p action to the target line number.

  • Command(s): sed -n '5p' filename
  • Why: -n suppresses automatic output printing, and 5p explicitly prints line number 5.
  • Gotchas: Omitting -n prints the entire file buffer while outputting line 5 twice.
  • Verify: sed -n '5p' /etc/passwd

20. How do you globally replace all instances of a word in a file using sed?

Apply global replacement s/old/new/g paired with the in-place file edit flag -i.

  • Command(s): sed -i 's/old_word/new_word/g' filename
  • Why: s executes substitution, g replaces all instances per line, and -i writes modifications directly to disk.
  • Gotchas: Omitting g alters only the first matched instance per line. Omitting -i prints modifications to standard output without updating the file on disk.
  • Verify: grep 'new_word' filename

Lab Tasks

Text Processing Practice

Perform advanced stream parsing, regular expression matching, and sed text manipulation workflows.

Configuration Steps

# Task 1: Method 1 - Display line 5 using sed stream suppression
sed -n '5p' /etc/passwd

# Task 1: Method 2 - Display line 5 using pipeline head and tail filters
head -n 5 /etc/passwd | tail -n 1

# Task 2: Search for text files containing active IP address using fixed string matching
MYIP=$(hostname -I | awk '{print $1}')
grep -rlF "$MYIP" /etc/ --include="*.conf" 2>/dev/null

# Task 3: Revert sed global substitution from "root" back to "Administrator"
sed -i 's/root/Administrator/g' /path/to/targetfile

# Task 4: Sort processes by memory utilization in descending order
ps aux --sort=-%mem

# Task 5: Extract column 6 (VSZ memory allocation) from ps output
ps aux | awk '{print $6}'

# Task 6: Delete line 6 from file in-place
sed -i '6d' ~/myfile
  • Why:

  • sed -n '5p' directly prints line index 5. The pipeline head -n 5 | tail -n 1 isolates line 5 by grabbing the first 5 lines and filtering the tail.

  • -F treats patterns as fixed literal strings. Regular expressions are not needed here because dots in IP addresses (.) represent wildcard characters in regex unless escaped. Fixed string search avoided unnecessary regex parsing overhead.

  • ps aux --sort=-%mem uses internal sorting flags to group highest memory processes at the top.

  • awk '{print $6}' handles variable whitespace padding across process list outputs cleanly compared to cut.

  • sed -i '6d' targets line index 6 and deletes (d) it in-place.

  • Gotchas:

  • Using cut -d' ' on ps aux fails because multiple spaces are used to align columns, causing offset errors.

  • Running sed -i 's/root/Administrator/g' without restricting the target path can cause catastrophic configuration corruption if executed on system identity files like /etc/passwd.

Verification Procedures

Confirm execution accuracy across output filters and modified file contents.

# Verify line extraction match consistency
diff <(sed -n '5p' /etc/passwd) <(head -n 5 /etc/passwd | tail -n 1)

# Inspect reverted text substitution results
grep 'Administrator' /path/to/targetfile

# Verify line deletion result from target file
sed -n '6p' ~/myfile