LINUX · Text tools · 8 min

Extract and sort service ports

Read a delimited file and sort numbers numerically.

Not completed

Your task

Write the second colon-separated field from services.txt to ports.txt in ascending numeric order.

What you’re aiming for

  • ports.txt contains 80, 443, and 8080 on separate lines.
  • services.txt is preserved.

1. Prepare your practice folder

Use a Linux terminal, such as Ubuntu or WSL on Windows. These examples use Linux file permissions. In WSL, create your practice folder in your Linux home directory (~).

Create a new, empty folder for this exercise. If that name already exists, choose another new name. Continue only after you’re inside the new folder.

NEW PRACTICE FOLDER
mkdir opsrung-linux-extract-service-ports && cd opsrung-linux-extract-service-ports

Run these setup commands in that folder:

SETUP — RUN ONCE
printf '%s\n' 'api:8080
web:80
secure:443' > services.txt

2. Try the task

Work toward the goal above. Start by inspecting the files or resources, then use a hint if you need one.

Hint 1

cut selects a field using a delimiter.

Hint 2

Numeric sorting differs from text sorting.

Hint 3

Use cut -d: -f2 followed by sort -n.

3. Review your result

Use these commands to look at your result and compare it with the goals. The website does not run or verify them.

REVIEW IN YOUR TERMINAL
cat ports.txt
cat services.txt

What you should see

  • ports.txt contains 80, 443, and 8080 on separate lines.
  • services.txt is preserved.

If your result differs: Compare the actual output with each goal. Keep your work inside this exercise’s folder and use only its named resources.

Compare with one possible solution
ONE POSSIBLE SOLUTION
cut -d: -f2 services.txt | sort -n > ports.txt

Other approaches are fine if you reach the same goal. Opening this solution does not mark the exercise complete.

Finished this exercise?

You decide when you’re done. Your choice is saved in this browser, and you can change it later.