Hal
Hal

Reputation: 372

Matching patterns for awk script

I'm trying to count the numbers of words that starts and ends with "de" but I could only make it count the words that has de. Example

My input file

dende
detail
DEnaDE
de.de
de

My code:

#!/bin/awk -f
/^de/&&/de/

Results:

dende
de.de
detail
de

I am suppose to get dende,de.de,DenaDe,de. I used the operator && in my code so I don't get why it doesn't work. Can anyone correct me on what I am doing wrong?

Upvotes: 1

Views: 84

Answers (3)

Sundeep
Sundeep

Reputation: 23667

you could also use grep

$ grep -ix 'de\|de.*de' ip.txt
dende
DEnaDE
de.de
de

$ grep -cix 'de\|de.*de' ip.txt
4
  • -i to ignore case
  • x to match only whole line
  • -c to print total count instead of matching lines
  • de\|de.*de match de as whole line or match line starting with de and ending with de

If \| is not supported, use grep -ixE 'de|de.*de' or grep -ixE 'de(.*de)?' or grep -ix -e 'de' -e 'de.*de' and so on...

Upvotes: 2

RavinderSingh13
RavinderSingh13

Reputation: 133428

Could you please try following.

awk '/^[Dd][eE]/&&/[Dd][eE]$/' Input_file

OR

awk '/^[Dd][eE]/&&/[Dd][eE]$/{count++}  END{print count}'  Input_file

Upvotes: 1

Ed Morton
Ed Morton

Reputation: 203169

With GNU awk for IGNORECASE:

$ awk -v IGNORECASE=1 '/^de/ && /de$/' file
dende
DEnaDE
de.de
de

With any awk:

$ awk '{lc=tolower($0)} (lc ~ /^de/) && (lc ~ /de$/)' file
dende
DEnaDE
de.de
de

Upvotes: 2

Related Questions