Regex question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regex question
# 1  
Old 09-26-2018
Regex question

I want to match all occurrence of 01,03,05,07,10,11 at 9th and 10th position of a string .
I tried the following but its also matching characters like 33 or 11 on 9th and 10th position .


Code:
sed  "/^[0-9]\{8\}00[01,03,05,07,10,11]/d" A.TXT
000000001000
433483433339  <<< wrong
121121211100  <<< wrong 
167710000110
167735250310
167735260510
167735280710
167735301010
167735431010
167735451010
167710101110
167730691110
167730611111


Last edited by Scott; 09-26-2018 at 06:13 PM.. Reason: Code tags
# 2  
Old 09-26-2018
You said you wanted the 11? Why, then, is the second "wrong" wrong?


In your regex, you want 00 in positions 9 and 10. And, you may want to reread the regex documentation, as (man regex):
Quote:
A bracket expression is a list of characters enclosed in "[]". It normally matches any single character from the list
So your bracket expr. seems to have too large a list, including 0 and , . On top, you are deleting the matching lines, so reverse the effect. Try instead:
Code:
sed  '/^[0-9]\{8\}\(0[1357]\|1[01]\)/!d' file

or, for better readability
Code:
sed  -E '/^[0-9]{8}(0[1357]|1[01])/!d' file


Last edited by RudiC; 09-26-2018 at 06:34 PM..
This User Gave Thanks to RudiC For This Post:
# 3  
Old 09-26-2018
hi Rudi .. sorry it was a typo. . 2nd wrong is not wrong. but 1st wrong is wrong.
I ran your command it selects nothing ?

Quote:
$ type a.txt
000000001000
433483433339
121121211100
167710000110
167735250310
167735260510
167735280710
167735301010
167735431010
167735451010
167750000010
167710101110
167730691110
167730611111
$
$ sed "/^[0-9]{8}(0[1357]|1[01])/!d" a.txt
$

------ Post updated at 09:12 PM ------

Quote:
In your regex, you want 00 in positions 9 and 10.
no i dont want to 00 in the 9th and 10th position ..i was not sure what 00 meant there.
what i want is simple .. i want to detect any records that contain 01,03,05,07,10 or 11 are in position 9th and 10th position.

Last edited by Don Cragun; 09-26-2018 at 10:21 PM.. Reason: Fix QUOTE tags.
# 4  
Old 09-26-2018
Your statement of what you're trying to do is ambiguous. Writing a regular expression to match 01, 03, 05, 07, 10, or 11 in character positions 9 and 10 is easy (and RudiC has shown you REs that do that). But what you mean by "detect any records that contain" those strings is not at all clear. If you match one of those strings, what do you want to do?

Do you want to delete all lines that match those strings in that position?

Do you want to delete all lines that DO NOT match those strings in that position?

Do you want to delete those strings from if they occur in that position leaving the rest of the characters on the lines the matched unchanged?

And, this is a prime example where it is crucial that you tell us what operating system (or at least which version of sed) you're using. Some versions of sed accept non-standard RE forms that are used in some of RudiC's suggestions; other versions of sed will interpret those REs in a different way.

Please explain clearly the operating environment you're using and show us the output you're hoping to produce from the sample input you provided.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 09-26-2018
sorry my bad. . this is what i want

ok let me tell it more clearly ..lets say I have a file a.txt
Code:
$ type a.txt
000000001000
433483433339 record 2
121121211100
167710000110
167735250310
167735260510
167735280710
167735431010
167750000010 record 9
167710101110

I am attempting to find if on any line I DO NOT have either 01,03,05,07,10 or 11 in position 9th or 10th.
if you look at record 2 above it as "33" in 9th and 10th position which is not in my list right? similarly if you look at record 9 ,it also
doesnt meet my criteria since it has "00" in 9th and 10th position.
other than these two all records meet my criteria.
so in this case i want the sed to produce an output as follows which are the voilating lines:
Code:
433483433339
167750000010

------------------------------------------------------------------
lets take another example .. lets say my file a.txt contains
Code:
167750000110 
888881881188

in this case all of the records match my criteira so sed should output nothing
cand you modify my sed command to achieve this ?
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, output, and code segments as required by forum rules.

Last edited by boncuk; 09-26-2018 at 11:49 PM..
# 6  
Old 09-26-2018
I repeat: What operating system are you using? What version of sed are you using?
# 7  
Old 09-27-2018
Rudi's sed is not returning me anything


Code:
$ type a.txt
000000001000
433483433339
121121211100
167710000110
167735250310
167735260510
167735280710
167735301010
167735431010
167735451010
167750000010
167710101110
167730691110
167730611111
$ sed  -E "/^[0-9]{8}(0[1357]|1[01])/!d" a.txt
$

------ Post updated at 10:39 PM ------

Quote:
Originally Posted by Don Cragun
I repeat: What operating system are you using? What version of sed are you using?
open VMS .. almost same to other other operating systems.

------ Post updated at 10:41 PM ------

sorry i said it wrong earlier. . i want sed to notify me for any violating record. that's the goal

------ Post updated at 10:43 PM ------

sorry for making a mistake in my original explaination , i corrected it .. i just want sed to output the violating records if any

------ Post updated at 10:55 PM ------

anotehr example to clear things up , from this file i want sed to output
Code:
000000000000
433483433339

because both of them do not have either 01,03,05,07,10,11 at 9th and 10th position
Code:
$ type a.txt
000000001000
000000000000
433483433339
121121211100
167710000110
167735250310
167735260510
167735280710
167735301010
167735431010
167735451010
167750000010
167710101110
167730691110
167730611111

------ Post updated at 11:00 PM ------

i am stuck with production issue need help urgently .
Don you said finding 01,03,05,07,10,11 at 9th an 10th position in a string is easy .. can you give me sed command for it please?

------ Post updated at 11:08 PM ------

I tried Rudi's command on sun solaris, Linux . .also not working .
If you can give me this solution on solaris or Linux its also fine for me


Code:
oracle$ sed  -E '/^[0-9]{8}(0[1357]|1[01])/!d'
sed: illegal option -- E
oracle$ uname -a
SunOS  5.11 11.3 sun4v sparc sun4v
$

Code:
[oracle ~]$ uname -a
Linux 004160PZ000 2.6.18-92.el5 #1 SMP Tue Apr 29 13:16:15 EDT 2008 x86_64 x86_64 x86_64 GNU/Linux
[oracle ~]$ sed  -E '/^[0-9]{8}(0[1357]|1[01])/!d' a.txt
sed: invalid option -- E

------ Post updated at 11:47 PM ------

Quote:
Originally Posted by RudiC

In your regex, you want [ICODE
00[/ICODE] in positions 9 and 10. And, you may want to reread the regex documentation, as (man regex): So your bracket expr. seems to have too large a list, including 0 and , . On top, you are deleting the matching lines, so reverse the effect. Try instead:
Code:
sed  '/^[0-9]\{8\}\(0[1357]\|1[01]\)/!d' file

or, for better readability
Code:
sed  -E '/^[0-9]{8}(0[1357]|1[01])/!d' file



I tried your command on unix and its returning me the strings that have valid numbers at 9th and 10th position e.g "01","03","05","07","10","11" ,
I want only the three records from this file that have "33" ,"17" and "00" at 9th and 10th position.


operating system is sun solaris and Linux .


Code:
oracle:$ sed  '/^[0-9]\{8\}\(0[1357]\|1[01]\)/d' a.txt
000000001000
433483433339  <<< want this to be spitted out by sed  33 at 9th n 10th pos
121121211100 
167710001710  <<< want this to be spitted out by sed 17 at 9th and 10th pos
167735250310
167735260510
167735280710
167735301010
167735431010
167735451010
167710101110
167730691110
167730600000  <<< want this to be spitted out by sed 00 at 9th and 10th pos

Moderator's Comments:
Mod Comment Please use CODE tags for data as well as required by forum rules!

Last edited by RudiC; 09-27-2018 at 04:38 AM.. Reason: Added CODE tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regex Question

Hi I am trying to match lines having following string BIND dn="uid= putting something like this is not working : /\sBIND dn="uid=/ Any suggestion. Thanks. John (9 Replies)
Discussion started by: john_prince
9 Replies

2. Shell Programming and Scripting

regex question

Hi guys, I am trying to "grep" or "egrep" the following entry out of the file using regex: MACCDB1 or MACCDB2 The problem is that the file might contain other entries which start with "MACCDB" string. I was trying to use regex to "grep" the exact pattern but it fails to output the correct... (2 Replies)
Discussion started by: aoussenko
2 Replies

3. Shell Programming and Scripting

regex question

Hi guys, I have a file in the following format: cmpr5551 cmpr6002 cmpr93 anne 5454 bbro 434 cmprsvc cmprsvc7 ffgi55 vefe99 cmprsvc8 cmprsvc9 I need to "grep" only the entries which start with "cmpr" followed by the number. All other entries should be excluded. I was trying to use... (3 Replies)
Discussion started by: aoussenko
3 Replies

4. Shell Programming and Scripting

RegEX question

Hi, I am trying to write a regex for myscript and need some input from experts. here is what I must grep for TICKET{Sapce}{Space}{hyphen} so here is the example data TICKET 34554, CT-12345, TICKET 12345: some text here TICKET 2342, CT-12345, MA-12344: some text here TICKET... (5 Replies)
Discussion started by: rider29
5 Replies

5. UNIX for Dummies Questions & Answers

regex question

I have dates in mm/dd/yy format that I wish to convert to yy-mm-dd format. ()/()/() finds them, but when I try to replace with $3-$1-$2 both kate and kwrite treat it as a text literal. (2 Replies)
Discussion started by: porphyry5
2 Replies

6. Shell Programming and Scripting

Question on regex with * and .

I have a basic question regarding * and . while using regex: # echo 3 | grep ^*$ 3 I think I understood why it outputs "3" here (because '*' matches zero or more of the previous character) but I don't understand the output of the following command: # echo 3 | grep ^.$ # I thought I... (7 Replies)
Discussion started by: mirage
7 Replies

7. Shell Programming and Scripting

regex question

Hi, im sure this is really simple but i cant quite figure it out. how do i test against a word at the beginning of the line but up to the point of a delimiter i.e. ":" for example if i wanted to test against the user in the /etc/passwd file peter:x:101:100:peters account:/var/peter:/bin/sh ... (3 Replies)
Discussion started by: hcclnoodles
3 Replies

8. Shell Programming and Scripting

regex question

Hi I have a question on regex There is a line in a script like my_file="$(echo SunMonTueWed | sed "s//_&g") " My question what does the expression _&g do. Obviously in this example the output is _Sun_Mon_Tue_Wed Another question can i use some trick to get the result like... (3 Replies)
Discussion started by: xiamin
3 Replies

9. Shell Programming and Scripting

regex question

I have a simple file test.out that contains data in the form of key1=A|shift1 key2=B|shift2 key3=C|shift3 and so on. I need to get it to print A B C I can do it using lookbehind assertion such as this ( ?<==)() yet I was wondering if there is another way of mutching single... (8 Replies)
Discussion started by: arushunter
8 Replies

10. UNIX for Dummies Questions & Answers

regex question

hi, i got a problem with understanding regular expressions. what i wanna do is scanning the wtmp logfile for ips and if a specific ip is echoed id like to be a part of a text to be assigned to it. the scanning is done with #! /bin/bash cat wtmp | strings | egrep -o "+\.+\.+\." | sort -u... (6 Replies)
Discussion started by: rocketkids
6 Replies
Login or Register to Ask a Question