Teja
Teja

Reputation: 13534

How to get the search count for a particular string from each and every line in a file using Unix?

I am trying to search for a particular string in a Unix file from each and every line and error out those records. Can someone let me how can I improve my code which is as below. Also please share your thoughts if you have a better solution.

v_filename=$1;
v_new_file="new_file";
v_error_file="error_file";
echo "The input file name is $var1"

while read line
do
        echo "Testing $line"
        v_cnt_check=`grep ',' $line | wc -l`
        echo "Testing $v_cnt_check"
#       if [  $v_cnt_check > 2 ]; then
#       echo $line >> $v_error_file
#       else
#       echo $line >> $v_new_file
#       fi
done < $v_filename

Input:

1,2,3
1,2,3,4
1,2,3

Output:

(New file)
1,2,3
1,2,3 

(Error file)
1,2,3,4 

Upvotes: 0

Views: 123

Answers (2)

Jonathan Leffler
Jonathan Leffler

Reputation: 753695

awk -F ',' -v new_file="$v_new_file" -v err_file="$v_error_file" \
    'BEGIN { OFS="," }
     NF == 3 { print >new_file }
     NF != 3 { print >err_file }' $v_filename

The first line sets the file name variables and sets the field separator to comma. The second line sets the output field separator to comma too. The third line prints lines with 3 fields to the new file; the fourth line prints lines with other than 3 fields to the error file.

Note that your code would be excruciatingly slow on big files because it executes two processes per line. This code has only one process operating on the whole file — which will be really important if the input grow to thousand or millions or more lines.

Upvotes: 2

jkt123
jkt123

Reputation: 189

From the grep manpage:

   General Output Control
   -c, --count
          Suppress normal output; instead print a count of matching lines for each input file.  With the -v, --invert-match  option  (see  below),  count  non-
          matching lines.  (-c is specified by POSIX.)

You could do something like:

grep --count "your pattern" v_filename

to get the number of occurrences. If you just want the number of lines with your pattern, replace the grep shown above with:

grep "your pattern" v_filename | wc -l

Upvotes: 0

Related Questions