If I want to search multi words by grep, I could do in the way:
egrep "a|b|c|d" my_input_file.txt
then egrep will help you to search OR of a, b, c and d. If searching for AND of multi-words, you can do by pipe:
grep a my_input_file.txt |grep b |grep c |grep d
. However, sometimes, it is not so easy to use pipe in one line for the above usage. You can always build string of the command first and evaluate it. For example,
cmd="grep $var1 my_input_file.txt|grep $var2"
...
cmd="$cmd |grep $var3"
...
eval $cmd
Saturday, November 8, 2008
linux:shell:bash:grep: multi-grep
Linux:shell: awk: multiple separators
'awk' is a very useful shell command (also a program language) in linux (unix). You split one text line by it. For example, for the line
line="aa, b c , d , f "
what you can do in shell command (bash) is
echo $line|awk -F "," '{print $1 $2 $3 $4}' # to get different columns separated by ","
However, if you wanna split one line with multi-separators, then you could do in the way, for example,
$ line="aa, bb_ ccd: ee"
$ echo $line |awk -F ",|_|:" '{print $1$2$3}'
Note here "|" will do logic "OR" of all separators. This is similar 'egrep'. If you want to grep multi key words, then you could do in the same way (which has been addressed in my other blog, just search grep ).
Tuesday, September 16, 2008
Linux:shell:command: speed up 'grep'
'grep' is very slow when using it in a UTF-8 mode, specially for a large text file. However, it is fast if using 'C' mode. Use the command 'locale' to check the variable of 'LC_ALL'. Then
export LC_ALL="C"
(from http://tdas.wordpress.com/2008/02/03/speed-up-grep)
I tested it. The "C" mode is faster 50 times than the regular mode!
Friday, July 20, 2007
Linux:shell:grep how to use grep's result
mystr="IloveChina"
echo $mystr |grep China
echo $? #this will return 0
echo $mystr |grep USA
echo $? #this will return 1
#so you could use this returned value to justify if grep found the string.
# Or you use how many searched results returned to do this, for example,
echo $mystr |grep -c China # return 1 to you
echo $mystr |grep -c USA # return 0 to you
# so simply, you can use the number to justify if you found the results like
if [ `echo $mystr |grep -c China` -ge 1 ]; then
echo "Yes, you are"
fi
