LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-04-2015, 03:29 AM   #1
lokesh.talk
LQ Newbie
 
Registered: May 2015
Posts: 4

Rep: Reputation: Disabled
search 2 strings using awk


I want to search 2 strings A nd B in a file,both present in different rows.If both are found I must get output as both the strings,otherwise blank output.I want to use awk here.

File contains:

A
C
D
B
X
Y

Desired output:

A
B

I am not getting the desired output using,
awk '/"A" && "B"/{print}' file
 
Old 05-04-2015, 03:57 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,959

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
So what have you tried to diagnose why you aren't getting what you expect (hope for) ?.
Reduce the test(s) - print a message when you find each one ...

All sorts of things you can try to learn as you go.
 
Old 05-04-2015, 04:13 AM   #3
lokesh.talk
LQ Newbie
 
Registered: May 2015
Posts: 4

Original Poster
Rep: Reputation: Disabled
I am using awk '$0 ~ /A/ && $0 ~ /B/{print $0}' file

But getting a blank output
 
Old 05-04-2015, 04:18 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,959

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
So what about "awk '$0 ~ /A/ {print $0} ; $0 ~ /B/{print $0}' file ?.

Think about what that really means - and why it may not be what you actually want.
Like I said, lots of potential for learning as you go.
 
Old 05-04-2015, 04:37 AM   #5
lokesh.talk
LQ Newbie
 
Registered: May 2015
Posts: 4

Original Poster
Rep: Reputation: Disabled
Its not working
 
Old 05-04-2015, 09:18 AM   #6
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,664

Rep: Reputation: 1543Reputation: 1543Reputation: 1543Reputation: 1543Reputation: 1543Reputation: 1543Reputation: 1543Reputation: 1543Reputation: 1543Reputation: 1543Reputation: 1543
Code:
awk '/"A" && "B"/{print}' file
As AWK is a line oriented program, that approach will not work, as nothing will match.

Restating your problem:
If a line contains "A", remember the line.
If a line contains "B", remember the line.
When all lines have been read, if "A" and "B" have been found, print "A" then "B".

Putting that logic into AWK syntax is your task.

NB - The logic could be made more efficient, especially if you know that "A" will always be followed by "B".

Last edited by allend; 05-04-2015 at 09:20 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Search for 2 strings between a start and end range in awk dazdaz Programming 2 03-15-2013 09:53 AM
[SOLVED] Awk question - Search, print & replace strings shivaa Linux - Newbie 6 12-06-2012 02:03 PM
[SOLVED] Pass search results to awk, and use awk output to search other files bspears1 Linux - Newbie 8 07-21-2012 10:17 AM
fastest way to search for strings niteshadw Linux - Newbie 4 01-30-2010 07:24 AM
Awk Question to search specific strings grouped by blank lines rk4k Programming 6 07-08-2008 12:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration