#每日Linux小练习#03 正则表达式

正则表达式(Regular Expression ,RE)是处理字符串的方法,它是以为单位来进行字符串的处理行为,

通过一些特殊符号的辅助,可以让用户轻易达到查找,删除,替换某特定字符串的处理程序。

#!/bin/bash

if [ $# == 0 ];then
    echo "Please input parameters:"
    echo "dmesg OR txt"
fi

if [ "$1" == "dmesg" ];then
    dmesg | grep 'eth'
    echo "******************"
    dmesg | grep -A1 -B2 'eth'
    echo "******************"
    dmesg | grep -A1 -B2  -n --color=auto 'eth'
fi

if [ "$1" == "txt" ];then
    echo "**********************lines with 'the'***********************"
    grep -n --color=auto 'the' ./material/regular_express.txt
    echo "**********************lines without 'the'********************"
    grep -v -n --color=auto 'the' ./material/regular_express.txt
    echo "**********************lines with 'the'(no matter lower or bigger)********************"
    grep -i -n --color=auto 'the' ./material/regular_express.txt
    echo "**********************lines with 't[ae]st'********************"
    grep -n --color=auto 't[ae]st' ./material/regular_express.txt
    echo "**********************lines with 'oo'********************"
    grep -n --color=auto 'oo' ./material/regular_express.txt
    echo "**********************lines without 'oo'starting with g ********************"
    grep -n --color=auto '[^g]oo' ./material/regular_express.txt
    echo "**********************lines without 'oo'starting with a-z ********************"
    grep -n --color=auto '[^a-z]oo' ./material/regular_express.txt
    echo "**********************lines without 'oo'starting with lower ********************"
    grep -n --color=auto '[^[:lower:]]oo' ./material/regular_express.txt
    echo "**********************lines without digit ********************"
    grep -n --color=auto '[^[:digit:]]' ./material/regular_express.txt
    echo "**********************lines starting with 'the' ********************"
    grep -n --color=auto '^the' ./material/regular_express.txt
    echo "**********************lines starting with lower ********************"
    grep -n --color=auto '^[a-z]' ./material/regular_express.txt
    echo "**********************lines not starting with character ********************"
    grep -n --color=auto '^[^a-zA-Z]' ./material/regular_express.txt
    echo "**********************lines ending with '.' ********************"
    grep -n --color=auto '.$' ./material/regular_express.txt
    echo "**********************display lines which are not empty and not begining with #  ********************"
    grep -v -n --color=auto '^$' ./material/regular_express.txt | grep -v -n '^#'
    echo "**********************lines with g..d  ********************"
    grep -n --color=auto 'g..d' ./material/regular_express.txt
    echo "**********************lines with no less than 2 oo (* means 0 or more) ********************"
    grep -n --color=auto 'ooo*' ./material/regular_express.txt 
    echo "**********************lines with string which begins with g and ends with g********************"
    grep -n --color=auto 'g.*g' ./material/regular_express.txt 
    echo "**********************lines with 2-5 o in go..g ********************"
    grep -n --color=auto 'go{2,5}g' ./material/regular_express.txt 
    echo "**********************lines with 2 o in go..g ********************"
    grep -n --color=auto 'go{2}g' ./material/regular_express.txt 
    echo "**********************lines with 2 or more o in go..g ********************"
    grep -n --color=auto 'go{2,}g' ./material/regular_express.txt 
fi
原文地址:https://www.cnblogs.com/wuqi/p/4709555.html