Skip to content

Instantly share code, notes, and snippets.

@HamidMolareza
Last active November 19, 2023 07:38
Show Gist options
  • Save HamidMolareza/316a9b8d44835dc62aab306820d2c84e to your computer and use it in GitHub Desktop.
Save HamidMolareza/316a9b8d44835dc62aab306820d2c84e to your computer and use it in GitHub Desktop.
The script is a Bash program that reads and displays lines from a text file based on user-defined filters such as displaying the first N lines, last N lines, or a range of lines, with an added help message for usage guidance.
#!/bin/bash
# Function to display help information
show_help() {
echo "Usage:"
echo " $(basename "$0") [OPTIONS] FILE"
echo " pipeline_command | $(basename "$0") [OPTIONS]"
echo "Reads lines from a file based on the specified filter."
echo ""
echo "Options:"
echo " +N Display the first N lines of the file"
echo " -N Display the last N lines of the file"
echo " M-N Display lines M through N of the file"
echo " M- Display lines from M to end of the file"
echo ""
echo "If no filter is provided, the entire file will be displayed."
echo ""
echo "Examples:"
echo " $(basename "$0") file.txt # Display entire file"
echo " $(basename "$0") +10 file.txt # Display first 10 lines"
echo " $(basename "$0") -20 file.txt # Display last 20 lines"
echo " $(basename "$0") 5-15 file.txt # Display lines 5 through 15"
echo " $(basename "$0") 15- file.txt # Display lines 15-end"
echo " ls -lah | $(basename "$0") +5"
echo ""
exit 0
}
# Function to run a command, either with a file or piped content
run_command() {
local is_pipe=$1
local file_or_content="$2"
local command="$3"
if [ "$is_pipe" = false ]; then
# Run the command with file content
$command "$file_or_content"
else
# Run the command with piped content
echo "$file_or_content" | $command
fi
}
# Function to read lines based on user input
read_lines() {
local is_pipe=$1
local file_or_content="$2"
local filter="$3"
if [ "$is_pipe" = false ]; then
# If not piped input, check if the file is provided
if [ -z "$file_or_content" ]; then
show_help
fi
# Check if file exists
if [ ! -f "$file_or_content" ]; then
echo "Error! The file ($file_or_content) does not exist."
show_help
fi
fi
# Check filter conditions and apply the appropriate command
if [ -z "$filter" ]; then
run_command "$is_pipe" "$file_or_content" "less"
elif [[ $filter =~ ^[+]?([0-9]+)$ ]]; then
parsed_number="${BASH_REMATCH[1]}"
run_command "$is_pipe" "$file_or_content" "head -n $parsed_number"
elif [[ $filter =~ ^-([0-9]+) ]]; then
parsed_number="${BASH_REMATCH[1]}"
run_command "$is_pipe" "$file_or_content" "tail -n $parsed_number"
elif [[ $filter =~ ([0-9]+)-([0-9]+)? ]]; then
start_number="${BASH_REMATCH[1]}"
end_number="${BASH_REMATCH[2]:-$}"
run_command "$is_pipe" "$file_or_content" "sed -n ${start_number},${end_number}p"
else
# Display help if no valid options are provided
show_help
fi
}
# Check if input is piped
if [ -p /dev/stdin ]; then
is_pipe=true
content=$(cat)
read_lines $is_pipe "$content" "$@"
else
is_pipe=false
# Check the number of arguments and call read_lines accordingly
if [ $# = 1 ]; then
read_lines $is_pipe "$1"
elif [ $# = 2 ]; then
read_lines $is_pipe "$2" "$1"
else
show_help
fi
fi
# LICENSE GPLv3
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# <https://www.gnu.org/licenses/>
@HamidMolareza
Copy link
Author

HamidMolareza commented Nov 19, 2023

How to Use the Script:

  1. To ensure convenience and accessibility, designate a directory for your scripts. Let's assume the desired directory is path.

  2. Download the script into the path directory and add the directory to your system PATH variable using the following command:
    curl https://gist.githubusercontent.com/HamidMolareza/316a9b8d44835dc62aab306820d2c84e/raw/21bc89d64b2f482ceaa65c3ea0825920ed784290/read-lines.sh -o path/read-lines; chmod +x path/read-lines; echo 'export PATH="$PATH:path"' >> ~/.bashrc && source ~/.bashrc

Note: Please don't forget to put your desired directory instead of path.

  1. Now you can use this script everywhere, for example:
    read-lines 20-30 file.txt
    ls -lah | read-lines 20-30

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment