search and replace

Hi,

I am having trouble with searching for a name and replacing it. The file is tab deliminated.

Basically I want to replace >ref|NC_001147| and >ref|NC_001148| with chr1 and chr2 respectively.

Here is what my file looks like:

>1_1_120_317 S OXTGHFKDK >ref|NC_001147|
>1_1_125_219 S UIDNEIU*(L >ref|NC_001148|

I want the output will look like this:

>1_1_120_317 S OXTGHFKDK chr1
>1_1_125_219 S UIDNEIU*(L chr2

I am using this line... the problem is that it does not work AND i cannot get it to replace multiple thing at once.

awk -v srch=">ref|NC_001147|" -v repl="chr1" '{ sub(srch,repl,$0); print $0 }' file1 > file2

thanks

You can do this simply enough with sed.

$ echo '>ref|NC_001147|' '>ref|NC_001148|' |
        sed 's/>ref|NC_001147|/chr1/g;s/>ref|NC_001148|/chr2/g'
chr1 chr2
$ sed -i 's/>ref|NC_001147|/chr1/g;s/>ref|NC_001148|/chr2/g' filename
$ 
$ cat f1
>1_1_120_317    S    OXTGHFKDK    >ref|NC_001147|
>1_1_125_219    S    UIDNEIU*(L    >ref|NC_001148|
$ 
$ 
$ # using sed
$ sed -e 's/>ref|NC_001147|/chr1/' -e 's/>ref|NC_001148|/chr2/' f1
>1_1_120_317    S    OXTGHFKDK    chr1
>1_1_125_219    S    UIDNEIU*(L    chr2
$ 
$ # using perl
$ perl -ne 's/>ref\|NC_001147\|/chr1/; s/>ref\|NC_001148\|/chr2/; print' f1
>1_1_120_317    S    OXTGHFKDK    chr1
>1_1_125_219    S    UIDNEIU*(L    chr2
$ 
$ 
$ # using awk
$ awk '{sub(/>ref\|NC_001147\|/,"chr1"); sub(/>ref\|NC_001148\|/,"chr2"); print}' f1
>1_1_120_317    S    OXTGHFKDK    chr1
>1_1_125_219    S    UIDNEIU*(L    chr2
$ 
$ 

tyler_durden