linux文件内容搜索

概览:

说明

例:
search -k PS1,alias
search -k PS1,alias -o 2.1>/dev/pts/5

“PS1”和“alias”作为搜索关键字
2.1> /dev/pts/ 5,’2.1’的整数部分中的’2’为第二个关键字,而“2.1”的小数部分中的“1”为文件描述符1.这个’> /dev/pts/5’表示输出结果到/dev/pts/5。

就是说将第二个关键字“alias”的搜索结果(标准输出)输出到/dev/pts/5。

注意:
如果要将结果分别输出到不同的设备。

文件描述符1是标准输出(stdout)。
文件描述符2是标准错误(stderr)。

‘/dev/pts/x’:将输出重定向到终端(/dev/pts/x)。
或者您可以将输出重定向到文件,例如’> filename’。

如果你不知道这是什么意思.请忽略它。

还在完善

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/bash --posix

KEYWORD=$1

user_guid(){
echo ""
echo -en "Usage: search -d [dir path] -k [Keyword,...] -o [Output,...]\n"
echo ""
echo -en " -k, \tKeywords to search.\n"
echo -en " -o, \tRedirect output dedeination.\n"
echo -en " -v, \tsearch version.\n"
echo -en " -h, \tdisplay this help message.\n"
echo -en "\t\t(default search path is current directory.)\n"
echo -en "\nExample:\n"
echo -en " search -k PS1,alias\n"
echo -en " search -k PS1,alias -o 2.1>/dev/pts/5\n\n"
echo -en "\"PS1\" & \"alias\" as search keywords\n"
echo -en " 2.1>/dev/pts/5 ,the '2' in the integer part of '2.1'\nrefers the to second keyword while the '1' in the decimal part of '2.1'\nrefers the to File descriptor 1.the '>/dev/pts/5' the means is output the result\n"
echo -en "\n\n"
echo -en "notice:\n"
echo -en "if you want to output the result separately to different device.\n\n"
echo -en "File descriptor 1 is the standard output (stdout).\n"
echo -en "File descriptor 2 is the standard error (stderr).\n"
echo -en "\n'/dev/pts/x' : redirect output to the terminal(/dev/pts/x).\n"
echo -en "or you can redirect output to file,like '>filename'.\n"
echo -en "\n\tif you dont't know what this mean.please ignore it.\n\n"
}

search(){
find ./ -name "*"|while read fn;do
if [ -f $fn ];then
echo "File Name:${fn}" >/dev/pts/0 2>&1;
if [ -n "$(cat ${fn} |grep "Raspberry")" ];then
echo -en "\e[5;031mFile Name: ${fn}\e[0m\n" >/dev/pts/1
echo -en "Matching Raspberry: \t" 1>/dev/pts/1
cat -n ${fn} |grep "Raspberry" 1>/dev/pts/1
fi
fi
done
}


main(){
user_guid
}

main
------ 文章已经结束,谢谢你的阅读 ------
------ article has ended,thank you for reading ------
Thanks support Me

---------------- The End ----------------