extract data that has both patterns

I need some help on one of my assignments , the scenario : we have data files -- eg bus company info : ( location id and company office/depot )

B003^Red Bus Off BSR - GPO
B015^Red Bus Depot BSR
B003^Red Bus Off BSR - Main Stand
B027^MCD Bus Depot LLR
B047^MCD Bus Depot BSR 

.... etc

i need to extract data that has both "Red Bus" (Company Name) and "Depot" in the office name not others ..... hence in the output file only B015^Red Bus Depot BSR

i was currently trying egrep "pat1|pat2" but that works for OR condition , i have also tried if- then grep/sed -- that dosent work well with what is required as i get results that are satisfy either of the patterns and not both !



You can the use logical AND with awk using the && operator like so:

$ awk '/Red Bus/&&/Depot/' file
B015^Red Bus Depot BSR

But you data shows not reason not to do:

$ fgrep 'Red Bus Depot' file
B015^Red Bus Depot BSR

Note: fgrep is just fixed string grep (i.e grep -F) as regexps aren't required.

If you can't guarantee only a space will separate Red Bus and Depot but can guarantee the order then:

$ grep 'Red Bus.*Depot' file
B015^Red Bus Depot BSR

Finally if you can't guarantee the order:

$ egrep 'Red Bus.*Depot|Depot.*Red Bus' file
B015^Red Bus Depot BSR

sed -n '/Red Bus/{/Depot/p}' file

try piping. grep "Red Bus" <File> |grep Depot Basically you grep for all "Red bus", and among those result, search for which have "Depot", this effectively creates an and condition

Need Your Help

Multiple push clear issue in android

android push-notification

I am trying to display push notification using gcm in android.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.