Sam Hakim Sam Hakim - 5 months ago 7
Bash Question

Match pattern in line using grep

I need to grep lines from a file with comma separated values, and direct the output to another file. Thus, if the second value starts with 'U' and the last value is 'Success', then this line matches the pattern.

Should match the pattern:

324,U63@DOM1,U63@DOM1,C1755,C1755,Kerberos,Network,LogOn,Success


Should fail to match:

456,C1164$@DOM1,C1164$@DOM1,C625,C625,?,Network,LogOff,Success
123,U63@DOM1,C11847$@?,C2109,C2109,?,?,TGT,Fail


Thank you!

Answer
grep '^[^,]*,U.*,Success$'

Look for the first comma, a U, the last comma, Success and end of line.

Comments