Skip to content

Instantly share code, notes, and snippets.

@neuralpain
Last active June 10, 2024 11:48
Show Gist options
  • Save neuralpain/462d10143aa6743f0fa20de3f52f7ac0 to your computer and use it in GitHub Desktop.
Save neuralpain/462d10143aa6743f0fa20de3f52f7ac0 to your computer and use it in GitHub Desktop.
Useful bash snipetts
re='^[0-9]+$'
if ![[ $yournumber =~ $re ]]; then
echo "error: Not a number" >&2
fi
# to use the default "#?" as the indicator for
# input, remove the PS3 line
PS3="File: "
select FILENAME in *; do
case $FILENAME in
*) echo "You picked $FILENAME"
esac
done
ARGS=$@
LIST=${ARGS[@]/"$1"}
for i in ${LIST[@]}; do
echo ${LIST[@]}
done
for i in ${LIST[@]}; do
echo $i >> LIST.txt
done
seq 1 1000000 | while read i; do echo -en "\r$i"; done
# from man echo:
# -n do not output the trailing newline
# -e enable interpretation of backslash escapes
# \r carriage return
# replacing a line in a file.
# https://stackoverflow.com/questions/11145270/how-to-replace-an-entire-line-in-a-text-file-by-line-number
FILE='replace_in_file.txt'
echo "Lorem ipsum dolor sit amet," > $FILE
echo "consectetur adipiscing elit." >> $FILE
echo "Duis eu diam non tortor laoreet" >> $FILE
echo "bibendum vitae et tellus." >> $FILE
sed -e '2s/.*/new text/' -i $FILE
# ^
# where N should be replaced by your target line number.
# This replaces the line in the original file.
# To save the changed text in a different file, drop the -i option:
# sed -e '2s/.*/new text/' $FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment