Linux grep – A quick tutorial showing how to use grep to count strings in a text file
In today’s screen cast, I am going to show you how to use the Linux grep command, to quickly count the number of instances of a text string, found within a given file.
The example will demonstrate how to quickly process a web servers access log file, for certain activity. Last month I released the TIFF splitter utility, which is a free piece of software to split multi page TIFF files into their individual
pages. It’s worth pointing out that this software will run on Linux when used in conjunction with Wine. Anyway, I wanted a quick and easy way to check how many downloads of the software ZIP file there have been.
Here I have a copy of the access log file, for the last weeks activity, and if I use the CAT command, to output the contents of the file to screen, you can see it contains a lot of information. So we need to use the grep command to
filter out the lines we are interested in. In my case, I want to find all the lines which have a dot zip reference in them, as this will give me the actual requests for the software download.
The command we need to type is grep, space, followed by the criteria your searching for, in this case dot zip. We then need to pipe in the file name we are going to process, for example access dot log. The grep output is now restricted, showing just the lines within the log file which have the dot zip reference, highlighting your search term for convenience.
To get the actual count, we just need to add the dash c switch to the grep command, which gives us a count of seventy two. So I now know my software has been downloaded seventy two times in the last week. To make life even easier, I have assigned this command sequence to an alias using the letter k. Now, all I need to type is k and hit return, to get the current download count. I think you’ll agree that’s pretty convenient for a web master who wants to quickly check certain activity on their web site.
Likewise, if you wanted to know how many times Google Bot has visited your web site, then just enter the term Google Bot as the search criteria, in my case, you can see it has visited just over a hundred times.
If you would like more information on the grep command, then I would recommend the grep pocket reference book, published by O’Reilly.
Thank you for watching the video, if you have found it useful then please leave a comment, and don’t forget to subscribe to the Linux by Example channel.
Xem thêm bài viết khác: https://ftlinuxcourse.com/lap-trinh-linux
Xem thêm Bài Viết:
- Tuyệt chiêu tạo usb boot kali linux đơn giản dành cho bạn
- Hướng dẫn chi tiết từ A – Z các bước cài đặt Python trên Windows 10
- Bật mí quy trình cài đặt Kali Linux trên Vmware đúng chuẩn và chi tiết
- Hướng dẫn sử dụng lightworks của bạn nhanh chóng và hiệu quả nhất
- Hướng dẫn cài máy ảo virtualbox nhanh chóng và tiện lợi nhất