#] #] ********************* #] "$d_SysMaint""Linux/grep summary.txt" www.BillHowell.ca 12Jun2021 initial extracted from 'grep notes.txt' # view this file in a text editor, with [constant width font, tab = 3 spaces], no line-wrap #************************ # Table of Contents, generated with : # defined in 'QNial setup.ndf' to easily see a short list : # opl_grep IS host (link 'grep "^#] " "$d_SysMaint""Linux/grep summary.txt" | # sed ' chr_apo 's/^#\]/ /' chr_apo) # $ grep "^#]" "$d_SysMaint""Linux/grep summary.txt" | sed 's/^#\]/ /' # DON'T use this - just look at $ linux -> 2 for grep (terminal output) 24************************24 #] +-----+ #] Search [ndf, txt] files in d_Q[root, ndfs, test] for [strings, [optr,var] symbols, etc] : #] \| for repeated patterns - can't concatenate more than 2 or so --invert-match terms #] #] $ find "$d_Qroot" -type f \( -name "*.ndf" -o -name "*.txt" \) | grep --invert-match "z_Old\|z_Archive" | tr \\n \\0 | xargs -0 -IFILE grep -i --with-filename --line-number 'ary_indexA_convertTo_boolA' "FILE" | sed "s|$d_Qroot||;s|:.*||" | sort -u >"$d_temp"'find-grep-sed temp.txt' #] #] find "$d_web" -maxdepth 6 -type f -name "*.html" | grep --invert-match "z_Old\|z_Archive\|Conference guides\|webWork\|Cool emails" >"$d_web"'webWork files/urls pLst base non-[webWork, conference guides].txt' #] #] grep regexpr following find (fnames ONLY here) : #] $ find "" -maxdepth 1 | grep "p\([0-9]\{3\}\)fib\([0-9]\{2\}\)\." https://stackoverflow.com/questions/21763904/linux-find-and-grep-command-together The nice thing about using grep with find is you can set -maxdepth on find, which you can't when just using grep -R. – jschmitter #] #] rename a selected list of files in a dir (uses "$d_bin"'fileops run.sh' [grep, sed, mv]) : #] dir_renameFileL "$d_web"'ProjMini/TrNNs-ART/images- Grossberg 2021/' 1 "p\([0-9]\{3\}\)fib\([0-9]\{2\}\)\." "s|p\([0-9]\{3\}\)fib\([0-9]\{2\}\)\.|p\1fig\2.|g" #] ***cannot include extra terms in grep with regexpr!!!? : #] eg grepStr="p\([0-9]\{3\}\)fib\([0-9]\{2\}\)\.\|Kanizsa" #] #] +--+ #] search a file, using [str, pattern]s listed in a file $ man grep -f FILE, --file=FILE Obtain patterns from FILE, one per line. If this option is used multiple times or is combined with the -e (--regexp) option, search for all patterns given. The empty file contains zero patterns, and therefore matches nothing. #] grep -i --with-filename --line-number "<:class:>" "$pinn" | sed 's|^/home/bill/web/\(.*\)\.html:[0-9]\+.*|\1|' | sort -u >>"$d_temp"'grep class.txt' #] #] +--+ #] -type [f, d] [f=file, d=directory] only one or the other $ find "$1" -maxdepth 1 -type f -name "$3" | sed 's#'$1'##g' >"$d_lst" $ find "$d_bads" -maxdepth 1 -type f -name "*.pdf" | sed "s#.*N-##g;s#\.pdf##g" >>"$p_badList" #] -type d directories only : $ find "$d_Qndfs" -maxdepth 1 -type d | sed "s|""$d_Qndfs""||" #] grep words (symbols) -w = word, -i = case insensitive $ find "$d_Qndfs" -type f -name "*.ndf" | grep --invert-match 'z_Archive' | grep --invert-match 'z_Old' | tr \\n \\0 | xargs -0 -ILINE grep -w -i --with-filename --line-number "str_to_phrases1" LINE #] -maxdepth 3 specify depth to search in directoryty tree $ find "/media/bill/SWAPPER/Website - raw/" -maxdepth 3 -name "*.html" | tr \\n \\0 | xargs -0 -IFILE grep -w -i --with-filename --line-number ':&file-insert &:' "FILE" | sed 's#:.*##' | sort -u #] "-atime -90" find files accessed less than 90 days ago $ find "$d_Qndfs" -atime -90 -type f -name "*.ndf" | grep --invert-match 'z_Archive' | grep --invert-match 'z_Old' | tr \\n \\0 | xargs -0 -ILINE grep --with-filename --line-number "_test" LINE | sed 's/^\(.*\):[0-9]\+:.*/\1/' | sort -u #] +-----+ #] output #] 2> /dev/null - send stderr to void must come AFTER normal ">" to stdout (default 1) #] 2>&1 - send stderr to stdout must come AFTER normal ">" to stdout (default 1) #] send stderr to a stdout then to file, when stderr floods results Can I find old bash scripts? #] $ find / -type f -name "batch_access.sh" 2>&1 | tr \\n \\0 | xargs -0 -IFILE grep --invert-match "Permission denied" "FILE" $ find / -type f -name "batch_access.sh" 2>"/media/bill/ramdisk/permission denied.txt" $ grep --invert-match "Permission denied" "/media/bill/ramdisk/permission denied.txt" >"/media/bill/ramdisk/permission denied2.txt" $ grep --invert-match "Operation not permitted" "/media/bill/ramdisk/permission denied2.txt" >> Not found anywhere #] --count Suppress normal output; print a count of matching lines for each input file #] bol str in pinn : nStrFnd=$( grep -c "$pinn" ); if [ 0 < "$nStrFnd384" ]; then... +-----+ https://stackoverflow.com/questions/4749330/how-to-test-if-string-exists-in-file-with-bash How to test if string exists in file with Bash? Asked 12 years, 8 months ago Modified 2 years, 10 months ago Viewed 844k times +--+ My version using fgrep FOUND=`fgrep -c "FOUND" $VALIDATION_FILE` if [ $FOUND -eq 0 ]; then echo "Not able to find" else echo "able to find" fi answered Jan 11, 2016 at 7:27 Rudy +--+ grep -Fxq "$FILENAME" my_list.txt The exit status is 0 (true) if the name was found, 1 (false) if not, so: if grep -Fxq "$FILENAME" my_list.txt then # code if found else # code if not found fi edited Jun 20, 2020 at 9:12 CommunityBot answered Jan 20, 2011 at 15:58 Thomas +--+ Regarding the following solution: rep -Fxq "$FILENAME" my_list.txt In case you are wondering (as I did) what -Fxq means in plain English: F: Affects how PATTERN is interpreted (fixed string instead of a regex) x: Match whole line q: Shhhhh... minimal printing From the man file: -F, --fixed-strings Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched. (-F is specified by POSIX.) -x, --line-regexp Select only those matches that exactly match the whole line. (-x is specified by POSIX.) -q, --quiet, --silent Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected. Also see the -s or --no-messages option. (-q is specified by POSIX.) edited Jun 30, 2018 at 19:25 Eje answered Jan 7, 2013 at 18:11 Kuf #] +-----+ #] Example of greps : #] #] grep "\[#" "pinn" | sed 's/\(.*\)\[#\(.*\)\]\(.*\)/\2/' | sort >"pout" # papers # 20005 - 21981 are provided, this includes posters >> there are a total of 1133 papers accepted for IJCNN2020 https://duckduckgo.com/?q=Linux+and+how+to+I+output+words+of+a+text+file%3F&t=h_&ia=web If you want to only print each word once, disregarding case, you can use sort #] grep -o -E '\w+' temp | sort -u -f -u only prints each word once -f tells sort to ignore case when comparing words -E '\w+' searches for words -o only prints the portion of the line that matches Abdullah Shoaib #] grep '\S' with_blank.txt > tmp.txt && mv tmp.txt with_blank.txt - remove [empty, whitespace] lines https://www.baeldung.com/linux/remove-blank-lines-from-file #] html ToC : $ grep '|
  • \n\t\t\t\t \1|' #] +-----+ #] With QNial #] qnial> host link 'grep --ignore-case --fixed-strings '' "' pinn '"' #] qnial> string_in_file '' 'pinn' 'pout' #] +-----+ #] special [characters, patterns] #] In general, it's prudent to single quote your regular expression so the shell leaves it alone. # Ray of https://stackoverflow.com/questions/4800214/grep-for-beginning-and-end-of-line #] [^$'",*.] regexpr special characters (escape to use as normal chrs) #] comma is sed OR separator in the bracket expression #] [^, $] = [beginning, end] of line #] [\n] not captured by grep, use multiline regepr with tr \\n \\0 #] [\\] escape chr, eg $ grep '\\' "$d_bin"'fileops.sh' (single-quote search term!) #] [\t] tab chr, YullyBear 'within the quote type the tab key' (shell dependent!) #] Howell used advice $ grep -P "^#\t", but I prefer "tab in the quote" #] cannot use escape chrs in grep eg \x9 #] .* grep wildcard not "?*", #] #] - grep stdin #] +-----+ #] repeat sequences #] \+ Like *, but matches one or more. It is a GNU extension. #] \? Like *, but only matches zero or one. It is a GNU extension. #] \{i\} Like *, but matches exactly i sequences (i is a decimal integer; # for compatibility, you should keep it between 0 and 255, inclusive). #] \{i,j\} Matches between i and j, inclusive, sequences. #] \{i,\} Matches more than or equal to i sequences. #] \(regexp\) Groups the inner regexp as a whole; this is used to: Apply postfix operators, like \(abcd\)*: this will search for zero or more whole sequences of ‘abcd’, while abcd* would search for ‘abc’ followed by zero or more occurrences of ‘d’. Note that support for \(abcd\)* is required by POSIX 1003.1-2001, but many non-GNU implementations do not support it and hence it is not universally portable. Use back references (see below). #] +-----+ #] count number str in file : $ grep -o "echo" FILE | wc -l https://stackoverflow.com/questions/6741967/how-can-i-count-the-occurrences-of-a-string-within-a-file How can I count the occurrences of a string within a file? Asked 12 years, 2 months ago Modified 1 month ago Viewed 175k times The number of string occurrences (not lines) can be obtained using grep with -o option and wc (word count): $ echo "echo 1234 echo" | grep -o echo echo echo $ echo "echo 1234 echo" | grep -o echo | wc -l 2 So the full solution for your problem would look like this: $ grep -o "echo" FILE | wc -l edited Apr 17, 2015 at 15:20 Jon Schneider 25.9k2323 gold badges144144 silver badges172172 bronze badges answered Jan 24, 2013 at 21:00 Dmitry $ man grep -o, --only-matching Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line. #] +-----+ #] key examples : #] cannot do more than a couple of piped --invert-match ?, use \| (see below) $ grep >>"$p_temp" "$authorsLastNames" "$p_tem2" #] $ find "$d_Qndfs" -maxdepth 4 -type f -name "*" | grep --invert-match "z_Old\|z_Archive" #] | tr \\n \\0 | xargs -0 -IFILE grep --with-filename --line-number "sym_stdForm_file" #] "FILE" | grep "IS OP" $ find "$d_Qndfs" -maxdepth 4 -type f -name "*" | tr \\n \\0 | xargs -0 -IFILE grep --with-filename --line-number "sym_stdForm" "FILE" qnial> host 'find "/media/bill/ramdisk" -maxdepth 4 -name "emSplit *" | tr \\n \\0 | xargs -0 rm' ; $ find "$d_Qndfs""MindCode" -type f -name "*.ndf" | tr \\n \\0 | xargs -0 -ILINE grep --with-filename "^#]" LINE | grep --invert-match 'z_Archive' "$d_temp""5_MindCode globals temp.txt" | grep --invert-match 'z_Old' | sed 's/.*#]//' pick out line numbers : $ grep -n "File transfer successful" "$d_bin""website rsync log.txt" | sed 's/\(^[0-9]*\).*/\1/' >"$d_temp""website rsync log lineNums.txt" #] $ grep "\*\(\*\)+\*", geany "\*(\*)+\*" #] to find all files without terms, may have to deselect fnames from full list with those terms : #] $ ls -1 '/home/bill/.local/share/evolution/mail/local/.7_5FNewsgroups slow.Connectionists/cur' >"$d_temp"'connectionists all email fnames.txt' #] $ find '/home/bill/.local/share/evolution/mail/local/.7_5FNewsgroups slow.Connectionists/cur' -maxdepth 1 -type f | tr \\n \\0 | xargs -0 -IFILE grep -i --with-filename "Post-doctoral\|workshop\|conference\|registration\|call for\|post-doc]\|position" "FILE" | sed "s|/home/bill/.local/share/evolution/mail/local/.7_5FNewsgroups slow.Connectionists/cur/\(.*\)\(.dell64:2,\)\(S*\).*|\1\2\3|" | grep --invert-match "Binary file " | sort -u >"$d_temp"'connectionists matching email fnames.txt' #] $ diff "$d_temp"'connectionists all email fnames.txt' "$d_temp"'connectionists matching email fnames.txt' --suppress-common-lines | sed 's|< ||' | grep ':' >"$d_temp"'connectionists filtered email fnames.txt' #] $ d_email="/home/bill/.local/share/evolution/mail/local/.6_5FNewsgroups.John OSulivan - PSI International.2021/cur/" #] +-----+ #] Searching for Whole Words #] grep -w -i free geek-1.log #] grep -E -w -i "average|memfree" geek-1.log multiple search terms #] -w = word, -i = case insensitive #] find "/media/bill/SWAPPER/Website - raw/" -maxdepth 3 -name "*.html" | tr \\n \\0 #] | xargs -0 -IFILE grep -w -i --with-filename --line-number ':&file-insert &:' "FILE" #] | sed 's#:.*##' | sort -u #] +-----+ #] use file for options etc? $ host link 'find "' d_webRaw '" -type f -name "*.html" | grep --invert-match "z_Old\|z_Archive\|System_maintenance\|Qnial_bag\|Cool emails/\|Electric Universe/References/\|Electric Universe/References/\|Yoonsuck Choe - conf program book/" >"' p_htmlFileList '" ' ; $ grep --invert-match "<\.\.\.@.*>" "/media/bill/ramdisk/emails.txt" | grep --invert-match "<@" - | grep --invert-match --ignore-case "" - | grep --invert-match --ignore-case "" - | grep --invert-match --ignore-case "" - | grep --invert-match --ignore-case "" - | grep --invert-match --ignore-case "LISTSERV@LISTSERV.IEEE.ORG" - #] +-----+ #] grep all files in a directory : $ find "$d_Qndfs" -maxdepth 1 -name "*" | tr \\n \\0 | xargs -0 -IFILE grep --with-filename --line-number "sym_stdForm" "FILE" #] +-----+ #] grep to first occurrence only in a [file, line] #] grep -o -a -m 1 -h -r "Pulsanti Operietur" /path/to/dir | head -1 #] grep to first occurrence only in a line : use sed!! #] grep -s "" /path/to/dir/{*,.*} #] +-----+ #] grep a given file, show line numbers of match (-n) : $ cd "/media/bill/SWAPPER/Lucas - Universal Force/" All headings : $ grep -n "^>>>" "Howell - Background math for Lucas Universal Force, Chapter 4.txt" | sed 's/>>>/ /g' Note that "$" is escaped, as it has a special grep meaning : $ grep -n ^\\$ "/media/bill/SWAPPER/Lucas - Universal Force/Howell - Background math for Lucas Universal Force, Chapter 4.txt" $ grep -n '\\t\\t\\t' uppercase ; #] +-----+ #] Note : the -P option is specific to GNU grep! grep --invert-match -P '\t' #] +-----+ #] null \x0 special characters : https://stackoverflow.com/questions/12387685/grep-for-special-characters-in-unix https://stackoverflow.com/questions/40811206/how-do-i-grep-for-special-charactercontrol-characters-using-hex-representation #] +-----+ #] '[]- IEEE-CIS character restraints - biggest problems (volume) 16Feb2020 Transform "'[]- then run : $ pname="/home/bill/PROJECTS/2020 WCCI Glasgow/reviews - mine/21114 r George, Banerjee, Dey, Mukherjee - A Reservoir-based Convolutional Spiking Neural Network for Gesture Recognition from DVS Input.txt.txt.txt" $ ptemp="/media/bill/ramdisk/pimp.txt" $ cat "$pname" | tr - z | tr [ z | tr ] z | tr \' z | tr \/ z | tr \" z | tr \: z >"$ptemp" $ grep -n '[^&A-Za-z0-9?!^_$\/\"`:,\(\).;\\-=+\*\n\r\t <>{}\\%]' "$ptemp" >> This doesn't work fully, but it reduces dramatically the lines to examine. # enddoc