search and replace dynamic data in a shell script
Hi,
I have a file that looks something like this:
...
0,6,256,87,0,0,0,1187443420
0,6,438,37,0,0,0,1187443380
0,2,0,0,0,10,0,1197140320
0,3,0,0,0,10,0,1197140875
0,2,0,0,0,23,0,1197140332
0,3,0,0,0,23,0,1197140437
0,2,0,0,0,17,0,1197140447
0,3,0,0,0,17,0,1197140543
0,2,0,0,0,30,0,1197140237
0,3,0,0,0,30,0,1197140231
0,6,375,98,0,0,0,1187443420
0,6,222,11,0,0,0,1187443380
...
The number in the last column is dynamic. "0,2,0,0,0" is always followed by "0,3,0,0,0" What I need to do is as follows:
- I need to find the first line that starts out with 0,2,0,0,0
- Compare the column 6 to mynumber_1 (10) and if they match I move on to the next line
- Make sure in the next line the column 6 is also 10
- This time, I do the same thing with mynumber_2
- I repeat until mynumber_4 (30 in this case) is found
- Add two lines of my own data in between:
0,3,0,0,0,30,0,1197140231 and 0,6,375,98,0,0,0,1187443420
- Basically, the end result should be new file with two more lines of data in the right place
- In next iteration, I just have to get to the two new lines that I added and update the column6
I am thinking of doing the following:
- cat each line and see if it starts with 0,2,0,0,0. Is there a easy way to do this? In Perl, you can check to see if your variable CONTAINS certain data. Can you do this in shell?
- If it doesn't start out with 0,2,0,0,0 then cat $line >> new_file
- If it does, check the subsequent lines until my last number is matched up in column 6.
- If it fails during these subsequent checks, move on to the next line and start over with my_number1
Does this sound like a good algorithm? Any suggestions? I am currently stuck at checking each line to see if it starts out with 0,2,0,0,0. Thanks.