xyz
xyz

Reputation: 8917

Printing lines from a file where a specific field is matched

I have many lines of form: A:B:C

I want to print those lines(complete) where the 3rd field (fields separated by :) contain a certain pattern.

Example:

new/old:california/new york:/ms/dist/fx/PROJ/fx/startScript

new/old:startScript/new york:/ms/dist/fx/PROJ/fx/stopScript

When searching for pattern startScript, the 1st line should be printed and not the 2nd one.

Thanks,

Jagrati

Upvotes: 1

Views: 255

Answers (3)

ghostdog74
ghostdog74

Reputation: 342373

awk -F":" '$3~/startScript$/' file

Upvotes: 0

Mark Byers
Mark Byers

Reputation: 838256

The general solution is to use awk but it is worth noting that in your specific example there is a much simpler solution - you can just use grep:

grep 'startScript$' yourfile

Upvotes: 1

Sjoerd
Sjoerd

Reputation: 75609

Separate on colon, then check third field:

awk -F : '$3 ~ /startScript/ { print }'

Upvotes: 7

Related Questions