Linux Text Formatting

LinuxLinuxBeginner
Practice Now

Introduction

In the decrepit corridors of an abandoned sanatorium, rumored to be haunted by the spirits of patients who once roamed its eerie halls, a legend is whispered of a brilliant, yet terrifying, psychiatrist who once practiced unorthodox treatments within these very walls. Dr. Eklund, known for his eccentric methods and borderline obsession with the human psyche, left behind a cryptic repository of patient records and notes, formatted in a perplexing manner only he could comprehend.

Your mission, should you choose to embrace the thrill, is to uncover the secrets of Dr. Eklund's work by deciphering his files using the power of Linux text formatting. This includes mastering the printf command to transform and extract information from data as ghostly as the sanatorium itself. Will you unravel the mystery or will the formatting horrors of Dr. Eklund's legacy leave you lost in the darkened hallways of confusion?


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux/BasicSystemCommandsGroup -.-> linux/printf("`Text Formatting`") subgraph Lab Skills linux/printf -.-> lab-271361{{"`Linux Text Formatting`"}} end

Discover the Formatting Secrets

In this step, you will delve into the fundamentals of the printf command to format a string that may uncover the first clue to Dr. Eklund's cryptic records. To ensure success, start by creating a psychiatrist_notes.txt file within the ~/project directory and format a string that includes a patient's name, age, and a special code.

Execute the following command in the terminal to create the file:

cd ~/project
touch psychiatrist_notes.txt

Then, using printf, print the formatted string to this file:

printf "Patient: %-10s Age: %-3d Code: %04d\n" "John Doe" 34 75 >> psychiatrist_notes.txt

This printf command uses format specifiers to structure the output:

  • %s for strings, with a left-justified minimum width of 10 characters.
  • %d for integers, with a minimum width of 3 characters.
  • %04d for integers padded with leading zeros to a width of 4 characters.

The result in psychiatrist_notes.txt should resemble:

Patient: John Doe    Age: 34  Code: 0075

Decode the Special Code

Having formatted the patient's details, it's now time to unravel the numerical codes Dr. Eklund used to classify the mental states of his patients. For this step, create a bash script named decoder.sh that will use printf to decipher a code and save it to a new file named decoded.txt.

Begin by writing your script:

cd ~/project
touch decoder.sh
echo 'printf "Decoded State: %b\n" "\x41\x62\x6e\x6f\x72\x6d\x61\x6c\x69\x74\x79" > decoded.txt' > decoder.sh
chmod +x decoder.sh

Then, execute your script:

./decoder.sh

The printf command in the script uses the %b format specifier to convert the escape sequences into ASCII characters. When you look into the decoded.txt, you should see the output as:

Decoded State: Abnormality

Summary

In this lab, we embraced the chilling context of a long-forgotten sanatorium to explore and master the printf tool within Linux for text formatting purposes. Designing the lab around a plot of uncovering a psychiatrist's secret not only engages the imagination but also provides a captivating practical way to learn shell scripting techniques. Through hands-on experience, we've indeed cracked the code and learned to use printf for formatting and decoding strings. The journey through Dr. Eklund's enigmatic legacy leaves us not just with newfound skills but also with a sense of accomplishment and perhaps a spine-tingling thrill.

Other Linux Tutorials you may like