MathJax 是一個 JavaScript 的函式庫,可以讓你直接使用 LaTeX、MathML 或 AsciiMath 的語法,把數學式寫進網頁中,並且可以用右鍵剪貼數學式,各種瀏覽器都可以看,效果幾乎跟傳統 LaTeX 一樣,是一個十分強大的函式庫。
2018年8月16日 星期四
2016年7月28日 星期四
Ubuntu 開啟ssh 連線
在ubuntu的環境下, 如果想要開啟遠端ssh連線
要安裝 openssh-client and openssh-server
並執行 ssh
安裝好ssh後可以在 /etc/ssh/sshd_config 裡面更改連線的port的位置
port 的預設值是22
如果沒有安裝ssh就連線, 會看到以下錯誤訊息
ssh: connect to host localhost port 22: Connection refused
要安裝 openssh-client and openssh-server
並執行 ssh
sudo apt-get install openssh-client openssh-server
sudo /etc/init.d/ssh start
port 的預設值是22
如果沒有安裝ssh就連線, 會看到以下錯誤訊息
ssh: connect to host localhost port 22: Connection refused
2016年7月25日 星期一
Eclipse / macro
Project Macros
This topic describes the various macros that are available for use within projects. They can be used when specifying build command or build options and in general are quite useful when trying to make projects portable and more maintainable.
| Macro | Description |
|---|---|
| ${InputFileName} | Represents the input file name. |
| ${InputFileExt} | Represents the extension of the input file. |
| ${InputFileBaseName} | Represents the base name of the input file. That is the file name with an extension stripped. |
| ${InputFileRelPath} | Represents the input file path relative to the builder current directory. |
| ${InputDirRelPath} | Represents the input file directory path relative to the builder current directory. |
| ${OutputFileName} | Represents the output file name. |
| ${OutputFileExt} | Represents the output file extension. |
| ${OutputFileBaseName} | Represents the output file base name. That is the output file name with an extension stripped. |
| ${OutputFileRelPath} | Represents the output file path relative to the current builder directory. |
| ${OutputDirRelPath} | Represents the output file directory path relative to the current builder directory. |
| ${ConfigName} | Represents the name of a given configuration. |
| ${ConfigDescription} | Represents the description of a given configuration. |
| ${BuildArtifactFileName} | Represents the name of the build artifact. |
| ${BuildArtifactFileExt} | Represents the extension of the build artifact. |
| ${BuildArtifactFileBaseName} | Represents the base name of the build artifact. |
| ${BuildArtifactFilePrefix} | Represents the prefix of the build artifact. |
| ${ProjName} | Represents the name of a given project. |
| ${ProjDirPath} | Represents the absolute path of a given project. |
| ${WorkspaceDirPath} | Represents the workspace absolute path. |
| ${DirectoryDelimiter} | Represents the directory delimiter used on the system. That is the “\? for Win32 systems and the “/? for Unix-like systems. This could be useful, e.g. in the case a user needs the absolute path of an input file. The absolute path would be represented in the following way: ${CWD}${DirectoryDelimiter}${InputFileRelPath} |
| ${PathDelimiter} | Represents the default path delimiter used on the system to separate paths in the path environment variables. That is the “;? for Win32 systems and the “:? for Unix-like systems
This might be used in the environment variable definitions
|
| ${EclipseVersion} | Represents the current Eclipse version. |
| ${CDTVersion} | Represents the current CDT version. |
| ${HostOsName} | Represents the operating system name on which eclipse is running. |
| ${C2000_CG_ROOT} | Installation folder of the C28x code generation tools. |
| ${C5500_CG_ROOT} | Installation folder of the C55x code generation tools. |
| ${C6000_CG_ROOT} | Installation folder of the C6x code generation tools. |
| ${MSP430_CG_ROOT} | Installation folder of the MSP430 code generation tools. |
| ${TMS470_CG_ROOT} | Installation folder of the ARM code generation tools. |
| ${CCS_INSTALL_ROOT} | Root installation directory of CCS. i.e. the directory installed in + /ccsv4 or /ccsv5 |
| ${CG_TOOL_ROOT} | Installation folder of the currently selected code generation tools. Will be the same value as the matching <ISA>_CG_ROOT macro. |
| ${CG_TOOL_SUFFIX} | Contains the ISA of the currently selected code generation tools. i.e. 2000 for C2000. |
2016年6月17日 星期五
caffe fast-rcnn GPU編譯流程
caffe fast-rcnn GPU編譯流程(含python layer)
2015/12/21 | 無
嗨, 大家好今天要來分享的是 fast-rcnn GPU編譯流程
1. 前期準備工作, 安裝相依程式
#下列為C++ 快速安裝方式, 若想要安裝最新版本請至各官網下載自行編譯
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt-get install libatlas-base-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
#下列為C++ 快速安裝方式, 若想要安裝最新版本請至各官網下載自行編譯
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt-get install libatlas-base-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
#下列為python 快速安裝方式
sudo apt-get -y install python-pip
cd ./caffe-fast-rcnn/python
for req in $(cat requirements.txt); do sudo pip install $req; done
#install easydict
wget https://pypi.python.org/packages/source/e/easydict/easydict-1.6.zip
unzip easydict-1.6.zip
cd easydict-1.6
python setup.py install
sudo apt-get -y install python-pip
cd ./caffe-fast-rcnn/python
for req in $(cat requirements.txt); do sudo pip install $req; done
#install easydict
wget https://pypi.python.org/packages/source/e/easydict/easydict-1.6.zip
unzip easydict-1.6.zip
cd easydict-1.6
python setup.py install
#下列為安裝CUDA 流程
#先到官網下載安裝檔
https://developer.nvidia.com/cuda-downloads
#Ctrl + Alt + F1
sudo lightdm stop
sudo dpkg -i cuda-repo-ubuntu1404_7.5-18_amd64.deb
sudo apt-get update
sudo apt-get install cuda
#安裝驅動期間必須要重新開機才能繼續
#先到官網下載安裝檔
https://developer.nvidia.com/cuda-downloads
#Ctrl + Alt + F1
sudo lightdm stop
sudo dpkg -i cuda-repo-ubuntu1404_7.5-18_amd64.deb
sudo apt-get update
sudo apt-get install cuda
#安裝驅動期間必須要重新開機才能繼續
#安裝cuDNN
tar -zxf cudnn-7.0-linux-x64-v3.0-prod.tgz
cd cuda
sudo cp lib64/* /usr/local/cuda/lib64/
sudo cp include/cudnn.h /usr/local/cuda/include/
tar -zxf cudnn-7.0-linux-x64-v3.0-prod.tgz
cd cuda
sudo cp lib64/* /usr/local/cuda/lib64/
sudo cp include/cudnn.h /usr/local/cuda/include/
#build caffe
cp Makefile.config.example Makefile.config
vi Makefile.config
cp Makefile.config.example Makefile.config
vi Makefile.config
#1.Uncomment the line: USE_CUDNN := 1 and WITH_PYTHON_LAYER := 1
#2.Make sure the CUDA_DIR correctly points to our CUDA installation.
#2.Make sure the CUDA_DIR correctly points to our CUDA installation.
make pycaffe -j8
make all -j8
make test -j8
make all -j8
make test -j8
cd ./caffe-fast-rcnn/lib
rm ./nms/*.so
make
rm ./nms/*.so
make
#恭喜完成
parse command line arguments in bash
./myscript.sh -e conf -s /etc -l /usr/lib /etc/hosts
#!/bin/bash
# Use -gt 1 to consume two arguments per pass in the loop (e.g. each
# argument has a corresponding value to go with it).
# Use -gt 0 to consume one or more arguments per pass in the loop (e.g.
# some arguments don't have a corresponding value to go with it such
# as in the --default example).
# note: if this is set to -gt 0 the /etc/hosts part is not recognized ( may be a bug )
while [[ $# -gt 1 ]]
do
key="$1"
case $key in
-e|--extension)
EXTENSION="$2"
shift # past argument
;;
-s|--searchpath)
SEARCHPATH="$2"
shift # past argument
;;
-l|--lib)
LIBPATH="$2"
shift # past argument
;;
--default)
DEFAULT=YES
;;
*)
# unknown option
;;
esac
shift # past argument or value
done
echo FILE EXTENSION = "${EXTENSION}"
echo SEARCH PATH = "${SEARCHPATH}"
echo LIBRARY PATH = "${LIBPATH}"
echo "Number files in SEARCH PATH with EXTENSION:" $(ls -1 "${SEARCHPATH}"/*."${EXTENSION}" | wc -l)
if [[ -n $1 ]]; then
echo "Last line of file specified as non-opt/last argument:"
tail -1 $1
fi
Rotate image around x, y, z axis in OpenCV
記錄一下在網路上找到的資料
http://stackoverflow.com/questions/19093728/rotate-image-around-x-y-z-axis-in-opencv/19110462#19110462
可以對圖片做x , y , z 三軸旋轉
http://stackoverflow.com/questions/19093728/rotate-image-around-x-y-z-axis-in-opencv/19110462#19110462
可以對圖片做x , y , z 三軸旋轉
Finally found a way, thanks to this post:https://plus.google.com/103190342755104432973/posts/NoXQtYQThgQ
I let OpenCV calculate the matrix for me, but I'm doing the perspective transform myself (found it easier to implement than putting everything into a cv::Mat)
float rotx, roty, rotz; // set these first
int f = 2; // this is also configurable, f=2 should be about 50mm focal length
int h = img.rows;
int w = img.cols;
float cx = cosf(rotx), sx = sinf(rotx);
float cy = cosf(roty), sy = sinf(roty);
float cz = cosf(rotz), sz = sinf(rotz);
float roto[3][2] = { // last column not needed, our vector has z=0
{ cz * cy, cz * sy * sx - sz * cx },
{ sz * cy, sz * sy * sx + cz * cx },
{ -sy, cy * sx }
};
float pt[4][2] = {{ -w / 2, -h / 2 }, { w / 2, -h / 2 }, { w / 2, h / 2 }, { -w / 2, h / 2 }};
float ptt[4][2];
for (int i = 0; i < 4; i++) {
float pz = pt[i][0] * roto[2][0] + pt[i][1] * roto[2][1];
ptt[i][0] = w / 2 + (pt[i][0] * roto[0][0] + pt[i][1] * roto[0][1]) * f * h / (f * h + pz);
ptt[i][1] = h / 2 + (pt[i][0] * roto[1][0] + pt[i][1] * roto[1][1]) * f * h / (f * h + pz);
}
cv::Mat in_pt = (cv::Mat_<float>(4, 2) << 0, 0, w, 0, w, h, 0, h);
cv::Mat out_pt = (cv::Mat_<float>(4, 2) << ptt[0][0], ptt[0][1],
ptt[1][0], ptt[1][1], ptt[2][0], ptt[2][1], ptt[3][0], ptt[3][1]);
cv::Mat transform = cv::getPerspectiveTransform(in_pt, out_pt);
cv::Mat img_in = img.clone();
cv::warpPerspective(img_in, img, transform, img_in.size());
2016年6月15日 星期三
Ubuntu 14.04 install boost
如何安裝 boost 到 ubuntu 14.04
1. Download boost
http://www.boost.org/
2. cd /path/boost/
sudo apt-get update
sudo apt-get install build-essential g++ python-dev autotools-dev libicu-dev build- essential libbz2-dev libboost-all-dev
./bootstrap.sh --prefix=/usr/local
sudo ./b2 install
3. sudo apt-get install libboost-all-dev
1. Download boost
http://www.boost.org/
2. cd /path/boost/
sudo apt-get update
sudo apt-get install build-essential g++ python-dev autotools-dev libicu-dev build- essential libbz2-dev libboost-all-dev
./bootstrap.sh --prefix=/usr/local
sudo ./b2 install
3. sudo apt-get install libboost-all-dev
2016年6月7日 星期二
Eclipse C++ 打包
在程式的主資料夾建立folder : build
裡面放一個執行檔 build.sh
***
WORKDIR=$(cd "$(dirname "$0")"; pwd)
cd $WORKDIR/../
#將系統內建的也打包進去
find . -type f -perm /a+x -exec ldd {} \; | grep so | sed -e '/^[^\t]/ d' | sed -e 's/\t//' | sed -e 's/.*=..//' | sed -e 's/ (0.*)//' | sort | sed '/^\s*$/d' | uniq | sed -e '/x86_64-linux-gnu/d' | sed -e '/lib64/d' | sed 's/^/sudo cp /' | sed 's?$? '`pwd`/lib'?' >> copy.sh
#find . -type f -perm /a+x -exec ldd {} \; | grep so | sed -e '/^[^\t]/ d' | sed -e 's/\t//' | sed -e 's/.*=..//' | sed -e 's/ (0.*)//' | sort | sed '/^\s*$/d' | uniq | sed 's/^/sudo cp /' | sed 's?$? '`pwd`/lib'?' >> copy.sh
mkdir lib
chmod +x copy.sh
./copy.sh
rm copy.sh
***
註解的不將系統內建的連結檔放入lib
裡面放一個執行檔 build.sh
***
WORKDIR=$(cd "$(dirname "$0")"; pwd)
cd $WORKDIR/../
#將系統內建的也打包進去
find . -type f -perm /a+x -exec ldd {} \; | grep so | sed -e '/^[^\t]/ d' | sed -e 's/\t//' | sed -e 's/.*=..//' | sed -e 's/ (0.*)//' | sort | sed '/^\s*$/d' | uniq | sed -e '/x86_64-linux-gnu/d' | sed -e '/lib64/d' | sed 's/^/sudo cp /' | sed 's?$? '`pwd`/lib'?' >> copy.sh
#find . -type f -perm /a+x -exec ldd {} \; | grep so | sed -e '/^[^\t]/ d' | sed -e 's/\t//' | sed -e 's/.*=..//' | sed -e 's/ (0.*)//' | sort | sed '/^\s*$/d' | uniq | sed 's/^/sudo cp /' | sed 's?$? '`pwd`/lib'?' >> copy.sh
mkdir lib
chmod +x copy.sh
./copy.sh
rm copy.sh
***
註解的不將系統內建的連結檔放入lib
2016年5月9日 星期一
NO_PUBKEY 575159689BEFB442
Ubuntu 14.04 更新的時候出現了
"Error GPG: http://download.fpcomplete.com stable Release: NO_PUBKEY 575159689BEFB442"
解決方式:
"Error GPG: http://download.fpcomplete.com stable Release: NO_PUBKEY 575159689BEFB442"
解決方式:
wget -q -O- https://s3.amazonaws.com/download.fpcomplete.com/ubuntu/fpco.key | sudo apt-key add -
2016年5月4日 星期三
Sed 小應用:刪除行尾(首)的最後一個字元
刪除每一行的最後一個字元
sed 's/.$//'
".$" 代表著行尾的前一個任意字元
利用sed來替換文字
sed's/想要被替換的字/替換的字/'
行首
sed 's/^/string/' 行首用'string'代替
2016年5月3日 星期二
google protobuff 3.0.0, ImportError: cannot import name symbol_database
安裝好caffe之後 進行演算法偵測
**
protobuf-3.0.0 有一個bug
按照以下網址修改
作者:xfxyjwf
https://github.com/xfxyjwf/protobuf/blob/494716a682ef854168e92231a3cdcc89d587d9b9/src/google/protobuf/arena.h
出現了 "ImportError: cannot import name symbol_database"
解決的方法是移除default的 python-protobuf
$: sudo apt-get remove python-protobuf
**
protobuf-3.0.0 有一個bug
按照以下網址修改
作者:xfxyjwf
https://github.com/xfxyjwf/protobuf/blob/494716a682ef854168e92231a3cdcc89d587d9b9/src/google/protobuf/arena.h
重新編譯google-protobuf即可
無法取得以下的密鑰ID 的公鑰: 1397BC53640DB551
這是google chrome的問題
wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
That will fix it
Source:
https://www.reddit.com/r/linux4noobs...no_public_key/
wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
That will fix it
Source:
https://www.reddit.com/r/linux4noobs...no_public_key/
2016年3月24日 星期四
Multiple “could not be resolved” problems using Eclipse
最近用eclipse的時候遇到一個很奇怪的問題
我在使用C++11的新功能 chrono的時候 會發現我已經更改了
Properties > C/C++ Build > Setting > GCC C++ Complier
> Dialet > Language standard = ISO C++1y (-std=c++1y)
但在build project的時候, 會出現很多很多error, 雖然最後仍然可以成功的build出程式
可是無法使用eclipse 來執行程式, 只能用terminate 來啟動
後來上網查到解決辦法
***
Go to
The eclipse live code analysis does not share the same settings with the build compiler. You can also change the setting globally (not just for the project) in
**
更改完這些設定之後Rebuild project
錯誤訊息就會消失了!
我在使用C++11的新功能 chrono的時候 會發現我已經更改了
Properties > C/C++ Build > Setting > GCC C++ Complier
> Dialet > Language standard = ISO C++1y (-std=c++1y)
但在build project的時候, 會出現很多很多error, 雖然最後仍然可以成功的build出程式
可是無法使用eclipse 來執行程式, 只能用terminate 來啟動
後來上網查到解決辦法
***
Go to
Project -> Properties -> C/C++ General -> Preprocessor Include Paths, Macros, etc. -> Providers -> CDT GCC built-in compiler settings, deactivate Use global provider shared between projects and add the command line argument -std=c++11.The eclipse live code analysis does not share the same settings with the build compiler. You can also change the setting globally (not just for the project) in
Window -> Preferences -> C/C++ -> Build -> Settings -> CDT GCC Built-in Compiler Settings.**
更改完這些設定之後Rebuild project
錯誤訊息就會消失了!
2016年3月15日 星期二
python-check there is a existing file in a folder
import os
bIsExist = os.path.exists(file_path)
if there is a file with same path, then bIsExist is True.
bIsExist = os.path.exists(file_path)
if there is a file with same path, then bIsExist is True.
2015年12月29日 星期二
2015年12月25日 星期五
Linux指令 -- grep
Linux系統中grep命令是一種強大的文本搜索工具,它能使用規則運算式搜索文本,並把匹配的行列印出來。grep全稱是Global Regular Expression Print,表示全域規則運算式版本,它的使用權限是所有用戶。 grep的工作方式是這樣的,它在一個或多個檔案中搜索字串範本。如果範本包括空格,則必須被引用,範本後的所有字串被看作檔案案名。搜索的結果被送到標準輸出,不影響原文件內容。 grep可用於shell腳本,因為grep通過返回一個狀態值來說明搜索的狀態,如果範本搜索成功,則返回0,如果搜索不成功,則返回1,如果搜索的檔案不存在,則返回2。我們利用這些返回值就可進行一些自動化的文本處理工作。
1.命令格式: grep [option] pattern file
2.命令功能: 用於過濾/搜索的特定字元。可使用規則運算式能多種命 令配合使用,使用上十分靈活。
3.命令參數:-a --text #不要忽略二進位的資料。 -A<顯示行數> --after-context=<顯示行數> #除了顯示符合範本樣式的那一列之外,並顯示該行之後的 內容。 -b --byte-offset #在顯示符合樣式的那一行之前,標示出該行第一個字元的 編號。 -B<顯示行數> --before-context=<顯示行數> #除顯示符合樣式的那一行外,並顯示該行之前內容。 -c --count #計算符合樣式的列數。 -C<顯示行數> --context=<顯示行數> #除了顯示符合樣式的那一行之外,並顯示該行之前後 的內容。
-d <動作> --directories=<動作> #當指定要查找的是目錄而非檔案時,必須使用這項參數, 否則grep指令將回報資訊並停止動作。
-e<範本樣式> --regexp=<範本樣式> #指定字串做為查找檔案內容的樣式。 -E --extended-regexp #將樣式為延伸的普通標記法來使用。 -f<規則檔案> --file=<規則檔案> #指定規則檔案,其內容含有一個或多個規則樣式,讓grep 查找符合規則條件的檔案內容,格式為每行一個規則樣 式。 -F --fixed-regexp #將樣式視為固定字串的清單。 -G --basic-regexp #將樣式視為普通的標記法來使用。 -h --no-filename #在顯示符合樣式的那一行之前,不標示該行所屬的檔案案名稱。 -H --with-filename #在顯示符合樣式的那一行之前,表示該行所屬的檔案案名稱。 -i --ignore-case #忽略字元大小寫的差別。 -l --file-with-matches #列出檔案內容符合指定的樣式的檔案案名稱。
-L --files-without-match #列出檔案內容不符合指定的樣式的檔案案名稱。 -n --line-number #在顯示符合樣式的那一行之前,標示出該行的列數編號。 -q --quiet或--silent #不顯示任何資訊。 -r --recursive #此參數的效果和指定“-d recurse”參數相同。 -s --no-messages #不顯示錯誤資訊。 -v --revert-match #顯示不包含匹配文本的所有行。(排除/反向/過濾/反向過濾 有包含字串就不要抓出來)
-V --version #顯示版本資訊。 -w --word-regexp #只顯示全字符合的列。 -x --line-regexp #只顯示全列符合的列。 -y #此參數的效果和指定“-i”參數相同。 4.規則運算式:grep的規則運算式:^ #錨定行的開始 如:'^grep'匹配所有以grep開頭的行。 $ #錨定行的結束 如:'grep$'匹配所有以grep結尾的行。 . #匹配一個非分行符號的字元 如:'gr.p'匹配gr後接一個任意字元,然後是p。 * #匹配零個或多個先前字元 如:'*grep'匹配所有一個或多個空格後緊跟grep的行。 .* #一起用代表任意字元。
1.命令格式: grep [option] pattern file
2.命令功能: 用於過濾/搜索的特定字元。可使用規則運算式能多種命 令配合使用,使用上十分靈活。
3.命令參數:-a --text #不要忽略二進位的資料。 -A<顯示行數> --after-context=<顯示行數> #除了顯示符合範本樣式的那一列之外,並顯示該行之後的 內容。 -b --byte-offset #在顯示符合樣式的那一行之前,標示出該行第一個字元的 編號。 -B<顯示行數> --before-context=<顯示行數> #除顯示符合樣式的那一行外,並顯示該行之前內容。 -c --count #計算符合樣式的列數。 -C<顯示行數> --context=<顯示行數> #除了顯示符合樣式的那一行之外,並顯示該行之前後 的內容。
-d <動作> --directories=<動作> #當指定要查找的是目錄而非檔案時,必須使用這項參數, 否則grep指令將回報資訊並停止動作。
-e<範本樣式> --regexp=<範本樣式> #指定字串做為查找檔案內容的樣式。 -E --extended-regexp #將樣式為延伸的普通標記法來使用。 -f<規則檔案> --file=<規則檔案> #指定規則檔案,其內容含有一個或多個規則樣式,讓grep 查找符合規則條件的檔案內容,格式為每行一個規則樣 式。 -F --fixed-regexp #將樣式視為固定字串的清單。 -G --basic-regexp #將樣式視為普通的標記法來使用。 -h --no-filename #在顯示符合樣式的那一行之前,不標示該行所屬的檔案案名稱。 -H --with-filename #在顯示符合樣式的那一行之前,表示該行所屬的檔案案名稱。 -i --ignore-case #忽略字元大小寫的差別。 -l --file-with-matches #列出檔案內容符合指定的樣式的檔案案名稱。
-L --files-without-match #列出檔案內容不符合指定的樣式的檔案案名稱。 -n --line-number #在顯示符合樣式的那一行之前,標示出該行的列數編號。 -q --quiet或--silent #不顯示任何資訊。 -r --recursive #此參數的效果和指定“-d recurse”參數相同。 -s --no-messages #不顯示錯誤資訊。 -v --revert-match #顯示不包含匹配文本的所有行。(排除/反向/過濾/反向過濾 有包含字串就不要抓出來)
-V --version #顯示版本資訊。 -w --word-regexp #只顯示全字符合的列。 -x --line-regexp #只顯示全列符合的列。 -y #此參數的效果和指定“-i”參數相同。 4.規則運算式:grep的規則運算式:^ #錨定行的開始 如:'^grep'匹配所有以grep開頭的行。 $ #錨定行的結束 如:'grep$'匹配所有以grep結尾的行。 . #匹配一個非分行符號的字元 如:'gr.p'匹配gr後接一個任意字元,然後是p。 * #匹配零個或多個先前字元 如:'*grep'匹配所有一個或多個空格後緊跟grep的行。 .* #一起用代表任意字元。
[] #匹配一個指定範圍內的字元,如'[Gg]rep'匹配Grep和grep。 [^] #匹配一個不在指定範圍內的字元,如:'[^A-FH-Z]rep'匹配不包含A-R和T-Z的一個字母開頭,緊跟rep的行。
\(..\) #標記匹配字元,如'\(love\)',love被標記為1。
\< #錨定單詞的開始,如:'\<grep'匹配包含以grep開頭的單詞的行。
\> #錨定單詞的結束,如'grep\>'匹配包含以grep結尾的單詞的行。
x\{m\} #重複字元x,m次,如:'0\{5\}'匹配包含5個o的行。
x\{m,\} #重複字元x,至少m次,如:'o\{5,\}'匹配至少有5個o的行。
x\{m,n\} #重複字元x,至少m次,不多於n次,如:'o\{5,10\}'匹配5--10個o的行。
\w #匹配文字和數位字元,也就是[A-Za-z0-9],如:'G\w*p'匹配以G後跟零個或多個文字或數位字元,然後是p。
\W #\w的反置形式,匹配一個或多個非單詞字元,如點號句號等。
\b #單詞鎖定符,如: '\bgrep\b'只匹配grep。
POSIX字元:為了在不同國家的字元編碼中保持一至,POSIX(The Portable Operating System Interface)增加了特殊的字元類,如[:alnum:]是[A-Za-z0-9]的另一個寫法。要把它們放到[]號內才能成為規則運算式,如[A- Za-z0-9]或[[:alnum:]]。在linux下的grep除fgrep外,都支援POSIX的字元類。[:alnum:] #文字數位字元 [:alpha:] #文字字元 [:digit:] #數位字元 [:graph:] #非空字元(非空格、控制字元) [:lower:] #小寫字元 [:cntrl:] #控制字元 [:print:] #非空字元(包括空格) [:punct:] #標點符號 [:space:] #所有空白字元(新行,空格,定位字元) [:upper:] #大寫字元 [:xdigit:] #十六進位數字(0-9,a-f,A-F)
5.使用實例:
實例1:查找指定進程(程式)命令:ps -ef|grep svn輸出:[root@localhost ~]# ps -ef|grep svnroot 4943 1 0 Dec05 ? 00:00:00 svnserve -d -r /opt/svndata/grape/root 16867 16838 0 19:53 pts/0 00:00:00 grep svn[root@localhost ~]#說明:第一條記錄是查找出的進程(程式);第二條結果是grep進程(程式)本身,並非真正要找的進程(程式)。
實例2:查找指定進程(程式)個數命令:ps -ef|grep svn -cps -ef|grep -c svn輸出:[root@localhost ~]# ps -ef|grep svn -c2[root@localhost ~]# ps -ef|grep -c svn 2[root@localhost ~]#說明:
實例3:從檔案中讀取關鍵字進行搜索命令:cat test.txt | grep -f test2.txt輸出:[root@localhost test]# cat test.txt hnlinuxpeida.cnblogs.comubuntuubuntu linuxredhatRedhatlinuxmint[root@localhost test]# cat test2.txt linuxRedhat[root@localhost test]# cat test.txt | grep -f test2.txthnlinuxubuntu linuxRedhatlinuxmint[root@localhost test]#說明:輸出test.txt文件中含有從test2.txt檔案中讀取出的關鍵字的內容行
實例4:從檔案中讀取關鍵字進行搜索 且顯示行號命令:cat test.txt | grep -nf test2.txt輸出:[root@localhost test]# cat test.txt hnlinuxpeida.cnblogs.comubuntuubuntu linuxredhatRedhatlinuxmint[root@localhost test]# cat test2.txt linuxRedhat[root@localhost test]# cat test.txt | grep -nf test2.txt1:hnlinux4:ubuntu linux6:Redhat7:linuxmint[root@localhost test]#說明:輸出test.txt文件中含有從test2.txt檔案中讀取出的關鍵字的內容行,並顯示每一行的行號
實例5:從檔案中查找關鍵字命令:grep 'linux' test.txt輸出:[root@localhost test]# grep 'linux' test.txt hnlinuxubuntu linuxlinuxmint[root@localhost test]# grep -n 'linux' test.txt 1:hnlinux4:ubuntu linux7:linuxmint[root@localhost test]#說明:實例6:從多個檔案中查找關鍵字命令:grep 'linux' test.txt test2.txt輸出:[root@localhost test]# grep -n 'linux' test.txt test2.txt test.txt:1:hnlinuxtest.txt:4:ubuntu linuxtest.txt:7:linuxminttest2.txt:1:linux[root@localhost test]# grep 'linux' test.txt test2.txt test.txt:hnlinuxtest.txt:ubuntu linuxtest.txt:linuxminttest2.txt:linux[root@localhost test]#說明:多檔案時,輸出查詢到的資訊內容行時,會把檔案的命名在行最前面輸出並且加上":"作為標示符
實例7:grep不顯示本身進程(程式)命令:ps aux|grep \[s]shps aux | grep ssh | grep -v "grep"輸出:[root@localhost test]# ps aux|grep sshroot 2720 0.0 0.0 62656 1212 ? Ss Nov02 0:00 /usr/sbin/sshdroot 16834 0.0 0.0 88088 3288 ? Ss 19:53 0:00 sshd: root@pts/0 root 16901 0.0 0.0 61180 764 pts/0 S+ 20:31 0:00 grep ssh[root@localhost test]# ps aux|grep \[s]sh][root@localhost test]# ps aux|grep \[s]shroot 2720 0.0 0.0 62656 1212 ? Ss Nov02 0:00 /usr/sbin/sshdroot 16834 0.0 0.0 88088 3288 ? Ss 19:53 0:00 sshd: root@pts/0 [root@localhost test]# ps aux | grep ssh | grep -v "grep"root 2720 0.0 0.0 62656 1212 ? Ss Nov02 0:00 /usr/sbin/sshdroot 16834 0.0 0.0 88088 3288 ? Ss 19:53 0:00 sshd: root@pts/0說明:
實例8:找出已u開頭的行內容命令:cat test.txt |grep ^u輸出:[root@localhost test]# cat test.txt |grep ^uubuntuubuntu linux[root@localhost test]#說明:
實例9:輸出非u開頭的行內容命令:cat test.txt |grep ^[^u]輸出:[root@localhost test]# cat test.txt |grep ^[^u]hnlinuxpeida.cnblogs.comredhatRedhatlinuxmint[root@localhost test]#說明:
實例10:輸出以hat結尾的行內容命令:cat test.txt |grep hat$輸出:[root@localhost test]# cat test.txt |grep hat$redhatRedhat[root@localhost test]#說明:
實例11:顯示IP、Bcast和Mask相關資訊命令:ifconfig eth0|grep "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}輸出:[root@localhost test]# ifconfig eth0|grep "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0[root@localhost test]# ifconfig eth0|grep -E "([0-9]{1,3}\.){3}[0-9]" inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0[root@localhost test]#說明:
實例12:顯示包含ed或者at字元的內容行命令:cat test.txt |grep -E "ed|at"輸出:[root@localhost test]# cat test.txt |grep -E "peida|com"peida.cnblogs.com[root@localhost test]# cat test.txt |grep -E "ed|at"redhatRedhat[root@localhost test]#說明:
實例13:顯示目前的目錄下面以.txt 結尾的檔案中的所有包含每個字串至少有7個連續小寫字元的字串的行命令:grep '[a-z]\{7\}' *.txt輸出:[root@localhost test]# grep '[a-z]\{7\}' *.txttest.txt:hnlinuxtest.txt:peida.cnblogs.comtest.txt:linuxmint[root@localhost test]#
訂閱:
文章 (Atom)