Regex for one to four letters (sed) not GNUsed


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Regex for one to four letters (sed) not GNUsed
# 1  
Old 08-11-2014
Regex for one to four letters (sed) not GNUsed

I have regular sed on my computer. I am trying to find out a regex for one-four letters.

I have tried
Code:
([A-Z]\{1,4\}

This will match one or four characters, but what if the expression has two characters?

Like AB1234

I don't have GNUsed and am having trouble with this regex.
# 2  
Old 08-11-2014
Quote:
Originally Posted by newbie2010
I have regular sed on my computer. I am trying to find out a regex for one-four letters.

I have tried
Code:
([A-Z]\{1,4\}

This will match one or four characters, but what if the expression has two characters?

Like AB1234

I don't have GNUsed and am having trouble with this regex.
The opening parenthesis in your RE is not present in the string Like AB1234, so your RE doesn't match this string. If you remove the parenthesis from your RE (as in [A-Z]]{1,4\}), it will match the L or, if used in a global substitute, the L and the AB in Like AB1234.
# 3  
Old 08-11-2014
works fine but not with 2 letters

Sorry, I didn't give you all the data. the line is actually part of a larger command:

Code:
 echo AB1234|sed -n 's/\(.*\)\([A-Z]\{1,4\}[0-9]\{3,4\}\)\(.*\)/\2/p'

This expression works fine with 1 or 4 letters but not 2 letters.

I am not sure that taking out the parenthesis will help.
# 4  
Old 08-11-2014
You got exactly what you asked for. Let's expand the replacement string a little bit so we can see what each of your three parenthesized expressions matched:
Code:
echo AB1234|sed -n 's/\(.*\)\([A-Z]\{1,4\}[0-9]\{3,4\}\)\(.*\)/1=\1,2=\2,3=\3/p'

which produces:
Code:
1=A,2=B1234,3=

Why add .* at the start of your RE if you don't want it to match anything?

We tire quickly of being asked questions where we have to make wild guesses about the context. In the future, give us the context for your question: show us the input, show us the complete command, show us the output you got, and show us the output you wanted.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 08-12-2014
Thanks , I solved it!

There are so many different forms I needed to use this expression on, it took considerable research, but I came up with this, which worked:

Code:
cat list.txt |sed -n 's/.*\([A-Z][^\-][^\-][^\-][0-9]\{2,5\}\)\(.*\)/\1/p'

The variations of the text in these lists is tremendous and I'm sorry for wasting your time as you would have to inspect quite a few lists to know what commands to use, and I didn't post them. It just took lots of research, and I solved it.

I appreciate your nudge in the right direction, though! Thanks!
# 6  
Old 08-12-2014
Note that sed is perfectly capable of reading a file on its own without creating a pipeline and invoking another utility to slow you down and waste system resources:
Code:
sed -n 's/.*\([A-Z][^\-][^\-][^\-][0-9]\{2,5\}\)\(.*\)/\1/p' list.txt

You're absolutely correct. I would never have come up with that RE to match AB in Like AB1234 (which is what you said you wanted in the 1st message in this thread).
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regex with sed

hi i would like to say "DATABASENAME=" to "TABLESNAME=" remove "," and press enter myconfig file thanks (1 Reply)
Discussion started by: mnnn
1 Replies

2. Shell Programming and Scripting

Multiple regex in sed

I am using the following sed script to remove new lines (\r\n and \n), except from lines starting with >: sed -i ':a /^>/!N;s/\r\n\(\)/\1/;s/\n\(\)/\1/;ta' Is there a way to include both \r\n and \n in one regex to avoid the second substitute script (s/\n\(\)/\1/)? (4 Replies)
Discussion started by: Xterra
4 Replies

3. UNIX for Dummies Questions & Answers

sed - extract a group of Letters/numbers

I have a file with hundreds of lines in it. I wanted to extract anything that matches the following: KR followed by 4 digits: example KR1201 cat list | sed "s///g" Is the closest I've come, and obviously it is not what I want. This would remove all of the items that I want and leave me... (2 Replies)
Discussion started by: newbie2010
2 Replies

4. UNIX for Dummies Questions & Answers

Selective Replacements: Using sed or awk to replace letters with numbers in a very specific way

Hello all. I am a beginner UNIX user who is using UNIX to work on a bioinformatics project for my university. I have a bit of a complicated issue in trying to use sed (or awk) to "find and replace" bases (letters) in a genetics data spreadsheet (converted to a text file, can be either... (3 Replies)
Discussion started by: Mince
3 Replies

5. Shell Programming and Scripting

sed command, look for numbers following letters

If I have a set of strings, C21 F231 H42 1C10 1F113 and I want to isolate the ints following the char, what would the sed string be to find numbers after letters? If I do, *, I will get numbers after letters, but I am looking to do something like, sed 's/*/\t*/g' this will give me... (14 Replies)
Discussion started by: LMHmedchem
14 Replies

6. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

7. Shell Programming and Scripting

Put parentheses around all capital letters using SED

Hello everyone I tell you that I'm trying to do a bash program that can put parentheses around each capital letter of each line using SED. I tell you probe with: sed -e '1,$s/A/(A)/g' "$file" but only add parentheses in A. then tested with: sed 'y/AB/(A)(B)/' "$archivo" but it... (3 Replies)
Discussion started by: adiegorpc
3 Replies

8. Shell Programming and Scripting

sed - using regex and | need help

From my understanding when using regex1|regex2 the matching process tries each alternative in turn, from left to right, and the first one that succeeds is used. When im trying to extract the name from those examples: A) name.can.be.different.20.03.2009.boom B)... (2 Replies)
Discussion started by: TehOne
2 Replies

9. Shell Programming and Scripting

sed error - using reserved letters

Hi All, below is my data file file.txt 0 ServerA LAN1 AAA IT01 04/30/2008 09:16:26 (no records) 0 ServerB LAN1 AAA IT02 04/30/2008 09:16:26 0 ServerA LAN1 AAA IT01 04/30/2008 11:11:26 0 ServerB LAN1 AAA IT02 ... (6 Replies)
Discussion started by: karthikn7974
6 Replies

10. UNIX for Dummies Questions & Answers

sed regex

I would like to do this: replace the word "prod" with the word "special" but it may occur through the file naturally without a command, I only want it to happen when it has a specific command in front of it. The command will always look like this <IMG,###,###,##,>prod/directory/IMG/file ... (4 Replies)
Discussion started by: Shakey21
4 Replies
Login or Register to Ask a Question