Reputation: 756
When I try to find the source of warnings or error in my code, the output from the compiler is often pre-processed, such that white-space is not anymore the same as in the original code.
What is a nice way to transform the grep
pattern with white-space like this:
grep 'data.frame(foo = x)' mycode
to
grep 'data.frame(foo *= *x)' mycode
?
One possibility, which I find a bit ugly:
PATTERN=`sed 's/ / */g' <(echo "test pattern") ` && grep $PATTERN mycode
Upvotes: 0
Views: 135
Reputation: 113964
Instead of:
grep 'data.frame(foo = x)' mycode
Use:
mygrep 'data.frame(foo = x)' mycode
where mygrep
is a file, with the executable bit set, containing:
#!/bin/bash
declare -a options
for arg in "$@"
do
case "$arg" in
-*) options+=("$arg") ; shift ;;
*) break ;;
esac
done
grep "${options[@]}" "$(sed 's/ / */g' <(echo "$1"))" "$2"
All the ugliness is hidden in the file. The arguments are the same. (Limitation: I haven't added the ability to handle options that take arguments for the special case where the option and its argument are separated by whitespace.)
Upvotes: 1
Reputation: 59566
How about
PATTERN=$(sed -r 's/\s+/\\s+/g' <<<"$PATTERN")
and then use egrep
to allow using the extended regexp feature \s
for whitespace.
Or in one step:
egrep "$(sed -r 's/\s+/\\s+/g' <<<"$PATTERN")" mycode
Upvotes: 1