Hot answers tagged

8

Running xargs cat like that loses the filenames and there's no good way to pass them through the pipeline at the same time as the data flows through. But grep -l lists the names of files with matching strings, so you could use that: find . -type f | xargs grep -l hello Or with just having grep -r recursively dig through the directory, also resolving ...


7

xargs is superfluous. When you do xargs cat | grep, grep only sees a nameless input stream that contains the concatenated content of all the files. Do instead: find . -type f -exec grep -H 'abc' {} + This will call grep on each file (and minimizes the number of instances of grep, by calling grep with several files as parameters).


7

Append: | awk '{if(NR>1){print $4-last,"("$4"-"last")"} last=$4}' Output: 26939 (265735665-265708726) 16047 (265751712-265735665) 2864 (265754576-265751712) 19804 (265774380-265754576)


3

Variables are not expanded within single quotes. In this case, simply switching to double quotes would probably be enough. Also, remove the [ and ] around $domain. With the square brackets, you would create a bracket expression that would match any single character in the domain name (but not the actual domain name itself). In the end, you would end up ...


3

awk '/buffer_pool_pages_flushed/{curr=$(NF-1); print curr-prev; prev=curr}'


3

Try this: while read line ; do grep -ri -e "$line" /searchfolder/ > "$line.txt" done < example.txt


3

find ./ -type f -exec grep -H abc {} \; or grep -R 'abc' ./


2

Grep takes multiple filenames and reads the files, and with multiple args it shows the filenames, so just pass the filename list direct. Also, don't pass directories to grep. And use null-terminated filenames to avoid special characters in filenames. find . -type f -print0 | xargs -0 grep 'abc'


2

Try awk instead of cat and grep find . -type f | xargs awk '/abc/{printf "%s is from %s\n", $0, FILENAME}' Or just use find and awk find . -type f -exec awk '/abc/{printf "%s is from %s\n", $0, FILENAME}' {} +


2

This will work using any awk in any shell on every UNIX box: $ awk '/^ *\[\.\//{title=$1} /^ *input =/{print FILENAME ":", title, "=", $NF}' */*/assessment 01/to/assessment: [./input_file_01] = 'input_file_01.i' 02/to/assessment: [./input_file_02] = 'input_file_02.i' 02/to/assessment: [./input_file_02b] = 'input_file_02b.i'


2

-d skip will make grep skip directories: grep -in -d skip github / According to this MaxOS man page that option should work for MacOS grep. If it turns out that this doesn't work with the MacOS grep you can install then Homebrew MacOS package manager and then use Homebrew to install the GNU version of grep, since GNU grep supports -d skip (though in ...


2

Awk scripts consist of pairs of patterns and actions that are executed when the pattern evaluates logically true. So you can use ... | awk '$7==2 {print}' or more succinctly, because {print} is the default action ... | awk '$7==2'


1

If you know the list of files to search for email addresses in, you can grep -H -f email_list_file list of files to search | awk -F: '{print "*** this email " $2 " found in " $1 "**}'


1

This way you get all file names that contain keyword: find /home -type f -name "*keyword*" See: man find


1

That first one, \([.]+\), would match literal dots inside literal parenthesis, like (...) etc. The other ones don't match the hex digits above 9, i.e. abcdef. There's a d in 671d. Remember that IPv6 addresses are in hex. You'd need to add those, so: echo 'files ([2001:450:671d:200::121]:59464).' | egrep 'files \([][0-9a-f:]+\)\.$' or more strictly: echo '...


1

For the grep command you are missing the FILE pattern, e.g. the current directory: $ grep -r "string to find" . Default behaviour for at least some Unices is to use standard input when it is missing. From man grep: "grep searches the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is given as file name..." EDIT:...


Only top voted, non community-wiki answers of a minimum length are eligible