Print strings that match pattern with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print strings that match pattern with awk
# 1  
Old 10-09-2012
Print strings that match pattern with awk

I have a file with many lines which contain strings like [46ms] [986976ms] .. etc.
But with no rule regarding field separators or anything else.
I want to print ONLY THE STRING from each line , not the entire line !!!

For example from the lines :
Code:
Flow [login] on service [credentialService] executed with success in [46ms]. Performances are []
[] Flow [login] on service [exposerService] executed with success in [6406ms]. Performances are [] 
Flow [logout] on [service] [credentialService] executed with error in [34512ms]. Performances are [] and []

I want to have the output :
Code:
[46ms]
[6406ms]
[34512ms]

Or even better only the numbers from each of these strings. I want it with awk because I need speed, I have very big files to work with.

thanks!
# 2  
Old 10-09-2012
as per your input..

this work fine. If not please provide some more variations from your input file..Smilie

Code:
awk -F "[][]" '{ for(i=2;i<=NF;i+=2){if($i ~ /^[0-9]/){print "["$i"]"}}}' file

This User Gave Thanks to pamu For This Post:
# 3  
Old 10-09-2012
Quote:
Originally Posted by pamu
as per your input..

this work fine. If not please provide some more variations from your input file..Smilie

Code:
awk -F "[][]" '{ for(i=2;i<=NF;i+=2){if($i ~ /^[0-9]/){print "["$i"]"}}}' file


Hello, yes it is what I wanted. Just one more little thing. Is it possible to print only the digits from each of the matched strings ? I mean without square brackets and the word "ms" ?

I know I can extract them with a sed afterwards or something else, but I think your awk snippet can provide that directly.

thanks again !
# 4  
Old 10-09-2012
Quote:
Originally Posted by black_fender
but I think your awk snippet can provide that directly.
Yes we can do this with awk..Smilie

Code:
awk -F "[][]" '{ for(i=2;i<=NF;i+=2){if($i ~ /^[0-9]/){gsub("ms","",$i); print $i}}}' file

# 5  
Old 10-09-2012
With some sane input:
Code:
awk 'sub(/].*/,"")&&/^[0-9]/{print $0+0}' RS='[' file

This User Gave Thanks to elixir_sinari For This Post:
# 6  
Old 10-09-2012
Quote:
Originally Posted by pamu
Yes we can do this with awk..Smilie

Code:
awk -F "[][]" '{ for(i=2;i<=NF;i+=2){if($i ~ /^[0-9]/){gsub("ms","",$i); print $i}}}' file


Good stuff . I've got what I need.

Cheers !

---------- Post updated at 07:42 AM ---------- Previous update was at 07:36 AM ----------

Quote:
Originally Posted by elixir_sinari
With some sane input:
Code:
awk 'sub(/].*/,"")&&/^[0-9]/{print $0+0}' RS='[' file


Hi, yes, it is even faster ! :

Code:
$: time zcat log.gz | grep "executed with .* in \[.*\]" | awk 'sub(/].*/,"")&&/^[0-9]/{print $0+0}' RS='[' | wc -l
31675

real    0m3.719s
user    0m4.068s
sys     0m0.164s
$:
$: time zcat log.gz | grep "executed with .* in \[.*\]" | awk -F "[][]" '{ for(i=2;i<=NF;i+=2){if($i ~ /^[0-9]/){gsub("ms","",$i); print $i}}}' | wc -l
31675

real    0m5.468s
user    0m5.741s
sys     0m0.163s

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Match Strings between two files, print portions of each file together when matched ([g]awk)

I have two files and desire to use the strings from $1 of file 1 (file1.txt) as search criteria to find matches in $2 of file 2 (file2.txt). If matches are found I want to output the entire line of file 2 (file2.txt) followed by fields $2-$11 of file 1 (file1.txt). I can find the matches, I cannot... (7 Replies)
Discussion started by: jvoot
7 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

Match pattern and print the line number of occurence using awk

Hi, I have a simple problem but i guess stupid enough to figure it out. i have thousands rows of data. and i need to find match patterns of two columns and print the number of rows. for example: inputfile abd abp 123 abc abc 325 ndc ndc 451 mjk lkj... (3 Replies)
Discussion started by: redse171
3 Replies

4. 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

5. Shell Programming and Scripting

awk print pattern match line and following lines

Data: Pattern Data Data Data Data Data Data Data Data Data ... With awk, how do I print the pattern matching line, then the subsequent lines following the pattern matching line. Varying number of lines following the pattern matching line. (9 Replies)
Discussion started by: dmesserly
9 Replies

6. Shell Programming and Scripting

awk Help: Horizontal to Vertical print with pattern match

Hi AWK Experts, Following is the data : BRH113 DD AA HH CA DD DD AA HH BRH091 A4 A6 AH H7 67 HH J8 9J BRH0991 AA D8 C23 V5 H7 BR2 BRH991 AA HH GG5 BT0 JJ0 I want the output to be alligned with the pattern matching "BRH" inthe line. The output should be look like: A]... (4 Replies)
Discussion started by: rveri
4 Replies

7. Shell Programming and Scripting

awk strings search + print next column after match

Hi, I have a file filled with search strings which have a blank in between and look like this: S. g. Ehr. o. Jg. v. d. Chijs g. Ehr. Now i would like to search for the strings and it also shall return the next column after the match. awk -v FILE="search_strings.txt" 'BEGIN {... (10 Replies)
Discussion started by: sdf
10 Replies

8. 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

9. Shell Programming and Scripting

AWK, print no of records after pattern match.

Hi ALL :). i have a file, cat 3 + dog 5 + rat 6 - i want to print no of record having pattern "+". thanks in advance :confused:. (2 Replies)
Discussion started by: admax
2 Replies

10. 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
Login or Register to Ask a Question