Saturday, October 17, 2026

Unix Pipes and grep

 

Unix  Pipes and grep

The objective is to use pipes (|), grep, find, wc, uniq, and output redirection to solve the exercises and save both the commands and their outputs in lab5_q2.txt.

(a) What does who | grep A20\* do?

who | grep A20\*
  • who displays users currently logged in.

  • | sends the output of who to grep.

  • grep A20\* searches for lines matching the pattern A20*.

Important: In basic grep regular expressions, \* means zero or more occurrences of the preceding character, 0. Therefore, this pattern matches A2 followed by zero or more zeros, such as A2, A20, or A200. It does not mean “any text starting with A20.”

If the intention is to find lines containing text beginning with A20, use:

who | grep 'A20'

If the intention is to match a username beginning with A20, use:

who | grep '^A20'

(b) Count files under /usr/include whose names start with l, contain a number, and end with .h

find /usr/include -type f -name 'l*[0-9]*.h' | wc -l

Explanation:

  • find /usr/include searches recursively.

  • -type f selects regular files.

  • -name 'l*[0-9]*.h' matches filenames beginning with l, containing at least one digit, and ending with .h.

  • wc -l counts the matching file paths.

(c) Find French F1 drivers from 2017 and 2018, using uniq

Assuming each line contains the driver's nationality and the driver name appears first, try:

grep -i 'French' /home/lab_files/F1_drivers_2017 /home/lab_files/F1_drivers_2018 | sort | uniq

This searches both files, selects lines containing French, sorts the results, and removes identical duplicate lines.

Note: uniq removes duplicate lines, not duplicate driver names appearing in different team/year records. If the same driver has different records in 2017 and 2018, the file format determines how to extract the driver name alone. You can inspect the data using:

head /home/lab_files/F1_drivers_2017

Save the commands and their outputs in lab5_q2.txt

Run this block in your terminal. It records each command and its output in the requested file.

{
    echo "Part (a): who | grep A20\*"
    who | grep A20\*

    echo
    echo "Part (b): find /usr/include -type f -name 'l*[0-9]*.h' | wc -l"
    find /usr/include -type f -name 'l*[0-9]*.h' | wc -l

    echo
    echo "Part (c): French F1 drivers"
    grep -i 'French' /home/lab_files/F1_drivers_2017 /home/lab_files/F1_drivers_2018 | sort | uniq
} > lab5_q2.txt 2>&1

Verify the saved file:

cat lab5_q2.txt

The output will depend on the files and logged-in users on your lab machine. The block above records the commands' results, including error messages, but does not print the command text automatically beyond the headings. If your instructor requires the literal commands as well as their outputs, use this version instead:

{
    echo 'Command: who | grep A20\*'
    who | grep A20\*

    echo 'Command: find /usr/include -type f -name "l*[0-9]*.h" | wc -l'
    find /usr/include -type f -name 'l*[0-9]*.h' | wc -l

    echo "Command: grep -i 'French' /home/lab_files/F1_drivers_2017 /home/lab_files/F1_drivers_2018 | sort | uniq"
    grep -i 'French' /home/lab_files/F1_drivers_2017 /home/lab_files/F1_drivers_2018 | sort | uniq
} > lab5_q2.txt 2>&1

For the shortest command in part (c): the pipeline above is concise, but to ensure each driver appears only once even when their team details differ, I would need to see the first few lines of the two F1 files to identify the correct driver-name column.

is it 1 or l? find /usr/include -type f -name 'l*[0-9]*.h' | wc -l

It is a lowercase letter l, not the number 1.

find /usr/include -type f -name 'l*[0-9]*.h' | wc -l

In the pattern 'l*[0-9]*.h':

  • l — lowercase letter l, as in lion.

  • * — any sequence of characters (including none).

  • [0-9] — any single digit from 0 to 9.

  • *.h — any sequence of characters followed by .h.

So the filename must start with lowercase l, contain at least one digit, and end with .h.

1. Processes, Pipes, Grep and Email

(a) List all running processes in full format

ps -ef

Explanation:

  • ps — displays process information.

  • -e — selects all processes.

  • -f — displays the full-format listing, including user, PID, parent PID, start time, and command.

(b) Display only the SSH processes using a pipe and text filter

ps -ef | grep 'ssh'

Explanation:

  • ps -ef — lists all processes in full format.

  • | (pipe) — passes the output of ps -ef to the next command.

  • grep 'ssh' — displays only lines containing the text ssh.

Note: This command may also display the grep 'ssh' command itself. To exclude that line, you can use:

ps -ef | grep '[s]sh'

(c) Redirect the SSH process listing to a file

Replace studentID with your actual student ID.

ps -ef | grep '[s]sh' > ssh_list_studentID.txt

For example, if your student ID is student123:

ps -ef | grep '[s]sh' > ssh_list_student123.txt

Explanation:

  • > — redirects the command output to a file.

  • If the file does not exist, it is created.

  • If the file already exists, its previous contents are overwritten.

Summary

Question

Command

(a) All processes in full format

ps -ef

(b) Only SSH-related processes

ps -ef \| grep '[s]sh'

(c) Save results to a file

ps -ef \| grep '[s]sh' > ssh_list_studentID.txt

Important: The filter matches any command line containing ssh, including related commands such as sshd.

(a) Copy mid_semester_revision.txt to your home folder

Run this command in Terminal 1:

cp /home/lab_files/mid_semester_revision.txt ~/

Explanation:

  • cp — copies a file.

  • /home/lab_files/mid_semester_revision.txt — source file path.

  • ~/ — your home directory.

Verify that the file was copied:

ls -l ~/mid_semester_revision.txt

(b) Open a second terminal and answer the questions using VI

Terminal 1

  1. Open the copied file in the VI editor:

    vi ~/mid_semester_revision.txt
  2. Read the questions in the file.

  3. Keep this terminal open while you work.

Terminal 2

  1. Open a second terminal window or tab.

  2. Log in with your student account if required by your lab environment.

  3. Test Linux commands here before using them in Terminal 1.

For example, test:

ps -ef
who
grep --help

Use the appropriate commands for the questions in the revision file.

Useful VI commands

Key/command

Purpose

i

Enter insert mode to type answers

Esc

Return to command mode

:w

Save changes

:wq

Save and exit

:q!

Exit without saving

Important: You should edit the copied file in your home folder, not the original file in /home/lab_files.



No comments:

Post a Comment