I have scenario like below and need to search for multiple patterns
Eg:
Test [Red]
Time Started= [2.3] secs
Time Ended = [4.5] secc
Green test
Test[Yellow]
Time Started= [2.3] secs
Time Ended = [4.5] secc
Green test
Output:
I need to display the text starting with Test and starting with Time in the sequence
Test [Red]
Time Started= [2.3] secs
Time Ended = [4.5] secc
Test[Yellow]
Time Started= [2.3] secs
Time Ended = [4.5] secc
Am trying with awk. But am able to search with only one pattern.
Thanks.
Three examples:
Code:
perl -ne '/^Test|^Time/ and print' weknowd.file
Test [Red]
Time Started= [2.3] secs
Time Ended = [4.5] secc
Test[Yellow]
Time Started= [2.3] secs
Time Ended = [4.5] secc
Code:
awk '$1 ~ /Test|Time/' weknowd.file
Test [Red]
Time Started= [2.3] secs
Time Ended = [4.5] secc
Test[Yellow]
Time Started= [2.3] secs
Time Ended = [4.5] secc
Code:
grep -E '^T(est|ime)' weknowd.file
Test [Red]
Time Started= [2.3] secs
Time Ended = [4.5] secc
Test[Yellow]
Time Started= [2.3] secs
Time Ended = [4.5] secc
Last edited by Aia; 05-20-2016 at 09:50 PM..
Reason: Adding original post
As long as empty lines between sets of data aren't important, you can also use:
Code:
sed -n '/^Test/,/Ended/p' file
and:
Code:
awk '/^Test/,/Ended/' file
both of which produce the output:
Code:
Test [Red]
Time Started= [2.3] secs
Time Ended = [4.5] secc
Test[Yellow]
Time Started= [2.3] secs
Time Ended = [4.5] secc
if file contains the data you showed us in post #1 in this thread.
If empty lines are critical between sets, you can try something a little more complex like:
Code:
awk '
/^Test/ {
if(c++) print ""
p=1
}
p
/Ended/ {
p = 0
}' file
which produces the output:
Code:
Test [Red]
Time Started= [2.3] secs
Time Ended = [4.5] secc
Test[Yellow]
Time Started= [2.3] secs
Time Ended = [4.5] secc
The above script could be made slightly simpler if you want an empty line to be output after every set instead of just between sets in the output (i.e., add a trailing empty line to the output).
As always, if you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
This User Gave Thanks to Don Cragun For This Post:
Hi Bigshots,
I have a pattern file with two columns. I have another data file. If column 1 in the pattern file appears as the 4th column in the data file, I need to replace it (4th column of data file) with column 2 of the pattern file. If the pattern is found in any other column, it should not... (6 Replies)
Hi All,
I am starting a service which will redirect its out put into 2 logs say A and B.
Now for succesful startup of the service i need to search pattern1 in log A and pattern2 in log B which are writen continuosly.
Now my requirement is to find the patterns in the increasing logs A and B... (19 Replies)
Hi,
I tried to search multiple pattern using awk
trans=1234
reason=LN MISMATCH
rec=`awk '/$trans/ && /'"$reason"'/' file`
whenevr i tried to run on command promt it is executing but when i tried to implment same logic in shell script,it is failing i.e $rec is empty
... (6 Replies)
I would like to print result of multiple search pattern invoked from an one liner. The code looks like this but won't work
gawk -F '{{if ($0 ~ /pattern1/) pat1=$1 && if ($0 ~ /pattern2/) pat2=$2} ; print pat1, pat2}'
Can anybody help getting the right code? (10 Replies)
I have two lists in a file that look like
a b
b a
e f
c d
f e
d c
I would like a final list
a b
c d
e f
I've tried multiple grep and awk but can't get it to work (8 Replies)
Hi,
I have to write one script that has to search a list of numbers in certain zipped files.
For eg. one file file1.txt contains the numbers. File1.txt contains 5,00,000 numbers and I have to search each number in zipped files(The number of zipped files are around 1000 each file is 5 MB)
I have... (10 Replies)
Good day, great gurus,
I'm new to Perl, and programming in general. I'm trying to retrieve a column of data from my text file which spans a non-specific number of lines. So I did a regexp that will pick out the columns. However,my pattern would vary. I tried using a foreach loop unsuccessfully.... (2 Replies)
Hi
I'm not very good with the serach patterns and I'd need a sample how to find a line that has multiple patterns.
Say I want to find a line that has "abd", "123" and "QWERTY" and there can be any characters or numbers between the serach patterns, I have a file that has thousands of lines and... (10 Replies)
I have one file:
123*100*abcd*10
123*101*abcd*-29*def
123*100*abcd*-10
123*102*abcd*-105*asd
I would like to parameterize the search patterns in the following way so that the user could dynamically change the search pattern.
*100* and *- (ie *minus)
*102* and *-
The output that is... (6 Replies)