4

The program is supposed to count the number of lines begin with a decimal number in parenthesis, containing a mix of both upper and lower case letters and end with a period.

I have

BEGIN {x=0}
/^\([0-9[0-9]*) [A-Z][A-z]* [a-z][a-z]* \.$/ {x = x+1}
END{print x}

I have them split on multiple different lines because I have been running display(!d) statements for debugging trying to figure it out. To run i use awk -f programName.awk filename.txt Any help is appreciated.

UPDATE

New code reads

BEGIN{x=0}
/^\([0-9]+\)[A-Za-z]+\.$/{x++}
END{print x}

I use vim EC.awk to edit this. awk -f EC.awk EC.txt to run comes back with 1. EC.txt contains 5 out of 12 lines that should be counted.

INPUT FILE vim EC.txt

(1) Line one, this should count.
(2)Line two. Should also count.
3 should not count..
4 not
(5)Yes.
(6). nope
7 OHHH mann
8 This suck
(9)Oh ya? YOU SUCK.
10 Cheaa
(11) BOI.
(12) WoW MoM. Print mofo.

UPDATED CODE

BEGIN{x=0}
/^\([0-9]+\).*?[A-Za-z]+\.$/{x++}
END{print x}

This gives me 6. I believe its counting line 11 (11) BOI. Working on printing out the lines to make sure.

6
  • 1
    Not sure your regex is correct. This ^\([0-9]\)[A-Za-z]*\.$ is a line beginning with a single digit in parenthesis, followed by u/l case letters and end with a period ? Change to ^\([0-9]+\)[A-Za-z]*\.$ for many digits. Commented Mar 3, 2016 at 0:13
  • 1
    Popular question: stackoverflow.com/questions/35715258/… stackoverflow.com/questions/28687756/… Commented Mar 3, 2016 at 0:36
  • 1
    I changed to what you suggested, but still getting the same result I want, if I put {x=x+1} on the next line it prints/counts 12 which is the amount of lines in my test file but only 5 of the lines should be counted. if I have the {x=x+1} at the end of the line it prints/counts 1. Commented Mar 3, 2016 at 0:36
  • can you post your EC.txt file? because then i could change the regex according to the input file Commented Mar 3, 2016 at 1:53
  • What would the expected output be given that input? Which lines do you expect to match? Commented Mar 3, 2016 at 3:14

4 Answers 4

5

For an alternative solution that expresses the intent more simply and clearly and is also locale-aware (doesn't invariably only match ASCII letters), see Ed Morton's helpful answer.

Try the following (POSIX-compliant):

awk '/^\([0-9]+\).*([A-Z].*[a-z]|[a-z].*[A-Z]).*\.$/ { ++x } END { print x+0 }' file
  • ^\([0-9]+\) matches a decimal number in parentheses at the beginning of a line.

  • \.$ matches a literal period at the end of a line.

  • .*([A-Z].*[a-z]|[a-z].*[A-Z]).* matches any string in between that:

    • Either: contains at least 1 uppercase letter followed by at least 1 lowercase one.
    • Or: contains at least 1 lowercase letter followed by at least 1 uppercase one.
    • Thus, this expression should match any string containing any mix of lower- and uppercase [ASCII-only] letters, as long as least 1 uppercase and 1 lowercase letter is present.

As for why your approach didn't work:

  • Your initial solution attempt, [A-Z][A-z] *[a-z][a-z]*, only matches lines whose first [ASCII] letter on the line is uppercase; in other words: lines where the first letter on the line is lowercase aren't matched.
  • Your later solution attempt, [A-Za-z]+, due to using a single character set any of whose characters are matched, also matches lines containing only uppercase or lowercase letters, which is why line (11) BOI. also matches.
Sign up to request clarification or add additional context in comments.

Comments

3

idk if this is the expected output or not since you didn't include that in your question but I just coded what you said in your question count the number of lines begin with a decimal number in parenthesis, containing a mix of both upper and lower case letters and end with a period and added the print so you can see what it matches so take a look and see if it does what you want:

$ cat tst.awk
/^\([0-9]+\)/ && /[[:upper:]]/ && /[[:lower:]]/ && /\.$/ { print; cnt++ }
END { print cnt+0 }

$ awk -f tst.awk file
(1) Line one, this should count.
(2)Line two. Should also count.
(5)Yes.
(9)Oh ya? YOU SUCK.
(12) WoW MoM. Print mofo.
5

Don't get stuck thinking that the condition part of an awk statement has to be a regexp, like if this was sed or grep, as it doesn't - it can be a compound condition of ands/ors of regexp segments if that's what makes your code simpler and clearer as in this case IMHO.

2 Comments

++ for locale-awareness and clarity of intent; I haven't tested, but perhaps you know from experience: in terms of performance, how does this compare to my single-regex solution (assuming both solutions use either [A-Z] / [a-z] or [[:upper:]] / [[:lower:]])?
Sorry, no I don't know. I THINK I remember someone doing a test in a different question that showed character classes were slightly faster than equivalent ranges but once you add in the ands/ors and everything else I couldn't guess which solution would be faster. They'll both run in the blink of an eye even for large files anyway though.
1

It is best to break down the conditions into separate regex's sometimes:

  1. Lines begin with a decimal number in parenthesis: /^\([0-9]+\)/ or /^\([[:digit:]]+\)/
  2. Containing upper case letters: /[A-Z]/ or /[[:upper:]]/
  3. Containing lowercase letters: /[a-z]/ or /[[:lower:]]/
  4. End with a period: /\.[ \t]*$/ (the [ \t]* catches trailing spaces if any...)

Now just combine those conditions:

awk '/^\([[:digit:]]+\)/ && /\.[ \t]*$/ && /[[:lower:]]/ && /[[:upper:]]/ { print }' file
(1) Line one, this should count.
(2)Line two. Should also count.
(5)Yes.
(9)Oh ya? YOU SUCK.
(12) WoW MoM. Print mofo.

Then run through wc -l to get the line count:

awk '//^\([[:digit:]]+\)/ && /\.[ \t]*$/ && /[[:lower:]]/ && /[[:upper:]]/ { print }' file | wc -l
5

Or, maintain your own count:

awk '/^\([[:digit:]]+\)/ && /\.[ \t]*$/ && /[[:lower:]]/ && /[[:upper:]]/ { i++ } END{print i}' file
5

The issue with your regex:

/^\([0-9]+\).*?[A-Za-z]+\.$/
            ^^                       Any string of characters
                 ^ ^                 Could be 'UPPER' or 'lower' 
  1. The .* matches all characters (including spaces) leading up to,
  2. [A-Za-z]+ which matches a run of upper and/or lower case letter but does not tell you if you have both.

Almost, but you are not detecting properly lines that fail to include both upper and lower case letters with that regex.

10 Comments

You should mention that \s is gawk-specific and that in some locales a-z means aAbBcC...z while A-Z means AbBcC...zZ so /[a-z]/ && /[A-Z]/ would be satisfied by just the letter c for example. Also in the END you need to say print i+0 so it'll print zero instead of a blank line if there's no matches.
idk off the top of my head, let me google for one and I'll get back to you.
Found one - according to teaching.idallen.com/cst8177/13w/notes/… en_US.utf8 is one such locale. I've never found anything to suggest that [0-9] includes anything but digits btw so I tend to still use that vs [[:digit:]] for conciseness.
As to [[:digit:]] vs [0-9] consider this list :-0
@EdMorton: Your first link tells me that POSIX in 2008 declared the behavior of ranges in locales other than "C" and "POSIX" as undefined. In practice, it seems that at least the most common utilities exhibit the traditional behavior with ranges such as [a-z] and [A-Z] (ASCII only, distinction between upper- and lowercase, as expected), even in locales such as en_US.UTF-8. The following commands demonstrate that (nothing matches): for char in 'c' 'ü'; do grep '[A-Z]' <<<"$char"; sed -n '/[A-Z]/p' <<<"$char"; awk '/[A-Z]/' <<<"$char"; tr -dC 'A-Z' <<<"$char"; done
|
1

Your regex tries to match the following text (1 or more digits)<space><1 or more Uppercase><space><1 or more lowercase><space><period>

I think while posting the question you have missed out the ] in case of digits, and if you want to have lowercase followed by uppercase then you must use your regex; but since you mentioned in your question it can be a mix of uppercase and lowercase you will have to use [A-Za-z]+. + ensures 1 or more i.e [a-z]+ is equivalent to [a-z][a-z]*

$cat file.txt 
(1) aBCdadg .
(2) dgshdf .
(3) DFHFH .
xyz
abcd
(56) sdflgkfd .
$ cat prgm.awk 
BEGIN {x=0}

/^\([0-9]+\) [A-Za-z]+ \.$/ {x++}

END {print x}
$ awk -f prgm.awk file.txt 
4
$

And if you want to have 1 or more lowercase chars followed by 1 or more uppercase then you will have to use the this regex:

/^\([0-9]+\) [a-z]+ [A-Z]+ \.$/ {x++}

Edit:

$ cat file.txt 
(1) Line one, this should count.
(2) Line two. Should also count.
3 should not count..
4 not
(5)Yes.
(6). nope
7 OHHH mann
8 This suck
(9) Oh ya? YOU SUCK.
10 Cheaa
(11) BOI.
(12) WoW MoM. Print mofo.
$ cat prgm.awk 
BEGIN {x=0}

/^\([0-9]+\)\s*[A-Za-z0-9., ]+\s*\./{x++}

END {print x}
$ awk -f prgm.awk file.txt 
5
$

Edit 2: Sorry i was in a hurry to go somewhere and was off my comp for few hours. Since its more clear what you need, i'll just update the answer for completeness.

$ cat prgm.awk 
BEGIN {x=0}

/^\([0-9]+\).*([A-Z].*[a-z]|[a-z].*[A-Z]).*\.$/{x++;print $0}

END {print x}
$ awk -f prgm.awk input_file.txt 
(1) Line one, this should count.
(2) Line two. Should also count.
(5)Yes.
(9) Oh ya? YOU SUCK.
(12) WoW MoM. Print mofo.
5
$

Do mark the question solved by accepting anyone's answer apart from mine :P :)

Edit 3: give others the credit.

3 Comments

Thanks for the help, I changed my program to use the + based on jas comment with the other stack overflow question. Your answer has asserted that this is the correct way to do it. Thanks for clarifying what the + does. I changed {x=x+1} to {x++} which i realize is the same thing, and now my code matches yours. I run it and now it prints/counts 0. In theory everything seems/looks right. Frustrating.
@ChrisFocker modified the regex
Ok, so that is printing line 11 BOI. which shouldn't be printed. The condition is a parenthesis with a number inside followed by a line with u/l and ends with a period.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.