This situation is convenient because it means that no matter what UNIX or UNIX-like system you use, you have a grep command available. Since then, the grep command’s code has been written and rewritten by several different programmers, but its name has persisted. (This tool received wide release only because Thompson's department head, Doug McIlroy, asked for a tool “to look for stuff” in files.) The grep command, which is an initialism for global regular expression print, started its life as a personal utility script on the computer of the co-creator of UNIX, Ken Thompson. If you are a sysadmin or programmer and find yourself obsessively dipping into streams of text on a POSIX system, then you have probably either encountered grep, or come across a time you wished you could use this command. Most people would be disappointed to learn that sysadmins and code monkeys more often poke at streams of text in hopes of getting the right response. Their goal? To reach into the virtual reality of the Internet, gathering the binary forces of code into the applications and infrastructure we all use today. tabs) in the line: if s in open(file).read().replace(" ", "").Most people imagine that system administrators and programmers fiddle with knobs and diodes. It can be expanded with other characters/strings (e.g. The script, as it is finds files with the string, with either whitespaces or newlines in it. If either the string or the directory contains spaces, use quotes: python3 /path/to/find_string.py '' '' Run it with the directory and the string as arguments: python3 /path/to/find_string.py Copy the script into an empty file, save it as find_string.py.The script #!/usr/bin/env python3įor root, dirs, files in os.walk(sys.argv): The try / except I built in to prevent the script from breaking if it runs into an unreadable file. home/jacob/Bureaublad/testmap/Naamloze map 2/test1.txt If it finds matching files, they will be printed in the terminal, including their paths, like: /home/jacob/Bureaublad/testmap/test2.txt This implies that the spaces/newlines can be on any position in the string inside your file(s). If the string exists in the remaining text, there is a match. The code below searches a directory recursively for files, removes all occurrences of " " and "\n". Using suggestion to get sed to generate the pattern for us would be the best option: grep -rlzP "$(sed 's/./\\s*&/2g' <<< "thisissometext")". With -P (PCRE) option you can replace the ] with \s (this would look much nicer): grep -rlzP 't\s*h\s*i\s*s\s*i\s*s\s*s\s*o\s*m\s*e\s*\ On the other hand, if the whitespaces can come at any places rather than the words, this would loose its good look: grep -rlz If you want to print the matches also, use -H instead of -l. Grep -l will print only the file names that having any of the desired patterns. So, we can use it to match all the whitespace characters that can come in between the words. ] character class pattern indicates any whitespace characters including space, tab, CR, LF etc. The main trick here is -z, it will treat the each line of input stream ended in ASCII NUL instead of new line, as a result we can match newlines by using usual methods. So, simply: grep -rlz 'this]*is]*some]*text'. choose files of which directory to exclude. find can be used to be selective on the files to search e.g. If you just want to search all files recursively starting from current directory, you don't need find, you can just use grep -r (recursive). type f -exec grep -lz 'this]*is]*some]*text' +Ĭonsidering the whitespaces can come in between the words only. With the newer versions of GNU grep (that has the -z option) you can use this one liner: find.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |