Can awk print lines that do not have a pattern?

Yes, just use any non-zero number and awk will do its default thing which is to print the line:

awk '7' file

If you want it as an "else", put "next" after whatever lines you select for special processing so this one isn't executed for them too.

awk '/pattern/{special processing; next} 7' file

You can do:

awk '/pattern/ {do something with line} 1' file

Here the 1 will print all lines, both the changed and the not changed line.


Just to show the solution Askan posted using else if

awk '{
    if (/pattern/)
        print "Line number:",NR,"pattern matched"
    else if (/Second/) 
        print "Line number:",NR,"Second matched"
    else 
        print "Line number:",NR,"Another line matched"
    }' file

You can negate the pattern to get else like behavior:

awk '
    /pattern/ {
        # custom block to print the line    
    }
    !/pattern/ {
        # else do other things
    }
'

Tags:

Awk