Script to match a pattern and print only the pattern and after that


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to match a pattern and print only the pattern and after that
# 8  
Old 02-28-2013
Try
Code:
awk     'NR==1  {n = split (items, IT)}                 # put the items to search for into array IT
                {for (i=1; i<NF; i++)                   # check every single field in actual line
                   for (j=1; j<=n; j++)                 # against the items wanted
                     if ($i == IT[j]) VL[j]=$(++i)      # if found, register value (= next field) in array VL, and skip check on value
                }
         VL[1]==VL[2]   {print VL[3]}                   # if values of first two items match, print value of third
        ' items="xyz: abz: qrt:" file
[2312]

# 9  
Old 03-01-2013
Can you provide me with a one liner, for the above solution, i tried to modify it to a oneliner, but wasn't working. Please help

Thanks
# 10  
Old 03-01-2013
Code:
Can you provide me with a one liner, for the above solution,

Is it a home work? If not,why you want it in a one liner?

You are interested in the solution (or)number of lines in the code?

Please justify.
# 11  
Old 03-01-2013
I need a one liner because it is part of a bigger script as I have mentioned. I am not sure what do you mean by homework. I am writing this script to gather a lot of different variables, and this is just one of the variable.

A solution and the number of lines in the code is also equally important to me as I will be filtering a lot of other parameters before i come to this part of comparison, and also do a lot more filtering and calculation.

Earlier when the position of the variables in the log files weren't changing so i used to use a simple awk comparison like below,

Code:
awk '$2!=$5{print $9}'

I am not an expert in awk, so I am asking help on this.

And finally sorry for starting another thread, I wasn't sure if it has to be one question in each thread, so I started a new one.

Most of the time, I search and try for solutions from the existing threads, but i couldn't find anything close to my problem and I really need some help in getting this solution.

Thanks

Last edited by Scrutinizer; 03-01-2013 at 06:52 AM.. Reason: code tags
# 12  
Old 03-01-2013
Can the position of the "qrt" is only after "xyz" and "abz" or it can be before that?

For example:If input is:

Code:
.......xyz: [1234] qrt:  [1234]....... abz: [2312]....
.......xyz: [1231] abz: [2414]......qrt: [1231]...

What is the output expected ?

If input is:

Code:
.......xyz: [1234] abz:  [1234]....... qrt: [2312]......xcd: [2234]

What is the output expected ?
# 13  
Old 03-01-2013
The position of "qrt", "xyz" and "abc" would change. It can be in any order.

if the input is

Code:
.......xyz: [1234] qrt:  [1234]....... abz: [2312]....
.......xyz: [1231] abz: [2414]......qrt: [1231]...

then the output should be null, as I want the comparison of the value in the bracket of only xyz and abz. and print qrt only if it is the same.
In your example the values in the brackets of xyz and abz are not equal so, it should not return anything

if the input is

Code:
.......xyz: [1234] abz:  [1234]....... qrt: [2312]......xcd: [2234]

then the output should be qrt:[2312]. As the values int he brackets of abz is equal to the value in the brackets of xyz.

The strings "xyz:", "abz:" and "qrt:" always remain the same only the values in the bracket keep changing.

I hope this makes it more clear.

Thanks

Last edited by Scrutinizer; 03-01-2013 at 06:53 AM.. Reason: code tags
# 14  
Old 03-01-2013
The solution offered above works perfectly on all examples you provided so far. Put it into a function if you aim for readability of the main script.
BTW, even if sensibly (!) collapsed to a one liner, it will work ...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

2. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

3. Shell Programming and Scripting

Print only match pattern

Hi, I want to print the lines from file1 which has the matching pattern from file2 in linux. file1 data is below ----------------- CACA|1234 CA|2345 file2 data is below ----------------- CA NC TX Now I want to print the lines from file1 for the values present in file2. ... (5 Replies)
Discussion started by: ureddy
5 Replies

4. Shell Programming and Scripting

Print only next pattern in a line after a pattern match

I have 2013-06-11 23:55:14 1Umexd-0004cm-IG <= user@domain.com I need sed/awk operation on this, so that it should print the very next pattern only after the the pattern mach <= ie only print user@domain.com (7 Replies)
Discussion started by: anil510
7 Replies

5. UNIX for Dummies Questions & Answers

Match Pattern after certain pattern and Print words next to Pattern

Hi experts , im new to Unix,AWK ,and im just not able to get this right. I need to match for some patterns if it matches I need to print the next few words to it.. I have only three such conditions to match… But I need to print only those words that comes after satisfying the first condition..... (2 Replies)
Discussion started by: 100bees
2 Replies

6. Shell Programming and Scripting

Script to compare pattern and print a different pattern in each line

Hi, I am writing a shell script to parse some files, and gather data. The data in the files is displayed as below. .......xyz: abz: ......qrt: .... .......xyz: abz: ......qrt: ... I have tried using awk and cut, but the position of these values keep changing, so I wasn't able to get... (2 Replies)
Discussion started by: Serena
2 Replies

7. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

8. UNIX for Dummies Questions & Answers

Match pattern in a field, print pattern only instead of the entire field

Hi ! I have a tab-delimited file, file.tab: Column1 Column2 Column3 aaaaaaaaaa bbtomatoesbbbbbb cccccccccc ddddddddd eeeeappleseeeeeeeee ffffffffffffff ggggggggg hhhhhhtomatoeshhh iiiiiiiiiiiiiiii ... (18 Replies)
Discussion started by: lucasvs
18 Replies

9. Shell Programming and Scripting

Use to awk to match pattern, and print the pattern

Hi, I know how to use awk to search some expressions like five consecutive numbers, , this is easy. However, how do I make awk print the pattern that is been matched? For example: input: usa,canada99292,japan222,france59664,egypt223 output:99292,59664 (6 Replies)
Discussion started by: grossgermany
6 Replies

10. Shell Programming and Scripting

Perl script to match a pattern and print lines

Hi I have a file (say 'file1')and I want to search for a first occurence of pattern (say 'ERROR') and print ten lines in the file below pattern. I have to code it in PERL and I am using Solaris 5.9. I appreciate any help with code Thanks Ammu (6 Replies)
Discussion started by: ammu
6 Replies
Login or Register to Ask a Question