bashloopsshellfile

How to extract lines with a specific string and save them to a file and print the match count in bash


I have a large file with data like the example below, and I need to extract lines that match a specific string. These matching lines should then be appended to another file, and I also want to count the total number of matching lines.

I know, I can use a while loop in Bash to read the file line by line and extract the data I needed, the process seems to be slow because of the data size in file.

Is there a more efficient approach to achieve this without using a while loop?

Here is the sample data in file

Time 10:25:21 Hello lavg data
Time 10:25:22 load average is 50%
Time 10:25:22 load average is 51%
Time 10:25:22 load average is 50%
Time 10:25:22 load average is 52%
Time 10:25:22 load average is 51%
Time 10:25:22 data coming from an another source
Time 10:25:23 routed the request
Time 10:25:23 load average is 55%

Solution

  • you can use simple grep command to get your suff done. here is an example

    Suppose, you want to extract the lines that has string load average

    Get total count of lines of matching string : grep -cw "load average" your_input_file.txt

    Write matching string lines to an new file load_average.txt: grep -w "load average" your_input_file.txt > load_average.txt

    Additionally, if you also want to filter matching substring, you can remove flag -w from command