In-place processing with grep
sponge (in moreutils package in Debian/Ubuntu) reads input till EOF and writes it into file, so you can grep file and write it back to itself. Like this: grep ‘pattern’ file | sponge file
sponge (in moreutils package in Debian/Ubuntu) reads input till EOF and writes it into file, so you can grep file and write it back to itself. Like this: grep ‘pattern’ file | sponge file
explainshell helpfully explains your command, and gives an excerpt from man grep: -w, –word-regexp Select only those lines containing matches that form whole words. So just remove -w since that explicitly does what you don’t want: grep -rn ‘/path/to/somewhere/’ -e “pattern”
This is a little different from Banthar’s solution, but it will work with versions of find that don’t support -newermt and it shows how to use the xargs command, which is a very useful tool. You can use the find command to locate files “of a certain age”. This will find all files modified between … Read more
You can work around this by typing a literal tab into your command: # type it with ^V then tab git grep ‘ ‘
Do you need to use ls? You can use find to do the same: find . -maxdepth 1 -perm -111 -type f will return all executable files in the current directory. Remove the -maxdepth flag to traverse all child directories. You could try this terribleness but it might match files that contain strings that look … Read more
The exit status of grep doesn’t necessarily indicate an error ; it indicates success or failure. grep defines success as matching 1 or more lines. Failure includes matching zero lines, or some other error that prevented matching from taking place in the first place. -q is used when you don’t care about which lines matched, only … Read more
find . -name ‘*.class’ -printf ‘%h\n’ | sort -u From man find: -printf format %h Leading directories of file’s name (all but the last element). If the file name contains no slashes (since it is in the current directory) the %h specifier expands to “.”.
With grep: grep -v ‘^\s*$\|^\s*\#’ temp On OSX / BSD systems: grep -Ev ‘^\s*$|^\s*\#’ temp
OK, I think this will do what you’re looking for. It will look for a pattern, and extract the 5th line before each match. grep -B5 “pattern” filename | awk -F ‘\n’ ‘ln ~ /^$/ { ln = “matched”; print $1 } $1 ~ /^–$/ { ln = “” }’ basically how this works is … Read more
Grep will recurse through any directories you match with your glob pattern. (In your case, you probably do not have any directories that match the pattern “*.cpp”) You could explicitly specify them: grep -ir “xyz” *.cpp */*.cpp */*/*.cpp */*/*/*.cpp, etc. You can also use the –include option (see the example below) If you are using … Read more