search.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # @yaml
  2. # signature: search_dir <search_term> [<dir>]
  3. # docstring: searches for search_term in all files in dir. If dir is not provided, searches in the current directory
  4. # arguments:
  5. # search_term:
  6. # type: string
  7. # description: the term to search for
  8. # required: true
  9. # dir:
  10. # type: string
  11. # description: the directory to search in (if not provided, searches in the current directory)
  12. # required: false
  13. search_dir() {
  14. if [ $# -eq 1 ]; then
  15. local search_term="$1"
  16. local dir="./"
  17. elif [ $# -eq 2 ]; then
  18. local search_term="$1"
  19. if [ -d "$2" ]; then
  20. local dir="$2"
  21. else
  22. echo "Directory $2 not found"
  23. return
  24. fi
  25. else
  26. echo "Usage: search_dir <search_term> [<dir>]"
  27. return
  28. fi
  29. dir=$(realpath "$dir")
  30. local matches=$(find "$dir" -type f ! -path '*/.*' -exec grep -nIH -- "$search_term" {} + | cut -d: -f1 | sort | uniq -c)
  31. # if no matches, return
  32. if [ -z "$matches" ]; then
  33. echo "No matches found for \"$search_term\" in $dir"
  34. return
  35. fi
  36. # Calculate total number of matches
  37. local num_matches=$(echo "$matches" | awk '{sum+=$1} END {print sum}')
  38. # calculate total number of files matched
  39. local num_files=$(echo "$matches" | wc -l | awk '{$1=$1; print $0}')
  40. # if num_files is > 100, print an error
  41. if [ $num_files -gt 100 ]; then
  42. echo "More than $num_files files matched for \"$search_term\" in $dir. Please narrow your search."
  43. return
  44. fi
  45. echo "Found $num_matches matches for \"$search_term\" in $dir:"
  46. echo "$matches" | awk '{$2=$2; gsub(/^\.+\/+/, "./", $2); print $2 " ("$1" matches)"}'
  47. echo "End of matches for \"$search_term\" in $dir"
  48. }
  49. # @yaml
  50. # signature: search_file <search_term> [<file>]
  51. # docstring: searches for search_term in file. If file is not provided, searches in the current open file
  52. # arguments:
  53. # search_term:
  54. # type: string
  55. # description: the term to search for
  56. # required: true
  57. # file:
  58. # type: string
  59. # description: the file to search in (if not provided, searches in the current open file)
  60. # required: false
  61. search_file() {
  62. # Check if the first argument is provided
  63. if [ -z "$1" ]; then
  64. echo "Usage: search_file <search_term> [<file>]"
  65. return
  66. fi
  67. # Check if the second argument is provided
  68. if [ -n "$2" ]; then
  69. # Check if the provided argument is a valid file
  70. if [ -f "$2" ]; then
  71. local file="$2" # Set file if valid
  72. else
  73. echo "Usage: search_file <search_term> [<file>]"
  74. echo "Error: File name $2 not found. Please provide a valid file name."
  75. return # Exit if the file is not valid
  76. fi
  77. else
  78. # Check if a file is open
  79. if [ -z "$CURRENT_FILE" ]; then
  80. echo "No file open. Use the open command first."
  81. return # Exit if no file is open
  82. fi
  83. local file="$CURRENT_FILE" # Set file to the current open file
  84. fi
  85. local search_term="$1"
  86. file=$(realpath "$file")
  87. # Use grep to directly get the desired formatted output
  88. local matches=$(grep -nH -- "$search_term" "$file")
  89. # Check if no matches were found
  90. if [ -z "$matches" ]; then
  91. echo "No matches found for \"$search_term\" in $file"
  92. return
  93. fi
  94. # Calculate total number of matches
  95. local num_matches=$(echo "$matches" | wc -l | awk '{$1=$1; print $0}')
  96. # calculate total number of lines matched
  97. local num_lines=$(echo "$matches" | cut -d: -f1 | sort | uniq | wc -l | awk '{$1=$1; print $0}')
  98. # if num_lines is > 100, print an error
  99. if [ $num_lines -gt 100 ]; then
  100. echo "More than $num_lines lines matched for \"$search_term\" in $file. Please narrow your search."
  101. return
  102. fi
  103. # Print the total number of matches and the matches themselves
  104. echo "Found $num_matches matches for \"$search_term\" in $file:"
  105. echo "$matches" | cut -d: -f1-2 | sort -u -t: -k2,2n | while IFS=: read -r filename line_number; do
  106. echo "Line $line_number:$(sed -n "${line_number}p" "$file")"
  107. done
  108. echo "End of matches for \"$search_term\" in $file"
  109. }
  110. # @yaml
  111. # signature: find_file <file_name> [<dir>]
  112. # docstring: finds all files with the given name in dir. If dir is not provided, searches in the current directory
  113. # arguments:
  114. # file_name:
  115. # type: string
  116. # description: the name of the file to search for
  117. # required: true
  118. # dir:
  119. # type: string
  120. # description: the directory to search in (if not provided, searches in the current directory)
  121. # required: false
  122. find_file() {
  123. if [ $# -eq 1 ]; then
  124. local file_name="$1"
  125. local dir="./"
  126. elif [ $# -eq 2 ]; then
  127. local file_name="$1"
  128. if [ -d "$2" ]; then
  129. local dir="$2"
  130. else
  131. echo "Directory $2 not found"
  132. return
  133. fi
  134. else
  135. echo "Usage: find_file <file_name> [<dir>]"
  136. return
  137. fi
  138. dir=$(realpath "$dir")
  139. local matches=$(find "$dir" -type f -name "$file_name")
  140. # if no matches, return
  141. if [ -z "$matches" ]; then
  142. echo "No matches found for \"$file_name\" in $dir"
  143. return
  144. fi
  145. # Calculate total number of matches
  146. local num_matches=$(echo "$matches" | wc -l | awk '{$1=$1; print $0}')
  147. echo "Found $num_matches matches for \"$file_name\" in $dir:"
  148. echo "$matches" | awk '{print $0}'
  149. }