AWK - I need to write a one line shell command tha

2019-03-04 17:19发布

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 "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.

Example(s):

This is the format of lines we want to print. Lines that do not match this format should be skipped:

(10) This is a sample line from file.txt that your script should count.

(117) And this is another line your script should count.

Lines like this, as well as other non-matching lines, should be skipped:

15 this line should not be printed

and this line should not be printed


Thanks in advance, I'm not really sure how to tackle this in one line.

标签: bash shell awk
2条回答
我只想做你的唯一
2楼-- · 2019-03-04 17:53

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.

查看更多
冷血范
3楼-- · 2019-03-04 17:58

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...

查看更多
登录 后发表回答