I have a huge text file with about 70k lines in it. My objective is to read this file, match a pattern ("Count"), and add or replace its value with an iterated number.
What I'm doing is :
- Reading file.
- Grep for the pattern Count.
- If it matches, delete the pattern.
- Append filw the desired pattern (Count = $i) in that line.
- Increment variable i.
Here's the code
line_count=0 i=0 while read line do line_count=$((line_count+1)) if echo "$line" | grep -q "Count" then sed -i "$line_count d" /tmp/$rand_file1 sed -i "$line_count i Count = $i" /tmp/rand_file1 i=$((i+1)) fi done </tmp/rand_file1
The above technique takes about 25min to complete. Is there a way to reduce this time as I will be working with larger data files?
Below is the input pattern and file and expected output :
Count Name = Sarah ID = 113 PhNo = Count Name = John ID = 787 PhNo = Count = 123 Name = Mike ID = 445 PhNo = Count Now Name = Max ID = 673 PhNo =
Expected output file
Count = 1 Name = Sarah ID = 113 PhNo = Count = 2 Name = John ID = 787 PhNo = Count = 3 Name = Mike ID = 445 PhNo = Count = 4 Name = Max ID = 673 PhNo =