Skip to content

Instantly share code, notes, and snippets.

@vokiel
Last active February 8, 2017 10:28
Show Gist options
  • Save vokiel/c025c6b0194f9651dcbbc4ec1584be56 to your computer and use it in GitHub Desktop.
Save vokiel/c025c6b0194f9651dcbbc4ec1584be56 to your computer and use it in GitHub Desktop.
#1. hello_world/
echo 'hello world'
#2. current_working_directory/
echo $PWD
#3. list_files/
ls -A
#4. print_file_contents/
cat access.log
#5. last_lines/
tail -5 access.log
#6. find_string_in_a_file/
cat access.log | grep GET
#7. find_tabs_in_a_file/
cat ./file-with-tabs.txt | grep -P "\t" | wc -l
#8. search_for_files_containing_string/
grep -rHl -e '500'
#9. search_for_files_by_extension/
find . -type f -name 'access.log*'
#10. search_for_string_in_files_recursive/
grep -Rrh ./ --include access.log* -e '500'
#11. extract_ip_addresses/
find . -type f -name access.log* -print0 | while read -d $'\0' fileName; do cat "$fileName" | sed -e 's/\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\).*$/\1/'; done
#12. delete_files/
find . -delete
#13. count_files/
find . -type f | wc -l
#14. simple_sort/
cat access.log | sort
#15. count_string_in_line/
cat access.log | grep GET | wc -l
#16. split_on_a_char/
cat split-me.txt | sed -e 's/;/\n/g'
#17. print_number_sequence/
seq -s ' ' 1 100
#18. remove_files_with_a_dash/
find . -type f -name '-*' -delete
#19. remove_files_with_extension/
find . -type f -name '*.doc' -delete
#20. remove_files_without_extension/
find . -type f ! -name '*.txt' ! -name '*.exe' -delete
#21. replace_text_in_files/
find . -type f -name '*.txt' -exec sed -i 's/challenges\ are\ difficult//g' {} \;
#22. sum_all_numbers/
awk '{ sum_me += $1 } END { print sum_me }' sum-me.txt
#23. just_the_files/
find . -type f -printf '%f\n'
#24. remove_extensions_from_files/
find . -type f -name '*.*' -print0 | while read -d $'\0' fileName; do mv "$fileName" "${fileName%.*}"; done
#25. replace_spaces_in_filenames/
ls -A | sed -se 's/ /\./g'
#26. files_starting_with_a_number/
find . -type f -name '[0-9{1,}]*' -printf '%f\n'
#27. print_nth_line/
head -25 faces.txt | tail -1
#28. remove_duplicate_lines/
cat faces.txt | awk '!x[$0]++'
#29. corrupted_text/
sed -r 's/(!!+)/!/g; s/(!\.!)/\./g; s/ !/ /g; s/(!)+( [a-z])/\2/g; s/([a-z]+)(!+)([a-z])/\1\3/g;' war_and_peace.txt
#30. print_common_lines/
join <(sort access.log.1) <(sort access.log.2) | sed -e 's/\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\).*$/\1/'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment