Tag Info

Hot answers tagged

2

Yes a function is the way to go. If you have GNU grep use the --exclude-dir option so the recursive search does not even bother with it. use the --binary-files=without-match option to ignore binary files (alias -I). So: mygrep() { grep -R --exclude-dir=tmp --exclude-dir=test -I "$@" . } Then call mygrep Foo mygrep -i foo mygrep -E '(foo|bar)baz' ...


2

You can use grep: grep -oP '(?<=-)([0-9]+\.?)+' a.txt That extracts all version numbers. If a line contains no version (for example javax.annotation) nothing is printed. The regex: (?<=-): first look for a dash (-), but it should not be a part of the match [0-9]+: search for numbers, they should appear at least one or multiple times ([0-9]+\.?)+: ...


1

You would need to scan all files and get the first line, then check for pattern, finally execute a print of the first ten lines. It might get really expensive. find /path/to/search \ -type f \ -exec /bin/bash -c "head -n 1 '{}' | grep 'PATTERN' >/dev/null" \; \ -exec head -n 10 \{\} \; The -type f is required to not run head on directories, ...


1

It isn't possible to perfectly match all of those strings as you've listed them as there's no way to tell the difference between something like "orai18n-" and "-se2.0". If you create a regex that looks for strings of digits and dots that begin with a dash, you'll match all but the "java-se2.0" string pretty well: sed 's/.*-\([0-9\.][0-9\.]*\).*/\1/' ...


1

Found out that grep considers the file as binary, thus a workaround would be cat new.sql | tr -d '\000' | grep -F "INSERT" Source here: http://unix.stackexchange.com/questions/19907/what-makes-grep-consider-a-file-to-be-binary


1

echo $var | grep -e "Some" is actually the right approach. When piping the output through grep with |, it no longer appears on the console. From the man page of Bash: Pipelines A pipeline is a sequence of one or more commands separated by one of the control operators | or |&. The format for a pipeline is: [time [-p]] [ ! ] command [ ...


1

bash now allows here strings, eg: var="Some random string" grep -e "Some" <<<"$var"


1

Tried this on my sh-compatible terminal: $ grep --only-matching --perl-regexp "[^.]*searchterm[^.]*" \ <<< "Foo blah. Blah blah searchterm blah blah. Foo bar." Blah blah searchterm blah blah $ Can be abbreviated to grep -oP. I think the problem with the regex you provided is specifying .*to how greedy you wanted it to be (as stated by ...


1

You are missing a space in your cut command. cut -d\ -f1 should be cut -d\ -f1 since you are using \ to escape the following space (using space as the delimiter), and arguments are separated by a space, there is a missing space between the -d and -f (for cut it looks like you are trying to use " -f1" as a delimiter, which is more than one character). ...


1

Forget using grep or awk and do it in your shell. while IFS= read -r line do echo "$line" > "$line" done < FILE


1

Are you determined to use Grep/Awk? There is a much easier way to do this: while read f; do echo $f > $f; done<file.txt



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