egrep

1. Basic grammar
    egrep = grep -E
    egrep [OPTIONS] PATTERN [FILE...]
2. Meta-charecter of extended regexp
    2.1 Character match
        .:  Matchs any single character
        []: Matchs any single character within specified range
        [^]:Matchs any signal character without specified range
    2.2 Matched number
        *:Matchs The preceding item zero or more times, refers to only times.
        .* : Matchs any item -----Any character of any length
        ? : Matchs the preceding item zero or at most one times.
        + : Matchs the preceding item at lest one times.
        {m} : The preceding item is matched exactly m times
        {m,n} : The preceding item is matched at lest m times and at most n times.
        {0,n} : he preceding item is matched at most n times
        {m,} : he preceding item is matched at lest m times
    2.3 Anchoring
        ^ : Only matchs the PATTERN at the begining of a line.
        $ : Only matchs the PATTERN at the end of a line.
        ^PATTERN$ : Matchs the entire line.
        ^$ : Matchs blank lines
        ^[[:space:]]*$ : Matchs blank lines
            
        \< or \b:Matchs at the beginning of a word
        \> or \b:Matchs at the end of a word
        \<PATTERN\>:Matchs the entire word
    2.4 group
        () : (xy)*ab  --  Matchs 'xy' zero or more times.
    2.5 reference : The same grammar as grep.
3. Exercise :
    1) Print the default shell and UID of the root/centos/user1 user
        # grep -E '^(root|centos|user1)\>' /etc/passwd | cut -d: -f1,3,7
    2) Print the lines which have a word following with parentheses.
        # grep -E -o "^[_[:alpha:]]+\(\)" /etc/rc.d/init.d/functions
    3) echo an absolute path and print its base name with egrep;
        # echo "/mnt/sdc" | grep -E -o "[^/]+/?$" | cut -d"/" -f1
        further:print the directory of the path,just like the result of command dirname
    4) find values between 1-255 of the result of command ifconfig 找出ifconfig命令结果中1-255之间的数值;
    5) 找出ifconfig命令结果中的IP地址;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Linux中的egrep是一个命令行工具,用于在文件中搜索匹配某个模式的行。它支持正则表达式,并且可以通过选项进行更多的控制和定制。egrepgrep命令的一个变体,它使用扩展正则表达式语法,可以更灵活地匹配文本。 ### 回答2: linux中的egrep命令是一个强大的文本搜索工具,用于在文件中查找匹配指定模式的文本行。 egrep命令支持使用正则表达式进行模式匹配。正则表达式是一种灵活且强大的文本模式匹配技术,可以用于匹配特定的字符串模式。 egrep命令可以接收一个或多个文件作为输入,并在这些文件中搜索匹配指定模式的文本行。它能够根据正则表达式匹配模式来过滤出符合条件的文本行,并将这些行输出到标准输出或指定文件。 egrep命令的语法为:egrep [选项] 模式 [文件] 其中,选项可以用于指定搜索的方式,如忽略大小写、显示匹配行的行号等。 egrep命令还支持使用一些特殊字符来扩展模式匹配的功能。例如,使用"."匹配除了换行符之外的任意字符,使用"*"匹配前面模式的零个或多个重复,使用"|"表示或逻辑等。 egrep命令可以与其他命令结合使用,扩展其功能。例如,可以将egrep的输出通过管道传递给其他命令进行进一步处理,或者将输出重定向到文件中保存。 总之,egrep是一个功能强大的文本搜索工具,可以通过使用正则表达式来进行模式匹配,从文件中过滤出符合条件的文本行。它在Linux系统中被广泛应用于文本处理、日志分析等场景。 ### 回答3: egrep是Linux命令中的一个强大的文本搜索工具,它用于在文件中查找符合指定模式的行。 使用egrep命令时,可以指定一个或多个模式,它会逐行扫描文件内容,并将匹配模式的行输出到终端。 egrep命令的语法如下: egrep [选项] 模式 [文件...] 选项: - i:忽略大小写 - v:只输出不匹配的行 - w:只匹配整个单词 - c:仅输出匹配行的计数 egrep支持使用正则表达式作为模式,可以使用常见的正则表达式元字符和特殊字符来构建复杂的模式匹配规则。 例如,如果我们想要在文件中查找所有包含单词"hello"的行,可以使用以下命令: egrep "hello" file.txt 如果我们想要忽略大小写进行匹配,可以添加-i选项: egrep -i "hello" file.txt 如果我们只想输出不包含"world"的行,可以使用-v选项: egrep -v "world" file.txt 另外,egrep还可以通过管道符号(|)结合其他Linux命令来实现更强大的文本处理功能。 总结来说,egrep是一个功能强大的文本搜索工具,通过灵活的模式匹配功能,可以帮助我们快速定位和处理文件中的特定文本行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值