awk regex problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk regex problem
# 15  
Old 06-03-2011
Try this.

Code:
cgi@tonga> (/home/cgi) $ gawk    --posix '/[A-Z][A-Z]{3,4}-[0-9]{5}/ {print}' test.txt
ABCDE-12345
abc BCDA-12345
defe erjhj ABCD-12345
dfjfh sdhjh jkfgh dfj aaaAAAA-12345
cgi@tonga> (/home/cgi) $

You have to use --posix to use any posix regex


To extract those fields, you could try:
Code:
cgi@tonga> (/home/cgi) $ awk -F"[a-z ]" '{print $NF}' test.txt
ABCDE-12345
BCDA-12345
ABCD-12345
AAAA-12345

This is the final shot, this should will extract the patterns
Code:
cgi@tonga> (/home/cgi) $ cat test.txt
ABCDE-12345
abc BCDA-12345
defe erjhj ABCD-12345
dfjfh sdhjh jkfgh dfj aaaAAAA-12345
sijefisfej AAAA-12345sefsfsef
sijefisfejAAAA-12345sefsfsef


cgi@tonga> (/home/cgi) $ gawk --posix -F"[a-z ]" '{for(i=1;i<=NF;i++){if($i~/^[A-Z][A-Z]{3,4}-[0-9]{5}/){print $i} }}' test.txt
ABCDE-12345
BCDA-12345
ABCD-12345
AAAA-12345
AAAA-12345
AAAA-12345
cgi@tonga> (/home/cgi) $


Last edited by kumaran_5555; 06-03-2011 at 05:29 AM..
# 16  
Old 06-03-2011
hii
thanks all for ur reply but the problem is that
gawk does not work on my machine...Smilie

i think it is a simple problem because suppose my string is

Code:
 
ABCDE-12345
sdfsdjfhsdABCD-12345
fjdhgfdABC-12345
ABCE-12345

so from this above file it should only return
Code:
 
 
ABCD-12345
ABCE-12345

# 17  
Old 06-03-2011
It also works with awk, same command.

Here you go

Code:
cgi@tonga> (/home/cgi) $ cat test.txt
ABCDE-12345
sdfsdjfhsdABCD-12345
fjdhgfdABC-12345
ABCE-12345
cgi@tonga> (/home/cgi) $ awk --posix -F"[a-z ]" '{for(i=1;i<=NF;i++){if($i~/^[A-Z][A-Z]{3}-[0-9]{5}/){print $i} }}' test.txt
ABCD-12345
ABCE-12345

# 18  
Old 06-03-2011
@above

many many thanks for ur reply
but here comes another problem SmilieSmilie

the problem is u have defined field separator as [a-z] but it could be any like the pattern can be like that also


Code:
${tag}ABCD-12345

so will this code work in that scenario.too

waiting for ur reply..
# 19  
Old 06-03-2011
You need to give us enough information, otherwise we try to exploit what ever we see in your sample. Post your input that reassemble the actual one.
# 20  
Old 06-03-2011
hiii
sry for not providing the actual input

now here it is

my input file contains the data as

Code:
 
CLF-123454564564564.
this bug no is ABC-1234599
this bug no for bug 222 defiend in SVN is  ABC-12345
this bug no is ABCDE-12345
this bug no is ABCD-12345
and SV bug is ${111}ABCD-12345

so i need to find the error code in this file for the pattern below

Code:
 
[A-Z][A-Z][A-Z][A-Z]-[0-9][0-9][0-9][0-9][0-9]

# 21  
Old 06-03-2011
Code:
 awk --posix -F"[^A-Z0-9-]" '{for(i=1;i<=NF;i++){if($i~/^[A-Z][A-Z]{3}-[0-9]{5}/){print $i} }}' test.txt

This should do , what your are looking for. Please test and let me know
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl Regex problem

Script logs into switches on my list but nothing seems to happen. Following error: tr nope, doesn't (yet) match (?-xism:-]+ ?(?:\(config*\))? ? ?$) du SEEN: Here is code in question: @version_info = $session_obj->cmd('term length 0'); $session_obj->cmd('show int | i... (5 Replies)
Discussion started by: mrlayance
5 Replies

2. Programming

Regex problem

Hi, I am looking for regex to extract following words from text: The word which comes after "Replaced" means Replaced disk Replaced floppy Replaced memory Please suggest the regex for it. Thanks! (4 Replies)
Discussion started by: gunjanamit
4 Replies

3. UNIX for Advanced & Expert Users

Sed regex problem

Hi, I tried to extract the time from `date` with sed. (I know it works with `date +%H:%M:%S` as well) I got three solutions of which just one worked. I thought "+" should repeat the previous expression 1 or more times and {n} should repeat the previous expression n times. $ date Thu... (9 Replies)
Discussion started by: thiuda
9 Replies

4. Shell Programming and Scripting

Need some help with a regex if loop problem

Need some help with a regex if loop problem. File1: 2323 3232 4230 3230 4340 4343 233 32320 I want to print "Zero" if the number ends with a zero, but print "number" if it does not! #!/bin/bash /usr/bin/nawk '{ if ($1 ==/+0\b/){ print "Zero"} else{ print "number"} (5 Replies)
Discussion started by: linuxkid
5 Replies

5. Programming

REGEX problem

Hi there, How can we use regex in perl to store the Route Distinguisher (the bold field) and also the underlined and bold lines in the below file? Note: These highlighted pattern is redundant through the whole input file. Basically, we just need to extract these fields at least to store them... (4 Replies)
Discussion started by: omoyne
4 Replies

6. Shell Programming and Scripting

Problem in splitiing file based on regex using awk/nawk

I have a file tmp.txt as shown below: Controller: 0 Disk: 0.0.0 Disk: 0.1.0 Disk: 0.2.0 Controller: 1 Volume:c1t2d0 Disk: 0.0.0 Disk: 0.1.0 Disk: 0.2.0 Controller: 2 Volume:c2t2d0 Disk: 0.2.0 Now I want to split... (4 Replies)
Discussion started by: durbam2002
4 Replies

7. UNIX for Dummies Questions & Answers

regex problem with +

Hi, Can someone tell me why the first regular expression with the + fails to match the input string? SUN /web>echo cat | grep '+' SUN /web>echo cat | grep '' cat I'm running SunOS 5.10 Thanks. Chris (2 Replies)
Discussion started by: che9000
2 Replies

8. Shell Programming and Scripting

regex problem

Hi, #!/usr/bin/perl -w my $timestamp; my $line = "Fri May 29 18:29:57.357 2009 Morocco Standard Time INFO: pid 3216 tid 1724: 170: 132192: apricocot Native Server: recvd AA_BIN_MSG_VER_CHG"; if ($line =~ /^(.*) INFO: .* recvd AA_BIN_MSG_VER_CHG/) { $timestamp = $1; ... (1 Reply)
Discussion started by: namishtiwari
1 Replies

9. Solaris

gcc 3.4 Regex problem

Hello All, I have been using String.h with gcc 2.95. Now I have upgraded to gcc 3.4. The support for String.h has been removed and I believe Regex.h support is also not inbuilt. So I tried to build my own library for String.h. I had to use Regex.h and rx.h for a succesful library compilation.... (0 Replies)
Discussion started by: manishs13
0 Replies
Login or Register to Ask a Question