AWK - I need to write a one line shell command that will count all lines that

后端 未结 2 689
天涯浪人
天涯浪人 2021-01-27 10:54

I need to write this solution as an AWK command. I am stuck on the last question:


Write a one line shell command that will count all lines in a file called \"f

相关标签:
2条回答
  • 2021-01-27 11:22

    This is not a homework solution service. But I think I can give a few pointers.

    One idea would be to create a counter, and then print the result at the end:

    awk '<COND> {c++} END {print c}'
    

    I'm getting a bit confused by the terminology. First you claim that the lines should be counted, but in the examples, it says that those lines should be printed.

    Now of course you could do something like this:

    awk '<COND>' file.txt | wc -l
    

    The first part will print out all lines that follow the condition, but the output will be parsed to wc -l which is a separate program that counts the number of lines.

    Now as to what the condition <COND> should be, I leave to you. I strongly suggest that you google regular expressions and awk, it shouldn't be too hard.

    0 讨论(0)
  • 2021-01-27 11:41

    I think the requirement is very clear

    Write a one line shell command that will count all lines in a file called "file.txt" that begin with a decimal number in parenthesis, containing a mix of both upper and lower case letters, and end with a period.

    1. begin with a decimal number in parenthesis
    2. containing a mix of both upper and lower case letters
    3. end with a period
    

    check all three conditions. Note that in 2. it doesn't say "only" so you can have extra class of characters but it should have at least one uppercase and one lowercase character.

    The example mixes concepts printing and counting, if part of the exercise it's very poorly worded or perhaps assumes that the counting will be done by wc by a piped output of a filtering script; regardless more attention should have been paid, especially for a student exercise.

    Please comment if anything not clear and I'll add more details...

    0 讨论(0)
提交回复
热议问题