Skip to content

Instantly share code, notes, and snippets.

@bijoy26
Last active September 29, 2022 17:41
Show Gist options
  • Save bijoy26/26f97fb9981503edecc2e967b9d4fa5d to your computer and use it in GitHub Desktop.
Save bijoy26/26f97fb9981503edecc2e967b9d4fa5d to your computer and use it in GitHub Desktop.
A text formatter script that generates a list of command-comment pairs based on line numbers

Pair_lister.sh Overview

pair_lister.sh is a mini bash script that generates a list of command-comment pairs based on line numbers.

Background

Generally, a cheat-sheet contains a set of notes for quick reference mostly consisting of command-comment pairs with some additional descriptions.

  • Sometimes you may prefer creating your own cheat-sheets while learning a specific technology from online sources such as PDFs, blogs or contents.

  • Catch is, the text becomes really messy when the markup data is copied from a browser page and pasted on a notepad. It requires a bit of text processing to make it look nice.

  • So, the challenge is to organize that unformatted data into command-comment pairs i.e. something visually pleasant.

💡 This is where Pair Lister script comes in.

Scenario

Suppose, you're learning file expressions in Bash (courtesy of @bobbyiliev) and you have collected this data 👇

True if file exists.
[[ -a ${file} ]]
True if file exists and is a block special file.
[[ -b ${file} ]]
True if file exists and is a character special file.
[[ -c ${file} ]]
True if file exists and is a directory.
[[ -d ${file} ]]

Pair Lister turns it into following format 👇

[[ -a ${file} ]]	# True if file exists.
[[ -b ${file} ]]	# True if file exists and is a block special file.
[[ -c ${file} ]]	# True if file exists and is a character special file.
[[ -d ${file} ]]	# True if file exists and is a directory.

Usage

  1. Download the script.

  2. Set Variables

    • Place your raw data in a text file in the same directory and add its filename in the DATA variable. Ex: DATA="myfile.txt".

      ⚠ Remember that the odd lines should contain comments and even lines contain corresponding commands

    • Alternatively, for a dry run, make sure the DATA="samle_data.txt" variable is unmodified.

  3. Make the script executable. $ chmod 755 pair_lister.sh

  4. Run it. $ ./pair_lister.sh

  5. View formatted output $ cat new_data.txt

👀 Feeling adventurous? You can get your desired output pattern by tweaking line 42 (printf) according to your need.

#!/bin/bash
#####################################
# File: pair_lister.sh
# Description: Generate a list of command-comment pairs from text data where
# odd lines contain comments and even lines contain corresponding commands
# Author: Anjum Rashid (@bijoy26)
# Created: Tuesday, 27 September 2022
# Last Modified: Wednesday, 28 September 2022
#####################################
# Input your data filename on DATA variable
DATA="sample_data.txt"
PROCESSED="new_data.txt"
#########################################################
# Cleanup old data (if exists) & create a fresh output file
function cleanup() {
if [[ -e "${PROCESSED}" ]]; then
rm ${PROCESSED} && echo "LOG | Old data cleaned up"
fi
touch ${PROCESSED} && echo "LOG | Output file (${PROCESSED}) created"
}
#########################################################
# fetch comments from odd lines and append to the commands in corresponding even lines
function format() {
TEMP=""
LINE=1
while read -r CURRENT; do
if [[ $((LINE % 2)) == 1 ]]; then
TEMP=${CURRENT}
else
CURRENT=$(echo -e ${CURRENT}) # remove trailing newlines (\n)
TEMP=$(echo -e ${TEMP})
printf "%s\t# %s\n" "${CURRENT}" "${TEMP}" >>new_data.txt
fi
((LINE++))
done <"${DATA}"
echo "LOG | Processing complete. ${PROCESSED} contains formatted data."
}
cleanup
format
True if file exists.
[[ -a ${file} ]]
True if file exists and is a block special file.
[[ -b ${file} ]]
True if file exists and is a character special file.
[[ -c ${file} ]]
True if file exists and is a directory.
[[ -d ${file} ]]
True if file exists.
[[ -e ${file} ]]
True if file exists and is a regular file.
[[ -f ${file} ]]
True if file exists and is a symbolic link.
[[ -h ${file} ]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment