#] #] ********************* #] "$d_SysMaint"'Linux/grep notes.txt' www.BillHowell.ca 13Apr2017 initial ??? # view this file in a text editor, with [constant width font, tab = 3 spaces], no line-wrap 24************************24 24************************24 # Table of Contents, generated with : # $ grep "^#]" "$d_SysMaint""Linux/grep notes.txt" | sed 's/^#\]/ /' # ********************* "$d_SysMaint"'Linux/grep notes.txt' 22Jun2021 search "Linux and do I remove empty lines in a file?" 22Jun2021 search "Linux and how to I output words of a text file?" 22Jun2021 search "Linux grep -w define separator characters" 24Oct2020 Searching for Whole Words, Using Multiple Search Terms 05Oct2020 search "Linux grep and how do I search for multiple patterns?" 16Feb2020 EEE-CIS paper system grep non-standard characters : 06Jan2020 problems with special characters 06Jan2020 \t in search term - use $ sign before single quotes, eg $'\t'' ' 12Jul2018 grep for Table of Contents 10Jul2018 hidden files don't glob 12Apr2018 find "strange" characters in a text file 03Oct2018 Did IJCNN proceedings have any [US gov, European, Crown, private] copyrights? 12Feb2018 grep-only-the-first-match-and-stop 13Jan2018 email addresses, grep -o "\([-A-Za-z0-9._]\+\)\(@\)\([-A-Za-z0-9._]\+\)" 13Apr2017 17:52 list of targets for initial file creation 21Mar2017 grep "<" character 08*********08 #] ??Oct2023 08*********08 #] ??Oct2023 08*********08 #] ??Oct2023 08*********08 #] ??Oct2023 08*********08 #] 21Oct2023 search "Linux grep binary file matches" +-----+ https://stackoverflow.com/questions/23512852/grep-binary-file-matches-how-to-get-normal-grep-output Grep 'binary file matches'. How to get normal grep output? [duplicate] Asked 9 years, 5 months ago Modified 1 year, 5 months ago Viewed 192k times Try: grep --text or grep -a for short. This is equivalent to --binary-files=text and it should show the matches in binary files. answered May 7, 2014 at 8:46 perreal 4 Note that you may need this flag in case your input file is indeed text file but it contains e.g. some random binary bytes in the middle because the data is corrupted or the "binary content" heuristics fails otherwise. The intent to need to specify this flag is to avoid outputting raw binary content to output accidentally if you grep a binary file by mistake. Traditionally outputting binary content to terminal could mess the whole terminal and require resetting the terminal to continue but that shouldn't happen with modern UTF-8 based systems. – Mikko Rantalainen Sep 21, 2022 at 10:44 2 -a, --text Process a binary file as if it were text; this is equivalent to the --binary-files=text option. – Danijel Jan 31 at 7:51 08*********08 #] 15Mar2023 search 'Linux grep and \s% option?' (should be printf %s) +-----+ https://unix.stackexchange.com/questions/11522/grep-s-vs-s Grep - ^(\s*$) vs ^\s*$ Asked 11 years, 11 months ago Modified 11 years, 11 months ago Viewed 17k times +-----+ Plain grep doesn't understand \s or parentheses.(*) You want grep -P if available, or grep -E/egrep and write \s out as [ ^I] (where ^I is Tab; grep without -P doesn't understand \t either). Additionally, with double quotes some shells will process the \s and leave behind just s; you should always use single quotes with regexes from the shell, except for when you actually need to interpolate a variable (e.g. something like '^\s*'"$foo"'\s+\(', switching quoting in mid-parameter). The alternatives in your second example (again, with -P and single quoting instead of double) do the same thing. The former is technically better because grep won't backtrack as much, but practically it won't matter. (*) pedantry: backslashed parens will work, again requiring single quoting so the shell doesn't eat the backslashes. egrep is usually easier. edited Apr 17, 2011 at 18:26 answered Apr 17, 2011 at 17:55 geekosaur @geekosaur: What shells are those? On Debian, I can't find a (Bourne-style) shell that prints anything but \a$| for printf %s\\n "\a$|". POSIX is clear that the backslash must be retained; I can't find anything about $ followed by an invalid character. – Gilles 'SO- stop being evil' Apr 17, 2011 at 18:32 08*********08 #] 03Mar2023 [escape, tab] chr +-----+ https://stackoverflow.com/questions/1825552/grep-a-tab-in-unix grep a tab in UNIX Asked 13 years, 3 months ago Modified 1 year, 2 months ago Viewed 339k times +--+ The answer is simpler. Write your grep and within the quote type the tab key, it works well at least in ksh grep " " * answered Apr 4, 2013 at 13:28 YullyBear 5 first you need to manage to input a TAB character in your shell - most shells interpret this key as a command (completion) – Kaii Jan 10, 2014 at 13:25 +-----+ https://stackoverflow.com/questions/42822174/escape-characters-in-grep Escape characters in grep Asked 5 years, 11 months ago Modified 1 year, 7 months ago Viewed 40k times If you double quote your regex, the shell treats backslashes specially (emphasis mine): The backslash retains its special meaning only when followed by one of the following characters: $, `, ", \, or newline. Within double quotes, backslashes that are followed by one of these characters are removed. This means that your expressions are treated as follows: grep -e"def|zzz" – grep receives def|zzz; because it defaults to basic regular expressions (BRE), | isn't special1 and grep tries to match the literal string def|zzz. grep -e"def\|zzz" – | isn't one of the special characters mentioned above, so grep receives def\|zzz, and GNU grep treats \| as alternation1. grep -e"def\\|zzz" – \\ is special according to the manual excerpt (try echo "\\"); grep sees def\|zzz because the shell removes a backslash, and the behaviour is the same as for the second case. grep -e"def\\\|zzz" – the shell turns this into def\\|zzz (\\ becomes \, \| isn't special to the shell and stays unchanged); grep sees \\ as a literal backslash (backslash escaped by backslash), so | isn't special, and grep tries to match the exact string def\|zzz. In general, it's prudent to single quote your regular expression so the shell leaves it alone. As a side note, I don't think your C program is representative of how the shell processes arguments; in Shell Operation, quoting is a separate step and includes backslash processing (see Escape Character). 1As an extension, GNU grep allows you to escape | in BRE and get alternation. POSIX BRE don't have alternation. As a consequence, the only difference between grep and grep -E for GNU grep is what has to be escaped; functionality is identical. edited Mar 4, 2020 at 22:44 answered Mar 4, 2020 at 22:25 Benjamin W. 08*********08 #] 03Mar2023 search 'Linux grep and beginning of line" +-----+ https://stackoverflow.com/questions/4800214/grep-for-beginning-and-end-of-line Grep for beginning and end of line? Asked 12 years, 1 month ago Modified 3 years, 4 months ago Viewed 254k times +--+ It looks like you were on the right track... The ^ character matches beginning-of-line, and $ matches end-of-line. Jonathan's pattern will work for you... just wanted to give you the explanation behind it answered Jan 26, 2011 at 0:06 Ray +--+ The lowercase -e option is used to express multiple search operations. The alternation is implied: $ echo abcdef | grep -e 'def' -e'zzz' abcdef $ echo abczzz | grep -e 'def' -e'zzz' abczzz Alternatively, you can use the upper -E option for extended regular expression notation: $ echo abcdef | grep -E 'def|zzz' abcdef I believe this solves you problem directly (either using -e for alternation or -E for extended regex notation). Hope this helps :-) FWIW, the issue with the backslashes is that | has special meaning to bash and needs to be escaped unless it is in single quotes. Here is a resource on quoting and escaping rules and the common pitfalls: http://wiki.bash-hackers.org/syntax/quoting edited Mar 16, 2017 at 1:08 answered Mar 16, 2017 at 0:55 Raymond Hettinger 08*********08 #] 19Feb2022 search 'Linux grep --invert-match does not exclude all matches' #] cannot do more than a couple of piped --invert-match ?, use \| # 19Feb2023 doesn't work! find "$d_web" -maxdepth 6 -type f -name "*.html" | grep --invert-match "z_Old" | grep --invert-match "z_Archive" | grep --invert-match 'Conference guides' | grep --invert-match "webWork" >"$d_web"'webWork files/urls pLst base non-[webWork, conference guides].txt' # same thing using \| OK find "$d_web" -maxdepth 6 -type f -name "*.html" | grep --invert-match "z_Old\|z_Archive\|Conference guides\|webWork" >"$d_web"'webWork files/urls pLst base non-[webWork, conference guides].txt' +-----+ https://www.baeldung.com/linux/grep-exclude-multiple-patterns Exclude Multiple Patterns With Grep by Abdul K Azad Last modified: November 4, 2020 2. Exclude Patterns Passed as Arguments 2.1. Combine Patterns as a Regular Expression We can form a regular expression that matches either word and invert the match with the -v flag: #] $ grep -ivw 'the\|every' /tmp/baeldung-grep Time for some thrillin' heroics. Here, we’ve used grep with the -w flag to treat the pattern as a whole word. As an additional note, the -i flag performs a case-insensitive match. However, the regular expression may become lengthy and unreadable as the number of patterns grows. To mitigate this, we can specify the patterns individually. 2.2. Specify Multiple Patterns The -e flag allows us to specify multiple patterns through repeated use. We can exclude various patterns using the -v flag and repetition of the -e flag: $ grep -ivw -e 'the' -e 'every' /tmp/baeldung-grep Time for some thrillin' heroics. We have successfully filtered the line that does not contain the specified patterns. 3. Exclude Patterns Mentioned in a File Alternatively, we can also read the patterns from a file using the -f flag, and invert the match with -v flag. From the manual: -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. Using a file to hold the patterns allows us to manage a large number of patterns easily and reuse the command from the shell history. Let’s make a file containing only one pattern: $ echo 'the' > /tmp/baeldung-grep-patterns $ grep -wiv -f /tmp/baeldung-grep-patterns /tmp/baeldung-grep Time for some thrillin' heroics. Every problem is an opportunity in disguise. Let’s add another entry to the file containing our patterns and run the grep command again: $ echo 'every' >> /tmp/baeldung-grep-patterns $ grep -wiv -f /tmp/baeldung-grep-patterns /tmp/baeldung-grep Time for some thrillin' heroics. We have successfully filtered lines not matching any pattern in the specified file. And, we can keep adding more entries to our patterns file as needed. 4. Conclusion In this short article, we’ve reviewed various grep flags to see how we can exclude lines matching one of the multiple specified patterns. 08*********08 #] 16Dec2022 unbalanced parenthesis - see "$d_Qndfs"'fileops.ndf' ' pinn_chr_writeScreenUnbalancedLines IS OP pinn chr - write lines of path with unbalanced chr 08*********08 #] 29Oct2021 search "Linux grep search and special grep characters that must be escaped" #] [^$'",*.[]{}-\\|\n] regexpr special characters (escape to use) - for grep searches #] but NOT always in a list, sed searches also use [/] +-----+ https://stackoverflow.com/questions/42822174/escape-characters-in-grep +--+ The lowercase -e option is used to express multiple search operations. The alternation is implied: $ echo abcdef | grep -e 'def' -e'zzz' abcdef $ echo abczzz | grep -e 'def' -e'zzz' abczzz Alternatively, you can use the upper -E option for extended regular expression notation: $ echo abcdef | grep -E 'def|zzz' abcdef I believe this solves you problem directly (either using -e for alternation or -E for extended regex notation). Hope this helps :-) edited Mar 16 '17 at 1:08 answered Mar 16 '17 at 0:55 Raymond Hettinger +--+ If you double quote your regex, the shell treats backslashes specially (emphasis mine): The backslash retains its special meaning only when followed by one of the following characters: $, `, ", \, or newline. Within double quotes, backslashes that are followed by one of these characters are removed. This means that your expressions are treated as follows: grep -e"def|zzz" – grep receives def|zzz; because it defaults to basic regular expressions (BRE), | isn't special1 and grep tries to match the literal string def|zzz. grep -e"def\|zzz" – | isn't one of the special characters mentioned above, so grep receives def\|zzz, and GNU grep treats \| as alternation1. grep -e"def\\|zzz" – \\ is special according to the manual excerpt (try echo "\\"); grep sees def\|zzz because the shell removes a backslash, and the behaviour is the same as for the second case. grep -e"def\\\|zzz" – the shell turns this into def\\|zzz (\\ becomes \, \| isn't special to the shell and stays unchanged); grep sees \\ as a literal backslash (backslash escaped by backslash), so | isn't special, and grep tries to match the exact string def\|zzz. In general, it's prudent to single quote your regular expression so the shell leaves it alone. As a side note, I don't think your C program is representative of how the shell processes arguments; in Shell Operation, quoting is a separate step and includes backslash processing (see Escape Character). 1 As an extension, GNU grep allows you to escape | in BRE and get alternation. POSIX BRE don't have alternation. As a consequence, the only difference between grep and grep -E for GNU grep is what has to be escaped; functionality is identical. edited Mar 4 '20 at 22:44 answered Mar 4 '20 at 22:25 Benjamin W. >> [$`,"\\n] This is PART of what needed +-----+ https://linuxhint.com/use-special-characters-in-the-grep-command/ months ago, by Aqsa Yasin Special characters are the regular expressions used in commands to perform several actions like #, %, *, &, $, @, etc. Using ... [^$'[]{}-|] 08*********08 #] 22Jun2021 search "Linux and do I remove empty lines in a file?" https://www.baeldung.com/linux/remove-blank-lines-from-file 4.1. Using grep We know that the grep utility is good at searching text. However, removing lines is a kind of file editing operation. It seems that we are picking the wrong tool for the problem. We can make use of the grep‘s -v option to print lines that don’t contain a pattern of a blank line. Or we tell grep to output lines containing a non-blank character. Let’s take a look at how the grep command solves the problem: $ grep -v '^[[:space:]]*$' with_blank.txt If our grep implementation supports shorthand character classes, for example, the widely used GNU Grep, we can make the command pretty short: $ grep '\S' with_blank.txt To write the output back to the input file, we need to save the output in a temp file and then “mv” it to the original input file: $ grep '\S' with_blank.txt > tmp.txt && mv tmp.txt with_blank.txt 08*********08 #] 22Jun2021 search "Linux and how to I output words of a text file?" +-----+ https://duckduckgo.com/?q=Linux+and+how+to+I+output+words+of+a+text+file%3F&t=h_&ia=web how to generate list of (unique) words from text file in ubuntu? You could use grep: -E '\w+' searches for words -o only prints the portion of the line that matches % cat temp Some examples use "The quick brown fox jumped over the lazy dog," rather than "Lorem ipsum dolor sit amet, consectetur adipiscing elit" for example text. if you don't care whether words repeat % grep -o -E '\w+' temp Some examples use The quick brown fox jumped over the lazy dog rather than Lorem ipsum dolor sit amet consectetur adipiscing elit for example text 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 % cat temp you can also use the tr command echo the quick brown fox jumped over the lazydog | tr -cs 'a-zA-Z0-9' '\n' edited Mar 6 at 16:12 Honey 24.5k1414 gold badges123123 silver badges218218 bronze badges answered May 10 '13 at 19:23 Abdullah Shoaib 08*********08 #] 22Jun2021 search "Linux grep -w define separator characters" +-----+ https://unix.stackexchange.com/questions/198419/how-to-tell-grep-w-what-characters-make-up-words In versions prior to 2.19, GNU grep's -w would only consider single-byte character alnums and underscore (so in UTF-8 locales, only the 26+26+10+1 (ASCII letters, digits and underscore)) as word constituents. So for instance echo Stéphane | grep -w St would match. That was fixed in 2.19. You could however implement the logic by hand: grep -E '([^[:alnum:]_.]|^)test([^[:alnum:]_.]|$)' That is test preceded by either a non-word-constituent or the beginning of the line and followed by either a non-word-constituent or the end of the line. (above [:alnum:] matches digits and letters in your locale, not only ASCII ones, fix the locale to C if you want only ASCII ones). If you don't want those surrounding non-word-constituents to be included in the match (for instance because you're using GNU's -o), you can this time use PCRE regexps and look-around operators: grep -Po '(*UCP)(?{}\\%'. Found '#' (0x23). >> But # does NOT appear! What is 0x23? >> How do I find special characters in [geany, kwrite]? 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 -e '[^&A-Za-z0-9?!^_$\/\"`:,\(\).;\\-=+\*\n\r\t <>{}\\%]' "$ptemp" >> This doesn't work fully, but it reduces dramatically the lines to examine. 08*********08 #] 06Jan2020 problems with special characters grep "\x0" https://stackoverflow.com/questions/27769375/shell-linux-grep-exact-sentence-with-null-character#27769528 To have the \0 taken into account try : grep -Pa "^key\x0" it works for me. edited Jan 4 '15 at 20:55 answered Jan 4 '15 at 19:35 rtome 08*********08 #] 06Jan2020 \t in search term - use $ sign before single quotes, eg $'\t'' ' https://stackoverflow.com/questions/1825552/grep-a-tab-in-unix#1825573 If using GNU grep, you can use the Perl-style regexp: grep -P '\t' * edited Oct 15 '18 at 20:30 codeforester 22.2k88 gold badges5454 silver badges7878 bronze badges answered Dec 1 '09 at 11:28 unwind The trick is to use $ sign before single quotes. It also works for cut and other tools. grep $'\t' sample.txt edited Oct 15 '18 at 20:30 codeforester 22.2k88 gold badges5454 silver badges7878 bronze badges answered Apr 17 '11 at 16:04 antimirov Doesn't work if the String contains anything other than '\t'. How would you search for "\t " (tab + space) for example? – Raman Apr 17 '13 at 15:05 6 Raman: You can use $'\t'' '. A real example that shows it works also with sh (not only bash, which is not by default installed on Android) is busybox grep -oE '^nodev'$'\t''fuse$' /proc/filesystems. – v6ak Jul 21 '13 at 7:14 5 I think $'...' is a bash idiom. Probably doesn't work in sh. Dunno about csh or tcsh. – Edward Falk Aug 31 '16 at 18:45 3 From 'man bash': Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. Backslash escape sequences, if present, are decoded ... – broeni Nov 22 '17 at 14:44 08*********08 #] 12Jul2018 grep for Table of Contents of "Howell - Background math for Lucas Universal Force, Chapter 4.txt" Heading 1 ">>> " at start of line ... Heading 2 ">>>>>> " Heading 3 ">>>>>>>>> " Table of Contents generated by : $ cd "/media/bill/SWAPPER/Lucas - Universal Force/" All headings : $ grep -n "^>>>" "Howell - Background math for Lucas Universal Force, Chapter 4.txt" | sed 's/>>>/ /g' Only Heading 1 : $ grep -n "^>>> " "Howell - Background math for Lucas Universal Force, Chapter 4.txt" | sed 's/>>>/ /g' Only Headings [1,2] : $ grep -n "^\(>>>\)\{1,2\} " "Howell - Background math for Lucas Universal Force, Chapter 4.txt" | sed 's/>>>/ /g' 08*********08 #] 10Jul2018 hidden files don't glob https://askubuntu.com/questions/777379/how-to-use-grep-on-all-files-non-recursively-in-a-directory +-----+ In Bash, a glob will not expand into hidden files, so if you want to search all the files in a directory, you need to specify hidden files .* and non-hidden *. To avoid the "Is a directory" errors, you could use -d skip, but on my system I also get an error grep: .gvfs: Permission denied, so I suggest using -s, which hides all error messages. So the command you are looking for is: grep -s "string" * .* If you are searching files in another dir: grep -s "string" /path/to/dir/{*,.*} Another option is to use the dotglob shell option, which will make a glob include hidden files. shopt -s dotglob grep -s "string" * For files in another dir: grep -s "string" /path/to/dir/* edited Jun 7 at 19:23 answered May 25 '16 at 18:53 wjandrea +-----+ Old timers would probably do this: find . -type f -print0 | xargs -0 grep "string" edited May 26 '16 at 1:22 Digital Trauma 1,435417 answered May 26 '16 at 0:25 dathompson 611 08*********08 #] 12Apr2018 find "strange" characters in a text file cat "filename " | grep --only-matching [^A-Za-z0-9] cat "/media/bill/SWAPPER/Qnial/MY_NDFS/translate symbols.ndf" | grep --only-matching "translate" --line-number cat "/media/bill/SWAPPER/Qnial/MY_NDFS/translate symbols.ndf" | grep --only-matching "translate" --line-number cat "/media/bill/SWAPPER/Qnial/MY_NDFS/translate symbols.ndf" | grep --invert-match ^$ --line-number | grep "[^A-Za-z0-9=;#'<(){}%?,@\+\-\/\*\.\_\:$]" >> OK - still have []"^&!!!>\t cat "/media/bill/SWAPPER/Qnial/MY_NDFS/translate symbols.ndf" | grep --invert-match ^$ --line-number | grep "[^A-Za-z0-9=;#'<()[]{}%?,@\+\-\/\*\.\_\:$]" >> nothing cat "/media/bill/SWAPPER/Qnial/MY_NDFS/translate symbols.ndf" | grep --invert-match ^$ --line-number | grep "[^A-Za-z0-9=;#'\"<()[]{}%?,@\+\-\/\*\.\_\:\t$]" >> nothing cat "/media/bill/SWAPPER/Qnial/MY_NDFS/translate symbols.ndf" | grep --invert-match ^$ --line-number | grep "[^A-Za-z0-9=;#'<>()[]{}%?,@+\-/*._:$]" >> nothing cat "/media/bill/SWAPPER/Qnial/MY_NDFS/translate symbols.ndf" | grep --invert-match ^$ --line-number | grep "[^A-Za-z0-9=;#'<>()[]{}%?,@\+\-\/\*\.\_\:$]" >> nothing cat "/media/bill/SWAPPER/Qnial/MY_NDFS/translate symbols.ndf" | grep --invert-match ^$ --line-number | grep "[^A-Za-z0-9=;#'<>(){}%?,@\+\-\/\*\.\_\:$]" >> OK - still have space tab "|!-[]^ cat "/media/bill/SWAPPER/Qnial/MY_NDFS/translate symbols.ndf" | grep --invert-match ^$ --line-number | grep "[^A-Za-z0-9=;#'|!^<>(){}%?,@\+\-\/\*\.\_\:$]" >> OK - still have space tab "|!-[]^~ cat "/media/bill/SWAPPER/Qnial/MY_NDFS/translate symbols.ndf" | grep --invert-match ^$ --line-number | grep "[^A-Za-z0-9~`=;#'|!^<>(){}%?,@\+\-\/\*\.\_\:$]" >> waits for further input https://unix.stackexchange.com/questions/81916/how-to-find-a-space-in-a-text-using-grep Note that \s also matches tab, return, vertical tab, form feed and, technically, newline. cat "/media/bill/SWAPPER/Qnial/MY_NDFS/translate symbols.ndf" | grep --invert-match ^$ --line-number | grep "[^A-Za-z0-9\s\~\`=;#'|!^<>(){}%?,@\+\-\/\*\.\_\:]" >> OK - still have space tab "[]&- , so \s didn't work cat "/media/bill/SWAPPER/Qnial/MY_NDFS/translate symbols.ndf" | grep --invert-match ^$ --line-number | grep "[^A-Za-z0-9\s\~\`=;#'|!^<>(){}%?,&@\+\-\/\*\.\_\:]" >> OK - still have space tab "[]- , so \s didn't work cat "/media/bill/SWAPPER/Qnial/MY_NDFS/translate symbols.ndf" | grep --invert-match ^$ --line-number | grep "[^A-Za-z0-9\s\~\`=;#'|!^<>()[]{}%?,&@\+\-\/\*\.\_\:]" >> Doesn't work - must be the [] cat "/media/bill/SWAPPER/Qnial/MY_NDFS/translate symbols.ndf" | grep --invert-match ^$ --line-number | grep "[^A-Za-z0-9\s\~\`=\";#'|!^<>(){}%?,&@\+\-\/\*\.\_\:]" >> OK - still have space tab -[] but most phrases come out, and '" aren't flagged 13Apr2018 - give up for now, just use QNial? >> wait - can force grep to treat all string as chars (including ")? --fixed-strings >> can I mix a pattern with fixed cat "/media/bill/SWAPPER/Qnial/MY_NDFS/translate symbols.ndf" | grep --invert-match ^$ --line-number | grep "[^A-Za-z0-9\t\n\~\`=\";#'|!^<>(){}%?,&@\+\-\/\*\.\_\:]" >> nothing - try with another file /media/bill/RaspPi_ext4_32Gb/temp/chrs_special test.txt >>>>> /media/bill/RaspPi_ext4_32Gb/temp/chrs_special test.txt kdfgbjh4u4yi4i@$(%(GFgik special char ε <<<<< cat "/media/bill/RaspPi_ext4_32Gb/temp/chrs_special test.txt" | grep --invert-match ^$ --line-number | grep "[^A-Za-z0-9\t\n\~\`=\";#'|!^<>(){}%?,&@\+\-\/\*\.\_\:]" --line-number | grep "[^A-Za-z0-9\t\n\~\`=\";#'|!^<>(){}%?,&@\+\-\/\*\.\_\:]" 1:/media/bill/RaspPi_ext4_32Gb/temp/chrs_special test.txt 3:kdfgbjh4u4yi4i@$(%(GFgik 4: 5:special char ε >> special chars are flagged, but ALL lines are printed >> Try $ cat "/media/bill/SWAPPER/Qnial/MY_NDFS/translate symbols.ndf" | grep --invert-match ^$ --line-number | grep "[^A-Za-z0-9\s\~\`=\";#'|!^<>(){}%?,&@\+\-\/\*\.\_\:$]" >> all output again >> use --only-matching cat "/media/bill/SWAPPER/Qnial/MY_NDFS/translate symbols.ndf" | grep --invert-match ^$ --line-number | grep --only-matching "[^A-Za-z0-9\s\~\`=\";#'|!^<>(){}%?,&@\+\-\/\*\.\_\:$]" >> no help, just prints non-included symbols Q'Nial V7.0 Open Source Edition Intel x86 64bit Linux Jul 6 2017 Copyright (c) NIAL Systems Limited Bus error >> shit, also get segmentation error, nothing works? 13Apr2018 18:48 cold reboot of Raspi - let's see what happens! 08*********08 #] 03Oct2018 Did IJCNN proceedings have any [US gov, European, Crown, private] copyrights? I want filename!! two stages $ cd "/media/bill/IJCNN 2017/PDFs/Papers/" $ find -maxdepth 1 -name "*" | sed 's/\.\///' | tr \\n \\0 | xargs -0 -IFILE pdftotext FILE $ find -maxdepth 1 -name "*.txt" | sed 's/\.\///' | tr \\n \\0 | xargs -0 -IFILE grep --with-filename --line-number "U.S. Government work not protected by U.S. copyright" FILE 144_0201.txt:54:U.S. Government work not protected by U.S. copyright 292_0820.txt:38:U.S. Government work not protected by U.S. copyright 294_0708.txt:57:U.S. Government work not protected by U.S. copyright 325_0646.txt:53:U.S. Government work not protected by U.S. copyright 353_0490.txt:69:U.S. Government work not protected by U.S. copyright $ find -maxdepth 1 -name "*.txt" | sed 's/\.\///' | tr \\n \\0 | xargs -0 -IFILE grep --with-filename --line-number "2017 Crown" FILE 243_0773.txt:101:978-1-5090-6182-2/17/$31.00 ©2017 Crown 381_0367.txt:56:978-1-5090-6182-2/17/$31.00 ©2017 Crown $ find -maxdepth 1 -name "*.txt" | sed 's/\.\///' | tr \\n \\0 | xargs -0 -IFILE grep --with-filename --line-number "2017 European Union" FILE 297_0695.txt:57:978-1-5090-6182-2/17/$31.00 ©2017 European Union $ find -maxdepth 1 -name "*.txt" | sed 's/\.\///' | tr \\n \\0 | xargs -0 -IFILE grep --with-filename --line-number "978-1-5090-6182-2/17/" FILE >"/media/bill/SWAPPER/2019 IJCNN Budapest/Publications/181003 2017 Anchorage copyrights.txt" >> includes general, Crown, European union, private $ find -maxdepth 1 -name "*.txt" | sed 's/\.\///' | tr \\n \\0 | xargs -0 -IFILE grep --with-filename --line-number "978-1" FILE >"/media/bill/SWAPPER/2019 IJCNN Budapest/Publications/181003 2017 Anchorage copyrights.txt" >> includes general, Crown, European union, private YIKES!!! European, Crown, general all have same ISBN in 2017?!! (not [USA government - no ISBN, private - special ISBN] ) Should all copyrights start with "ISBN "? $ cat "/media/bill/SWAPPER/2019 IJCNN Budapest/Publications/181003 2017 Anchorage copyrights.txt" | grep --invert-match "978-1-5090-6182-2/17/" 224_0619.txt:1031:Manual Caister”. Academic Press. Editor (2011). ISBN 978-1904455-88-2. 272_0194.txt:916:http://dx.doi.org/10.1007/978-1-4419-0468-3 366_0612.txt:640:http://dx.doi.org/10.1007/978-1-4471-4072-6 18 >> These have ISBNs in the references 323_0687.txt:53:978-1-5090-1370-8/16/$31.00 Crown 2016 >> This one has 978-1-5090-1370-8/16/$31.00 2016 in the bottom of the left-hand column, but 978-1-5090-6182-2/17/$31.00 ©2017 IEEE in the footer! Binary file 411_0905.txt matches >> ?? 08*********08 #] 12Feb2018 grep-only-the-first-match-and-stop https://stackoverflow.com/questions/14093452/grep-only-the-first-match-and-stop -m 1 means return the first match in any given file. But it will still continue to search in other files. Also, if there are two or more matched in the same line, all of them will be displayed. You can use head -1 to solve this problem: grep -o -a -m 1 -h -r "Pulsanti Operietur" /path/to/dir | head -1 edited Apr 17 '14 at 3:50, Dan Dascalescu answered Dec 30 '12 at 18:44, mvp https://stackoverflow.com/questions/9543201/use-grep-to-match-a-pattern-in-a-line-only-once Do you want grep to stop matching or do you only care about the first match. You could use head if the later is true... `grep stuff | head -n 1` Grep is a line based util so the -m 1 flag tells grep to stop after it matches the first line which when combined with head is pretty good in practice. edited Mar 3 '12 at 3:44 answered Mar 3 '12 at 3:38, Andrew White 08*********08 #] 13Jan2018 email addresses, grep -o "\([-A-Za-z0-9._]\+\)\(@\)\([-A-Za-z0-9._]\+\)" $ email_test=" From: \"Saxena, Vishal v-saxena@u-idaho.ed)\" " From: "Saxena, Vishal v-saxena@u-idaho.ed)" $ echo "$email_test" | grep "\([^-A-Za-z0-9._]\+\)\([-A-Za-z0-9._]\+\)\(@\)\([-A-Za-z0-9._]\+\)" From: "Saxena, Vishal v-saxena@u-idaho.ed)" >> full line isn't useful, need "-o" flag : $ echo "$email_test" | grep -o "\([-A-Za-z0-9._]\+\)\(@\)\([-A-Za-z0-9._]\+\)" v-saxena@u-idaho.ed vsaxena@uidaho.edu >> perfect!!! I might be missing a few "legal" characters for email addresses 08*********08 #] 13Apr2017 17:52 list of targets for initial file creation My own example, created for the OPM project make system Notice the Perl (-P) search for a tab #!/bin/sh # /home/bill/OOPM builds/make/0_list of targets for initial file creation.sh # www.BillHowell.ca 13Apr2017 17:54 # /home/bill/OOPM builds/make/0_list of targets for initial file creation - template.txt # Template for the initial creation of target files, to avoid root ownership. # $ bash "/home/bill/OOPM builds/make/0_list of targets for initial file creation.sh" cd "/home/bill/OOPM builds/make" grep --invert-match -P '^\t' 0_Makefile | grep --invert-match ".*echo.*" | grep --invert-match ^[#] | grep --only-matching ".*.txt" | sort >"0_list of targets for initial file creation.txt" cat "0_list of targets for initial file creation.txt" | \ sed >"0_list of targets for initial file creation - bash.sh" 's/\(.*\)/cp "0_list of targets for initial file creation - template.txt" "\1"/' bash "0_list of targets for initial file creation - bash.sh" 08*********08 #] 21Mar2017 grep "<" character man grep : Backslash Character and Special Expressions The Backslash Character and Special Expressions The symbols \< and \> respectively match the empty string at the beginning and end of a word. The symbol \b matches the empty string at the edge of a word, and \B matches the empty string provided it's not at the edge of a word. The symbol \w is a synonym for [_[:alnum:]] and \W is a synonym for [^_[:alnum:]]. . http://stackoverflow.com/questions/12387685/grep-for-special-characters-in-unix Grep for special characters in Unix Tell grep to treat your input as fixed string using -F option. grep -F '*^%Q&$*&^@$&*!^@$*&^&^*&^&' application.log Option -n is required to get the line number, grep -Fn '*^%Q&$*&^@$&*!^@$*&^&^*&^&' application.log edited Dec 21 '13 at 8:30, Steven Penny answered Sep 12 '12 at 12:04, Prince John Wesley The one that worked for me is: grep -e '->' The -e means that the next argument is the pattern, and won't be interpreted as an argument. From: http://www.linuxquestions.org/questions/programming-9/how-to-grep-for-string-769460/ edited Dec 21 '13 at 8:30, Steven Penny answered Apr 18 '13 at 13:12, Tom Brito 08*********08 #] 18Dec2021 man grep man grep : Backslash Character and Special Expressions Matching Control -e PATTERN, --regexp=PATTERN Use PATTERN as the pattern. If this option is used multiple times or is combined with the -f (--file) option, search for all patterns given. This option can be used to protect a pattern beginning with “-”. General Output Control -c, --count Suppress normal output; instead print a count of matching lines for each input file. With the -v, --invert-match option (see below), count non-matching lines. -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. -i, --ignore-case Ignore case distinctions in both the PATTERN and the input files. -v, --invert-match Invert the sense of matching, to select non-matching lines. -w, --word-regexp Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore. This option has no effect if -x is also specified. -x, --line-regexp Select only those matches that exactly match the whole line. For a regular expression pattern, this is like parenthesizing the pattern and then surrounding it with ^ and $. -y Obsolete synonym for -i. # enddoc