Last active
June 10, 2024 11:48
-
-
Save neuralpain/462d10143aa6743f0fa20de3f52f7ac0 to your computer and use it in GitHub Desktop.
Useful bash snipetts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
re='^[0-9]+$' | |
if ![[ $yournumber =~ $re ]]; then | |
echo "error: Not a number" >&2 | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ARGS=$@ | |
LIST=${ARGS[@]/"$1"} | |
for i in ${LIST[@]}; do | |
echo ${LIST[@]} | |
done | |
for i in ${LIST[@]}; do | |
echo $i >> LIST.txt | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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