Skip to content

Instantly share code, notes, and snippets.

@justincjahn
Created May 4, 2011 15:20
Show Gist options
  • Save justincjahn/955399 to your computer and use it in GitHub Desktop.
Save justincjahn/955399 to your computer and use it in GitHub Desktop.
Script to remove lines using sed.
#!/usr/bin/env bash
#
# Very simple shell script that removes a line from a file
# using sed.
#
# Usage: removeline 13 /path/to/file
#
nParams=$#
# Verify that there are more than two parameters
if (( nParams < 2 )); then
echo "Removes a single line from a file."
echo -e "\n\tUsage: removeline number file"
exit 0
fi
# Verify that the first parameter is a valid integer.
if [[ $1 =~ ^[[:digit:]]+\,{0,}[[:digit:]]{0,}$ ]]; then
# Check the file for existence and actually do it
if [ -e $2 ]; then
sed -i '' $1d $2
else
echo "File provided does not exist."
exit 1
fi
else
echo "First argument must contain a line number or range."
exit 1
fi
# Default to a successful exit
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment