char greeting[] = "Hello" char *p = greeting; //non const point, const data const char * p = greeting; //non const point, const data char const * p = greeting; //non const point, const data char * const p = greeting; //const point, non const data const char* const p = greeting; //const point, const data
const std::vector<int>::iterator it; // --> T * const p *it = 10; //合法 it++; //不合法 std::vector<int>::const_iterator it; // --> const T * p it = 10; //不合法 it++; //合法
std::shared<T> p1; std::shared<const T> p; // --> const T *p p = p1; //合法 *p.get() = x; //不合法 const std::shared<T> p; // --> T* const p p = p1; //不合法 *p.get() = x; //合法
Shall I compare thee to a summer's day? Thou art more lovely and more temperate: Rough winds do shake the darling buds of May, And summer's lease hath all too short a date:
Sometime too hot the eye of heaven shines, And often is his gold complexion dimm'd; And every fair from fair sometime declines, By chance or nature's changing course untrimm'd
But thy eternal summer shall not fade Nor lose possession of that fair thou owest; Nor shall Death brag thou wander'st in his shade, When in eternal lines to time thou growest:
So long as men can breathe or eyes can see, So long lives this and this gives life to thee. $ cp demofile demofile2
在文件中搜索某个字符串。
1 2 3 4
$ grep "and" demofile Thou art more lovely and more temperate: Nor shall Death brag thou wander'st in his shade, So long lives this and this gives life to thee.
$ grep -w "and" demofile Thou art more lovely and more temperate: So long lives this and this gives life to thee.
忽略大小写
1 2 3 4 5 6
$ grep -i -w "and" demofile Thou art more lovely and more temperate: And summer's lease hath all too short a date: And often is his gold complexion dimm'd; And every fair from fair sometime declines, So long lives this and this gives life to thee.
使用正则表达式
只要把匹配的字符串换成需要的正则表达式即可。比如说需要搜索以And开头的行。
1 2 3 4
$ grep "^And" demofile And summer's lease hath all too short a date: And often is his gold complexion dimm'd; And every fair from fair sometime declines,
显示上下文
有时候搜出来需要看看上下文,可以利用上面提到的-B,-A和-C来实现。
比如想要找出包含”nature”的句子并打印出上下两行。
1 2 3 4 5 6
$ grep -C2 "nature" demofile And often is his gold complexion dimm'd; And every fair from fair sometime declines, By chance or nature's changing course untrimm'd
$ grep -E "heaven|nature" demofile Sometime too hot the eye of heaven shines, By chance or nature's changing course untrimm'd
复杂匹配多个模式
需要找出符合多个模式的匹配。比如说,找出以”When”开头或者包含”fade”的行。
1 2 3
$ grep -e "^When" -e "fade" demofile But thy eternal summer shall not fade When in eternal lines to time thou growest:
找出不满足条件的匹配
在一些情况下我们需要找出不满足某个条件的行,这个时候就可以用-v来实现了。
比如说想找出首字母不是大写字母的行。
1 2
$ grep -v "^[A-Z]" demofile sonnet 18
打印出匹配所在行的行数
1 2 3
$ grep -e "^When" -e "fade" -n demofile 13:But thy eternal summer shall not fade 16:When in eternal lines to time thou growest:
打印出文件名
进行多个文件搜索的情况下,默认会打印出文件名。
1 2 3 4 5
$ grep -e "^When" -e "fade" demofile demofile2 demofile:But thy eternal summer shall not fade demofile:When in eternal lines to time thou growest: demofile2:But thy eternal summer shall not fade demofile2:When in eternal lines to time thou growest: