如何验证文件中是否出现 3 行

如何验证文件中是否出现 3 行

我想验证文件中出现以下行,

/var/log/report
{
<any string>

第一行应该是 - /var/日志/报告

第二 -{

第三个 - 是任何单词/字符串 (A-Za-z)

所以在每个出现的行上,我们在输出中都得到了 OK 的结果,并且有匹配行

/var/log/report
{
<any string>

例子

# Un-comment the following to provide a specific roving profile share.
# The default is to use the user's home directory:
;       [Profiles]
;       path = /var/lib/samba/profiles
;       browseable = no
;       guest ok = yes



    /var/log/report
    {
    fkergfve

# A publicly accessible directory that is read only, except for users in the
# "staff" group (which have write permissions):
;       [public]
;       comment = Public Stuff
;       path = /home/samba
;       public = yes
;       writable = yes
;       printable = no
;       write list = +staff



    /var/log/report
    {
     kfreve

# "staff" group (which have write permissions):
;       [public]
;       comment = Public Stuff
;       path = /home/samba
;       public = yes
;       writable = yes
;       printable = no
;       write list = +staff


    /var/log/report
    {
    jdhe

预期产出

OK
        /var/log/report
        {
        fkergfve
OK
        /var/log/report
        {
         kfreve
OK
        /var/log/report
        {
        jdhe

答案1

Awk解决方案(对于关键图案线的严格顺序和格式):

awk 'NF == 1{ 
         if (f) { 
             if (NR-n == 1 && $1 == "{")
                 r = r ORS $0;
             else if (NR-n == 2 && $0 ~ /[a-zA-Z]+/)
                 print "OK" ORS r ORS $0; 
         }
         if ($1 == "/var/log/report") { 
             f = 1; n = NR; r = $0 
         }
     }n && NR-n > 2{ f = 0 }' file

输出:

OK
    /var/log/report
    {
    fkergfve
OK
    /var/log/report
    {
     kfreve
OK
    /var/log/report
    {
    jdhe

答案2

gnu sed 解决方案

sed '
\#[[:blank:]]*/var/log/report#!d
N
/\n[[:blank:]]*{$/!d
N
/\n[[:blank:]]*[A-Za-z]*$/!d
s/.*/OK\n&/
' infile

相关内容