sed - extract a group of Letters/numbers


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers sed - extract a group of Letters/numbers
# 1  
Old 10-01-2013
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

Code:
cat list | sed "s/[KR[0-9$]//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 with the rest of the text. I've tried sed -n and /p' but this doesn't work and I'm confused about the syntax. I know other ways to do this with egrep and grep -w but I want to learn to do it in sed.

The one thing I don't want to do is print the rest of the line; I want only to print the KR1201 or KR followed by 4 digits. There is something missing in my syntax; not sure how to encapsulate the pattern.
# 2  
Old 10-01-2013
As with sort, sed takes pathname operands. Using:
Code:
cat file | sed [options] [OtherOperands]

instead of:
Code:
sed [options] [OtherOperands] file

wastes system resources, slows down your system for other users who may be sharing your system, and makes your command line run slower.

If you have an input file named list containing:
Code:
nothing from this line
KR1201
abcKR3012
KR1111def
defKR3333extra
12345KR678910

Previous line was empty; neither of these should appear in output

and you run the command:
Code:
sed -n 's/\(.*\)\(KR[0-9][0-9][0-9][0-9]\).*/\2/p' list

using a standards conforming version of sed, you get the output:
Code:
KR1201
KR3012
KR1111
KR3333
KR6789

The Linux sed man page says:
Quote:
POSIX.2 BREs should be supported, but they aren't completely because of performance problems.
and in the description of the sed s(ubstitute) command says:
Quote:
The replacement may contain the special character & to refer to that portion of the pattern space which matched, and the special escapes \1 through \9 to refer to the corresponding matching sub-expressions in the regexp.
But, there is no other mention of sub-expressions on that man page.

Note that this sed command makes no effort to find more than one occurrence of your pattern if it appears more than once on a single line
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 10-02-2013
Really appreciate it!

I've learned a lot from your sed/sort tutorials and today was able to solve a sort problem on my own for once after reading all your posts. I thank you for taking the time out of your day to really create detailed posts with examples! This is much more helpful than any textbook!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Decimal numbers and letters in the same collums: round numbers

Hi! I found and then adapt the code for my pipeline... awk -F"," -vOFS="," '{printf "%0.2f %0.f\n",$2,$4}' xxx > yyy I add -F"," -vOFS="," (for input and output as csv file) and I change the columns and the number of decimal... It works but I have also some problems... here my columns ... (7 Replies)
Discussion started by: echo manolis
7 Replies

2. Shell Programming and Scripting

Sorting mixed numbers and letters

Hello, I have a file such as this: chr1 chr2 chr1 chr2 chr3 chr10 chr4 chr5 chrz chr1AI want to sort it, I use this command: sort -k1 -th -n testfilebut I get this output, how can I fix this? chr1 chr1 chr10 chr1A chr2 chr2 (3 Replies)
Discussion started by: Homa
3 Replies

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

4. Shell Programming and Scripting

Need help please with Grep/Sed command to extract text and numbers from a file

Hello All, I need to extract lines from a file that contains ALPHANUMERIC and the length of Alphanumeric is set to 16. I have pasted the sample of the lines from the text file that I have created. My problem is that sometimes 16 appears in other part of the line. I'm only interested to... (14 Replies)
Discussion started by: mnassiri
14 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

Merge group numbers and add a column containing group names

Hi All I do have a file like this with 6 columns. Groups of data merge together and the group number is indicated above each group. 1 1 12 26 289 3.2e-027 GCGTATGGCGGC 2 12 26 215 6.7e+006 TTCCACCTTTTG 3 9 26 175 ... (1 Reply)
Discussion started by: Lucky Ali
1 Replies

7. Shell Programming and Scripting

Merge group numbers and add a column containing group names

I have a file in the following format. Groups of data merge together and the group number is indicated above each group. 1 adrf dfgr dfg 2 dfgr dfgr 3 dfef dfr fd 4 fgrt fgr fgg 5 fgrt fgr (3 Replies)
Discussion started by: Lucky Ali
3 Replies

8. Shell Programming and Scripting

sed to extract only floating point numbers from HTML

Hi All, I'm trying to extract some floating point numbers from within some HTML code like this: <TR><TD class='awrc'>Parse CPU to Parse Elapsd %:</TD><TD ALIGN='right' class='awrc'> 64.50</TD><TD class='awrc'>% Non-Parse CPU:</TD><TD ALIGN='right' class='awrc'> ... (2 Replies)
Discussion started by: pondlife
2 Replies

9. UNIX for Dummies Questions & Answers

Help! scrolling numbers and letters

Hello all I am a unix newbie.... I have a sun netra t1 and it is freaking out I am connected to it through a console port, and it is just spitting out a ton on numbers and letters like below its just keeps going and going. I have tried rebooting it and I cannot get it back to any kind of a... (1 Reply)
Discussion started by: intraining11
1 Replies

10. UNIX for Dummies Questions & Answers

Letters, Numbers or Alphanumerical

How do I check if a variable consisted of letters, numbers or both letters and numbers? For example, I have a variable $X and I want to print "1" if it contains only letters, "2" if it contains only numbers and "3" if it contains both (2 Replies)
Discussion started by: sleepster
2 Replies
Login or Register to Ask a Question