When we have many text files such as code samples or research documents then more often than not we want something which is in one of the files but we cannot remember which one. This is when searching inside text files becomes necessary.
There is a one-liner that can do the job.
find . -name "*" | xargs -d '\n' grep -i -A2 -B2 --color=auto "KEYWORD" 2> /dev/null
For example,
The find command lists all files in all sub-directories.
The xargs command takes the output of find and splices the by the delimiter which is a newline. What that does is that it prepares a list of filenames to be fed to the grep command one-by-one.
The grep command accepts the filenames from the input stream created by xargs command one-by-one and searches for the text "21sma".
-i switch ignores case.
-B2 includes two rows before the match
-A2 includes two rows after the match
--color=auto highlights the search key.
2> /dev/null suppresses the error messages from grep acting on the directories in the list generated by the find command (redirects the error channel to /dev/null)
There is a one-liner that can do the job.
find . -name "*" | xargs -d '\n' grep -i -A2 -B2 --color=auto "KEYWORD" 2> /dev/null
For example,
$ find . -name "*" | xargs -d '\n' grep -i -A2 -B2 --color=auto "21sma" 2> /dev/null ./coding/python/stock-python/stock-analysis/stock-analysis.py-# create SMA columns ./coding/python/stock-python/stock-analysis/stock-analysis.py-# min_periods starts averaging from day-0 not day-200 so it is always continuous with no initial 200-day gap ./coding/python/stock-python/stock-analysis/stock-analysis.py:stock['21SMA'] = stock['Adj Close'].rolling(window=21, min_periods=0).mean() ./coding/python/stock-python/stock-analysis/stock-analysis.py-stock['200SMA'] = stock['Adj Close'].rolling(window=200, min_periods=0).mean() ./coding/python/stock-python/stock-analysis/stock-analysis.py- ./coding/python/stock-python/stock-analysis/stock-analysis.py:# sd = stock['21SMA'].std() ./coding/python/stock-python/stock-analysis/stock-analysis.py-tempstd = stock['Adj Close'].rolling(window=200, min_periods=0).std()
The xargs command takes the output of find and splices the by the delimiter which is a newline. What that does is that it prepares a list of filenames to be fed to the grep command one-by-one.
The grep command accepts the filenames from the input stream created by xargs command one-by-one and searches for the text "21sma".
-i switch ignores case.
-B2 includes two rows before the match
-A2 includes two rows after the match
--color=auto highlights the search key.
2> /dev/null suppresses the error messages from grep acting on the directories in the list generated by the find command (redirects the error channel to /dev/null)
Comments