I could probably do it in php, i dont know if this helps you but isnt it simply step 1 get text inbetween for and *** via regex /for[^C]+(\*\*\*)/ (the capital C is to stop the next Current) step 2 get text between first space and second space /\s[a-z0-9]+\s/ step 3 trim As i said, could give you exact commands for php, but im no linux scripter