sed regex backreference replacement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed regex backreference replacement
# 1  
Old 05-15-2012
sed regex backreference replacement

Hello, I want to rename multiple files and catch some points about backreference within sed and regex.
Here is a part of my file list. Input:
Code:
S92A.fa
S92B.fa
... 
S96Z.fa
S921.fa
S922.fa
... 
S997.fa

Note: The file names are not necessarily continuous from A~Z or 921 ~ 997, as some of the files may be missing and there are other types of the file with different pattern in the same folder.
I want to put the prefix "CGW-" to each file name and change the extension to fasta. Output:
Code:
CGW-S92A.fa
CGW-S92B.fasta
... 
CGW-S96Z.fasta
CGW-S921.fasta
CGW-S922.fasta
...
CGW-S997.fasta

I have used two steps do the job, one at a time:
Code:
for i in S9*.fa; do mv $i $(echo $i | sed 's/S9/CGW-S9/g'); done

then
Code:
for i in CGW-S9*.fa; do mv $i $(echo $i | sed 's/fa/fasta/g'); done

It seems using the backreference will do the same job better and easier. But I could not find clues in this forum or Google. Any expertise is greatly appreciated. Thanks a lot!

Last edited by yifangt; 05-15-2012 at 02:22 PM..
# 2  
Old 05-15-2012
You don't need sed for this:
Code:
for i in S9*.fa; do
    mv "$i" CGW-"$i"sta
done

When using sed, you should not use the substitution command's global flag, g, when your intent is to modify only a single occurrence of the pattern.

If it were at all possible that the portion of the filename matched by the wildcard, *, could contain "S9" or "fa", you'd need to be much more careful. Beyond dropping the global flag, to ensure that only the file extension suffix matches, you would need to anchor that pattern to the end of the string.

Since the file glob in the for-loop list has already constrained the matching filename to begin with S9 and end with .fa, the correct sed command doesn't have to bother being specific: sed 's/^/CGW-/; s/$/sta/'. If that file glob were not present, then the following could be used: sed '/^S9.*\.fa$/{s/^/CGW-/; s/$/sta/;}'.

Regards,
Alister

Last edited by alister; 05-15-2012 at 03:10 PM..
This User Gave Thanks to alister For This Post:
# 3  
Old 05-15-2012
Thanks Alister!
Another thing in my mind is the back reference. How to do the job with back reference? Say, I want to change extensions ".fa", ".fx" or ".fq" to ".fasta"? Thanks again! Yifang
# 4  
Old 05-15-2012
sed 's/\(.*\)\.f[axq]$/CGW-\1.fasta/'
This User Gave Thanks to neutronscott For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Segregate file content using sed backreference

I have some text like EU1BTDAT:ASSGNDD filename='$SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD' EU1BTDATEST:ASSGNDD filename='$SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD' EU1CLOSEDATES:ASSGNDD filename='$SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD' EU1DATED:ASSGNDD... (8 Replies)
Discussion started by: gotamp
8 Replies

2. Shell Programming and Scripting

Help with sed backreference please

Hi, I'm using /bin/sh I would appreciate if someone could help me with SED syntax for a "simple" line. Here is where I Got to: I have these strings that are returned by my(Examples) (naturally "FullPath" is always changing don't hardcode this lol) FullPath/AAA.framework... (3 Replies)
Discussion started by: Herrick
3 Replies

3. Shell Programming and Scripting

Regex and backreference to replace in binary file

Hello to all, I have this sed script that replaces hex strins within a binary file. As you can see, I want to replace all bytes 4X with 2X (where X could take values 0 to F). sed -e 's/\x40/\x20/g' -e 's/\x41/\x21/g' -e 's/\x42/\x22/g' -e 's/\x43/\x23/g' -e 's/\x44/\x24/g' -e... (7 Replies)
Discussion started by: Ophiuchus
7 Replies

4. Shell Programming and Scripting

Use regex in replacement string in SED

Hi, I need to use the regex in the replacement string in SED command. something like sed -e ' s/\(^\{5\}\).\{150\}\(.*\)$/\10\{30\}1\{30\}A\{60\}B\{30\}\2/' abc which means for all the lines in file abc that starts with 5 characters, I need to replace character 6-151... (6 Replies)
Discussion started by: snowline84
6 Replies

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

6. Shell Programming and Scripting

Replacement with sed

I am trying to replace the line which has string "tablespace" not case senstive.... with below simple script: mysrcipt.sh sed "s/.*/TABLESPACE USERS/g" create_table > tmp mv tmp create_table Is there any better way to do it? If Search string tooooooo long it will be tough to code in... (4 Replies)
Discussion started by: ganeshd
4 Replies

7. Shell Programming and Scripting

Help with sed replacement

This seems like it should be an easy problem, but I'm a noob and I can't figure it out. I'm trying to use sed, but would be happy to use anything that does the job. I am trying to trim off a fixed number of unknown characters from 2 different : delimited fields while keeping the intervening... (4 Replies)
Discussion started by: helix_w
4 Replies

8. Shell Programming and Scripting

Bash string replacement - how to use regex?

Hello I have a bash script where I need to do a substring replacement like this: variable2=${variable1/foo/bar} However, I only want "foo" replaced if it is at the end of the line. However, this does not work: variable2=${variable1/foo$/bar} as you can see I'm using the $ regex for... (2 Replies)
Discussion started by: Ubuntu-UK
2 Replies

9. Shell Programming and Scripting

egrep vs. sed backreference

My egrep outputs this: $ cat html.out|sed -n '/bluetext/s/&nbsp;/ /gp'|egrep '{5}' <span class="bluetext"><b> Lexington Park, MD 20653</b></span> But my backreference \1 is empty. I dont understand why. Can someone clarify? $ cat html.out|sed -n '/bluetext/s/&nbsp;/ /gp'|sed -n... (1 Reply)
Discussion started by: r0sc0
1 Replies

10. UNIX for Dummies Questions & Answers

Replacement using sed

Hi I have the following file that i need to run a sed command on 1<tab>running 2<tab>running 3<tab>running 4<tab>running I want to be able to replace a line i.e the second one with '2<tab>failed'. As the first number is unique that can be used to search for the relevant line (using ^2 i... (5 Replies)
Discussion started by: handak9
5 Replies
Login or Register to Ask a Question