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
# 1  
Old 02-27-2013
Script to match a pattern and print only the pattern and after that

Hi,

I am writing a shell script to parse some files, and gather data.
The data in the files is displayed as below.

Code:
.......xyz: [1234] abz: [8987]......
.......xyz: [12312] abz: [adfa].....

I have tried using awk and cut, bu the position of these values keep changing, so I can use awk and split it into columns.
The value before the ":' is always constant, the value in the brackets only keep changing.
I need an option which prints in the following manner based on what is being grepped.

if xyz: is grepped it should print
Code:
xyz: [1234]
xyz: [12312]

(OR)

if abz: is grepped it should print
Code:
abz: [8987]
abz: [adfa]

Please help

Thanks

Last edited by Scrutinizer; 03-01-2013 at 06:51 AM.. Reason: code tags
# 2  
Old 02-27-2013
Something like this:

Code:
$ cat file
abc :[2345] xyz: [1234] abz: [8987]
cde :[256] xyz: [12312] abz: [adfa]

$ awk -F"[: ]" -v va="abz" '{for(i=1;i<=NF;i++) { if(c==1&&length($i)>1) {c=0;print $i} ; if($i==va) {c=1} }}' file
[8987]
[adfa]

# 3  
Old 02-27-2013
You could also use grep:

Code:
$ grep -oE "xyz: \[[^]]+\]" file
xyz: [1234]
xyz: [12312]

This User Gave Thanks to Chubler_XL For This Post:
# 4  
Old 02-27-2013
Thank you both the solutions work,

I have one more question. I also need to be able to compare the values in the same line for example

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

it should be a one liner and it should be able to compare the value in the brackets for "xyz:" and "abz:" and if it is the samee then it should print "qrt: [2312]"

In the above lines the output should be only "qrt: [2312]" as that is the only line with the matching value in the bracket of "xyz:" and "abz:".

The positions of this variable keep changing in the same line, so I cannot use the divide by columns.

Last edited by Scrutinizer; 03-01-2013 at 06:52 AM.. Reason: code tags
# 5  
Old 02-27-2013
I'm curious: why should it be a one-liner? Is there some length limit? Because, technically, you can convert any large, multi-line awk script into a one liner by simply deleting the newline bytes.

Regards,
Alister
# 6  
Old 02-27-2013
What should the script output if qrt: does not match the other two? Are these abc: xyz: qrt: values input parameters or hard-coded into script? Can you should some example input, script parameters and required output?
# 7  
Old 02-28-2013
this condition is part of a much larger script, Initially I have used the following condition, bu there have been changes int he file format where the values are not specific to a column, so this is not working.
Code:
awk '$2!=$5{print $9}'

I hope this info helps.

Thanks

---------- Post updated 02-28-13 at 12:51 PM ---------- Previous update was 02-27-13 at 01:40 PM ----------

can some one please help me with the second question?

Thanks

Last edited by Franklin52; 02-28-2013 at 03:13 AM.. Reason: Please use code tags for data and code samples
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