1

I want to verify in file the following occurrence lines ,

/var/log/report
{
<any string>

the first line should be - /var/log/report

second - {

third - is any word/string (A-Za-z)

so on each occurrence lines we get OK in output , with the match lines

/var/log/report
{
<any string>

example

# 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

expected output

OK
        /var/log/report
        {
        fkergfve
OK
        /var/log/report
        {
         kfreve
OK
        /var/log/report
        {
        jdhe
2
  • post the expected result for multiple matches Feb 18 '18 at 12:44
  • see my update example + expected results
    – yael
    Feb 18 '18 at 12:48
1

Awk solution (for strict order and format of crucial pattern lines):

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

The output:

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

gnu sed solution

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

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .