2015-01-04 68 views
2

我手邊有一個文本文件,其中包含690個與P.S.中顯示內容類似的條目。 (在P.S.中顯示的是一個例子,從這裏http://www.ncbi.nlm.nih.gov/nuccore/AB753792.1)。在我的文本文件中,條目由「//」分隔。在某個單詞(密碼/ sed/awk)之後在文本文件中插入字母數字字符串

在我的情況下,在「ACCESSION」(字符串和3個空格)之後,沒有大寫字母數字字符串 (例如P.S.中的「AB753792」)。我運行MacOSX的優勝美地使用默認的Bash和希望,以填補所產生的獨特的大寫字母數字字符串,如690個空空間:

openssl rand -hex 4 | tr '[:lower:]' '[:upper:]'  

(5.1.15:我已經改變了上面的命令,它在這篇文章的第一個版本)

我可以看到SED/AWK可能是這個問題的解決方案不同,但我想不出SED如何能夠插入唯一的8位大寫字母每個「ACCESSION」後的字符串。

我很樂意接受幫助。

親切的問候,

保羅

附:

LOCUS  AB753792     712 bp DNA  linear INV 26-JUN-2013 
DEFINITION Acutuncus antarcticus mitochondrial gene for cytochrome c oxidase 
      subunit 1, partial cds. 
ACCESSION AB753792 
VERSION  AB753792.1 GI:478246768 
KEYWORDS . 
SOURCE  mitochondrion Acutuncus antarcticus 
ORGANISM Acutuncus antarcticus 
     Eukaryota; Metazoa; Ecdysozoa; Tardigrada; Eutardigrada; Parachela; 
     Hypsibiidae; Acutuncus. 
REFERENCE 1 
AUTHORS Kagoshima,H., Imura,S. and Suzuki,A.C. 
TITLE  Molecular and morphological analysis of an Antarctic tardigrade, 
      Acutuncus antarcticus 
JOURNAL J. Limnol. 72 (s1), 15-23 (2013) 
REFERENCE 2 (bases 1 to 712) 
AUTHORS Kagoshima,H. and Suzuki,A.C. 
TITLE  Direct Submission 
JOURNAL Submitted (07-OCT-2012) Contact:Hiroshi Kagoshima Transdisciplinary 
     Research Integration Center/Nationlal Institute of Genetics; 1111 
     Yata, Mishima, Shizuoka 411-8540, Japan 
FEATURES    Location/Qualifiers 
    source   1..712 
        /organism="Acutuncus antarcticus" 
        /organelle="mitochondrion" 
       /mol_type="genomic DNA" 
       /isolation_source="moss sample (Bryum pseudotriquetrum, 
       Bryum argenteum, and Ceratodon purpureus)" 
       /db_xref="taxon:467037" 
       /country="Antarctica: East antarctica, soya coast, 
       Skarvsnes and Langhovde" 
CDS    <1..712 
       /codon_start=2 
       /transl_table=5 
       /product="cytochrome c oxidase subunit 1" 
       /protein_id="BAN14781.1" 
       /db_xref="GI:478246769" 
       /translation="GQQNHKDIGTLYFIFGVWAATVGTSLSMIIRSELSQPGSLFSDE 
       QLYNVTVTSHAFVMIFFFVMPILIGGFGNWLVPLMISAPDMAFPRMNNLSFWLLPPSF 
       MLITMSSMAEQGAGTGWTVYPPLAHYFAHSGPAVDLTIFSLHVAGASSILGAVNFIST 
       IMNMRAPSISLEQMPLFVWSVLLTAILLLLALPVLAGAITMLLLDRNFNTSFFDPAGG 
       GDPILYQHLFWFFGHPEV" 
ORIGIN  
     1 tggtcaacaa aatcataaag atattggtac actttatttt atttttggag tatgagctgc 
     61 tacagtagga acatctctta gtatgattat ccggtcagaa cttagacaac caggatcact 
     121 cttctcagat gaacaacttt acaacgttac agtaacaaga catgcatttg tcataatttt 
     181 cttttttgta atacccatcc ttattggagg atttggaaat tgactagtac ctttaatgat 
     241 ttcagcacca gatatagctt tcccccgaat aaataacctg agattctgac tactaccccc 
     301 atcttttata ttaattacta taagaagtat agcagaacaa ggagccggga cagggtgaac 
     361 agtttacccc cctttagctc actattttgc acactcagga ccagctgtcg atttaactat 
     421 tttttctctg catgtagcag gagcatcgtc gattttagga gccgtaaact tcatttctac 
     481 aattatgaat atgcgagctc catcaattag tttagaacaa atgccactat ttgtatgatc 
     541 agtactactt acagccattt tacttctact agctctgcca gtattagcag gagccatcac 
     601 aatgctttta ttagaccgaa attttaacac atcgtttttt gatcctgctg gtgggggaga 
     661 tccaattctc tatcaacatt tattttgatt ttttggtcac cctgaagttt aa 
//  
+0

的OpenSSL蘭特-base64 32 | | tr'[a-z]'[A-Z]'給出語法錯誤...應該是「openssl rand -base64 32 || tr'[az]''[AZ]'」?....如果是這樣,這不會生成一個8位數字的字母數字group.please建議 – repzero 2015-01-04 12:27:51

+0

大家好,謝謝你到目前爲止的幫助。如前所述,我沒有在原始文章中測試字符串生成的命令,因爲我寫了沒有Bash的帖子。原來的命令是openssl rand -hex 4 | tr'[:lower:]''[:upper:]' – Paul 2015-01-04 22:58:21

+0

我的答案編輯用於說明修改命令的解決方案「openssl rand -hex 4 | tr'[:lower:]''[:upper:]'」 – repzero 2015-01-04 23:51:24

回答

2

您可以使用該gawk

gawk '/ACCESSION[ \t]*$/{l=$0;cmd="openssl rand -base64 32 | tr '[a-z]' '[A-Z]'";cmd |& getline a;close(cmd);print l,a;next}{print}' /path/to/input > /path/to/output 

這是因爲多腳本更好的可讀性:

#!/usr/bin/gawk -f 

# If a line with an empty ACCESSION field appears 
# The following block gets executed 
/ACCESSION[ \t]*$/ { 
    # Backup current line 
    line=$0 
    # Prepare the openssl command 
    cmd="openssl rand -base64 32 | tr '[a-z]' '[A-Z]'" 
    # Execute the openssl command and store results into random 
    cmd |& getline random; 
    close(cmd); 
    # Print the line 
    printf "%s %s\n", line, random; 
    # Step forward to next line of input. (Don't execute 
    # the following block) 
    next 
} 

# Print all other lines - unmodified 
{print} 

請注意,你需要GNU AWK(gawk)爲,因爲該腳本使用僅與GNU版本awk一起提供的協處理。

+0

你可以使用sed -i.bak ..所以它會在運行sed之前創建files.bak。 – SMA 2015-01-04 11:23:37

+0

是的,好點。 – hek2mgl 2015-01-04 11:25:15

+0

他有一個文本文件有600個條目,而不是600個文件。 – jm666 2015-01-04 11:26:09

1

你可以嘗試如下後跟您的文件

#!/bin/bash 
for i in {1..7}; do 
    var=$(openssl rand -hex 4 | tr '[:lower:]' '[:upper:]'); 
    sed -i.bak '/^ACCESSION $/{s#ACCESSION #&'"${var}"'#g;:tag;n;b tag}' "$1" 
done 

注意我用{} 1..7循環七次,如果我有7行的ACCESSION其次正好三個空格的文件線的和端

例如

ACCESSION 
VERSION 
ACCESSION 
VERSION 
ACCESSION 
VERSION  
ACCESSION 
VERSION  
ACCESSION 
VERSION  
ACCESSION 
VERSION  
ACCESSION 

輸出

ACCESSION E4197EB1 
VERSION 
ACCESSION EFA0CEFF 
VERSION 
ACCESSION 9499CA54 
VERSION  
ACCESSION 2AD2690D 
VERSION  
ACCESSION 3598659F 
VERSION  
ACCESSION 25608153 
VERSION  
ACCESSION 1B43896B 

編輯 由於您使用的是Mac OS X,你可以嘗試其他

#!/bin/bash 
for i in {1..7}; do 
    var=$(openssl rand -hex 4 | tr '[:lower:]' '[:upper:]'); 
    sed -i.bak ' 
    /^ACCESSION $/{ 
    s#ACCESSION #&'"${var}"'#g 
    :tag 
    n 
    b tag 
    }' "$1" 
done 
+0

謝謝你的幫助。你的代碼看起來很有希望,但給我一個錯誤信息。我再次檢查:格式是「ACCESSION」,後面跟着三個空格和一個換行符。任何想法,爲什麼這不適合我?我跑了:'因爲我在{1..621}; do var = $(openssl rand -hex 4 | tr'[:lower:]''[:upper:]'); sed'/^ACCESSION $/{s#ACCESSION#&'「$ {var}」'#g;:tag; n; b tag}'/Users/paul/Documents/140911_c3_analysis/ref_db_COI/150103_AVC_nem_rot_tar_WITH_TAXONOMY.gb done '錯誤信息是'sed:1:「/^ACCESSION $/{s#ACCE ...」:意外的EOF(正在申請)' – Paul 2015-01-05 00:37:16

+0

嘗試將下面的代碼放在一個文件(腳本)中......然後使其成爲可執行的「chmod a + x 。打開一個終端並運行拖拽腳本到終端,接着是一個空格,然後是您的文件..確切代碼 – repzero 2015-01-05 01:33:47

+0

再次感謝您的幫助,我做了您的建議,但錯誤消息仍保持sed :1:「/^ACCESSION $/{s#ACCE ...」:意外的EOF(正在處理)')輸入文件的識別有可能是錯誤的?乾杯並再次感謝 – Paul 2015-01-05 06:43:52

0

非常感謝你的幫助我用@ hek2mgl解決方案,我不能讓sed的命令去。

感謝您在示例代碼中提供註釋。我修改如下:

#!/usr/local/bin/gawk -f 
# If a line with an empty ACCESSION field appears 
# The following block gets executed 
/ACCESSION/ { 
# Backup current line 
line=$0 
# Prepare the openssl command 
cmd="openssl rand -hex 4 | tr '[:lower:]' '[:upper:]'" 
# Execute the openssl command and store results into random 
cmd |& getline random; 
close(cmd); 
# Print the line 
printf "ACCESSION %s\n",random; 
# Step forward to next line of input. (Don't execute 
# the following block) 
next 
} 

# Print all other lines - unmodified 
{print} 
相關問題