Lập Trình Linux

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.

Nguồn: https://ftlinuxcourse.com

Xem thêm bài viết khác: https://ftlinuxcourse.com/lap-trinh-linux

Tổng Hợp
LMHT Tiêu Điểm Tướng Yorick – Kẻ Đào Mộ
Phần Mềm
Cách tải phần mềm mario ( gõ phím 10 ngón ) học sinh
Lập Trình Linux
Zlib-1.2.9 error solve in kali with terminal
  • likeeee


  • Thanks a lot !
    I'm looking forword also to see the video about the usage of "awk" . I know it's very powful to deal with the text.
    Thanks again for your video and detailed description which really needs a lot of work.


  • I'll do a short video showing you how to do this


  • Good video.
    In your example you used : grep *.zip < acess.log , but I usually use it without <
    which is : grep *.zip access.log

    I want to know how to display duplicate lines based on column 2 in a file

    k1;v1
    k2;v1
    k3;v2

    expected output is :
    k1;v1
    k2;v1


  • RocoRoco

    Author Reply

    sgrep shirt


  • Della Phillips is easy on the ears…and it is written so even I can understand…LIKE!!!!


  • i like


  • good .. please upload more videos like this.. thanks..


  • The speech isn't done via any software, I wish voice text to voice software was that good. The voice over was done by a professional voice artist @DellaPhillips on Twitter


  • Good video 🙂
    What kind of application are you using for the text voice changer/speech? Thank you 🙂


  • I seam to remember that MS word files are a proprietary binary format, so grep won't be able to search for the plain text within them.

    If you have the AbiWord word processor installed on your machine (or install it via your package manager) then you can use the following command line to convert the contents of the MS Word files to text files.

    find *.doc exec abiword –to=txt -o {}.txt {} ;

    Then you can use grep on the resulting .txt files to find the matches your looking for.


  • grep searches and locates .txt files. Does grep work with MS Word, i.e. .doc files?


  • i liked the british female voice, it was refreshing to hear clear speech with prodessional sound quality–good mixing!!! usually the sound recording is amateur at best, i prefer this over a cheap mic with un-processed sound.

    Paul don't change a thing, you're presentation is very good the way it is…please keep it up and please make more…!


  • If you've watched any of the other tutorials on this topic on the YousTubes, you might understand why I completely appreciate hearing a serious, clear and professional tone of voice for once…this was kind of refreshing to me


  • grep is great


  • I found it not useful, but very useful, thank you very much


  • @daeheadshot

    Helge sorry you found the voice annoying, I certainly wasn't try to annoy anyone — I was trying to make the video "work safe" and sound professional, but I'll take your comments on board, and try to make future videos less serious

    Paul,


  • Hi, thanks ..this is quite a useful lesson. also could you please tell me how to add alias as yo have mentioned in the lesson.


  • @ritika8sharma2

    Thanks, glad you liked the video.

    You can search all files and sub-folders by doing :-
    grep -rl "stringtofind" .

    Which will list the file names the string to find is found in,
    the dot at the end tells grep to use the current folder as the
    starting point.

    To search for multiple strings use the following format :-

    grep -rl 'string1|string2|string3' .

    Cheers
    Paul


  • Hi,
    It's a very interesting upload.
    Could you pls. tell me how to search multiple strings in multiple files in a folder and subfolders?