jcalfee314
jcalfee314

Reputation: 4840

Can awk print lines that do not have a pattern?

Can awk print all lines that did not match one of the patterns?

In other words, I want to transform some lines but leave the rest unchanged. So, if a /pattern/ matched I would provide a custom block to print the line. I just need to provide a default matcher (like an else) to print the other lines.

Upvotes: 14

Views: 18974

Answers (7)

Dirk Hoffmann
Dirk Hoffmann

Reputation: 1563

0 boolean false 1 boolean true

awk -v somevar="replacement" '
    match($0, /^(.*)tag=(.*)$/, m)   { print m[1] "tag=" somevar; matched=1 } 
    match($0, /^(.*)other=(.*)$/, m) { print m[1] "other=" somevar; matched=1 } 
    !matched                         { print }
    matched=0
'

if you just have one "thing" you want to replace in a line you may simplify to: (next statement forces awk to immediately stop processing the current record and go on to the next record)

1 == true ==> print the current record whatever it is
instead of 1 you also can write { print }

awk -v somevar="replacement" '
    match($0, /^(.*)pre(.*)$/, m) { print m[1] "pre:" somevar ; next }
    1
'

Upvotes: 0

Satish
Satish

Reputation: 721

awk '!/pattern/ {print}' input worked for me

Upvotes: 1

Adrian May
Adrian May

Reputation: 2182

echo -n "one\ntwo\nthree\nfour" | 
  awk '{a=1} /one/ {print 1;a=0} /three/ {print 3;a=0} a'

1
two
3
four

Upvotes: 1

Jotne
Jotne

Reputation: 41460

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

Upvotes: 4

a5hk
a5hk

Reputation: 7834

You can use switch if you are using gawk for example

awk '{switch ($0) {
case /pattern/:
    print "Line number:",NR,"pattern matched"
    break

case /Second/:
    print "Line number:",NR,"Second matched"
    break

default:
    print "Line number:",NR,"Another line matched"

}}' input.txt

input.txt

This line matches the pattern
Second line does not match
Hello
This line also matches the pattern
Another line

Output:

Line number: 1 pattern matched
Line number: 2 Second matched
Line number: 3 Another line matched
Line number: 4 pattern matched
Line number: 5 Another line matched

You can also group the cases by removing the break between them. more info

Upvotes: 1

Cole Tierney
Cole Tierney

Reputation: 10324

You can negate the pattern to get else like behavior:

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

Upvotes: 16

Mark Setchell
Mark Setchell

Reputation: 207670

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

Upvotes: 11

Related Questions