شمارش رشته (لینوکس)


یک اسکریپت Bash به نام wordcount.sh بنویسید که آدرس یک فایل را به همراه یک رشته به عنوان آرگومان دریافت کند و تعداد تکرار رشته داده‌شده در آن فایل را بدون توجه به بزرگی و کوچکی حروف، در خروجی استاندارد (stdout) بنویسد.

همچنین در صورتی که فایل داده‌شده وجود ندارد، در stderr بنویسد File not found.

نکته: رشته داده‌شده در هرجای متن که باشد شمرده می‌شود. حتی اگر جزئی از یک کلمه دیگر باشد. مثلاً اگر می‌خواهیم تعداد تکرار رشته and را بشماریم، این رشته را در میان کلمه standard نیز به حساب می‌آوریم.

نمونه🔗

فایل sample.txt:

In computer programming, standard streams are preconnected input and
output communication channels between a computer program and its
environment when it begins execution. The three I/O connections are
called standard input (stdin), standard output (stdout) and standard
error (stderr). Originally I/O happened via a physically connected
system console (input via keyboard, output via monitor), but standard
streams abstract this. When a command is executed via an interactive
shell, the streams are typically connected to the text terminal on
which the shell is running, but can be changed with redirection, e.g.
via a pipeline. More generally, a child process will inherit the
standard streams of its parent process.
Plain text

نتیجه اجرای اسکریپت:

bash wordcount.sh sample.txt the
5

bash wordcount.sh sample.txt and
10

bash wordcount.sh sample.txt "via a"
3

bash wordcount.sh /nowhere/not-existing-file.txt command
File not found
Plain text

فایل wordcount.sh را به صورت یک فایل zip فشرده کنید و به عنوان پاسخ آپلود کنید.

ارسال پاسخ برای این سؤال
در حال حاضر شما دسترسی ندارید.