파일에 3줄이 나타나는지 확인하는 방법

파일에 3줄이 나타나는지 확인하는 방법

파일에 다음 줄이 있는지 확인하고 싶습니다.

/var/log/report
{
<any string>

첫 번째 줄은 - /var/log/reports

두번째 -{

세 번째 - 아무거나단어/문자열(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

관련 정보