【问题标题】:Insert alphanumeric string into text file after certain word (password / sed / awk)在某个单词之后将字母数字字符串插入文本文件(密码/sed/awk)
【发布时间】:2015-01-06 11:12:12
【问题描述】:

我手头有一个包含 690 个条目的文本文件,类似于 P.S. (在 P.S. 中显示的是一个示例,来自这里 http://www.ncbi.nlm.nih.gov/nuccore/AB753792.1)。在我的文本文件中,条目由“//”分隔。

就我而言,在“ACCESSION”(字符串和 3 个空格)之后没有大写字母数字字符串 (例如 P.S. 中的“AB753792”)。我正在使用默认 Bash 运行 MacOSX Yosemite,并希望使用唯一的大写字母数字字符串填充 690 个空格,例如:

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

(5.1.15: 上面的命令我改了,在本帖的第一个版本不一样)

我可以看到 sed / awk 如何解决这个问题,但我不知道 sed 如何能够在每个“ACCESSION”之后插入一个唯一的 8 位大写字母数字字符串。

我很乐意得到帮助。

亲切的问候,

保罗

附言

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
 //    

【问题讨论】:

  • openssl rand -base64 32 | | tr '[a-z]' '[A-Z]' 给出语法错误...应该是“openssl rand -base64 32||tr '[a-z]' '[A-Z]'”?....如果是这样,这不会生成 8 位字母数字组。请告知
  • 大家好,感谢您迄今为止的帮助。如前所述,我没有在原始帖子中测试用于字符串生成的命令,因为我写的帖子没有 Bash 可用。原来的命令是 openssl rand -hex 4 | tr '[:lower:]' '[:upper:]'
  • 我的答案已编辑以使用修改后的命令“openssl rand -hex 4 | tr '[:lower:]' '[:upper:]'”来说明解决方案

标签: bash shell awk sed passwords


【解决方案1】:

您可以为此使用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 的协同进程。

【讨论】:

  • 您可以使用 sed -i.bak.. 这样它会在就地运行 sed 之前创建 files.bak。
  • 他有一个包含 600 个条目的文本文件,而不是 600 个文件。
  • 这让事情变得更容易。添加了g 选项
  • 哦,我刚刚从问题中复制了 openssh 命令。谢谢提示。关于 MAC 上 sed-i 选项,似乎我没有那么完美(从未使用过 MAC)... 将更新我的答案。
  • 是的,这似乎更稳定,补充说。谢谢你的帮助! :)
【解决方案2】:

您可以按照以下方式尝试,然后是您的文件

#!/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

请注意,如果我的文件包含 7 行 ACCESSION 后跟 正好三个空格 和行尾

,我会使用 {1..7} 循环七次

例如

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

【讨论】:

  • 感谢您的帮助。您的代码看起来很有前途,但给了我一条错误消息。我再次检查:格式是“ACCESSION”,后跟三个空格和一个换行符。任何想法为什么这对我不起作用?我跑了:for i in {1..621}; do var=$(openssl rand -hex 4 | tr '[:lower:]' '[:upper:]'); sed '/^ACCESSION $/{s#ACCESSION #&amp;'"${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 ...": unexpected EOF (pending }'s)
  • 尝试将以下代码放入文件(脚本)...然后使其可执行 "chmod a+x . 打开终端并将脚本拖入终端,后跟空格,然后你的文件..确切的代码
  • 再次感谢您的帮助。我按照您的建议做了,但错误消息仍然存在 sed: 1: "/^ACCESSION $/{s#ACCE ...": unexpected EOF (pending }'s) 输入文件的识别可能有问题?再次欢呼和感谢。
  • 我看到您使用的是 MAC OSX...我编辑了我的答案以在 OSX 上运行它..请参阅旧答案下方的编辑....还有出现错误的原因可能是由于问题在这里stackoverflow.com/questions/15467616/…
【解决方案3】:

非常感谢您的帮助我使用了@hek2mgl 解决方案,因为我无法运行 sed 命令。

感谢您在示例代码中提供 cmets。我修改如下:

#!/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}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-10
    • 1970-01-01
    • 1970-01-01
    • 2017-05-25
    • 2018-08-20
    • 1970-01-01
    • 1970-01-01
    • 2017-06-18
    相关资源
    最近更新 更多