Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 读取文件行,执行web操作,并使用终端将输出存储为单独的文件_Shell_Unix_Curl_Awk - Fatal编程技术网

Shell 读取文件行,执行web操作,并使用终端将输出存储为单独的文件

Shell 读取文件行,执行web操作,并使用终端将输出存储为单独的文件,shell,unix,curl,awk,Shell,Unix,Curl,Awk,我需要读取input.txt中的行并忽略以“>”开头的行,然后读取下一行并使用web工具以fasta格式获取输出。我已经编写了代码,但到目前为止无法忽略'>'行,并且希望以一种更简单的方式更改行的名称,例如给定的示例(output_1.fasta) $i=0; 当读取行时: if line:do curl-s-d“dna_sequence=“$line”&output_format=fasta”https://web.expasy.org/cgi-bin/translate/dna2aa.cgi

我需要读取input.txt中的行并忽略以“>”开头的行,然后读取下一行并使用web工具以fasta格式获取输出。我已经编写了代码,但到目前为止无法忽略'>'行,并且希望以一种更简单的方式更改行的名称,例如给定的示例(output_1.fasta)

$i=0;
当读取行时:
if line:do curl-s-d“dna_sequence=“$line”&output_format=fasta”https://web.expasy.org/cgi-bin/translate/dna2aa.cgi >>我的${line}.fasta$i+1;完成<'input.txt'
输入文件
>A123
ATTGGGCTTTT
>B1234
GGGCCCTTAAA
输出_1.fasta
>A123
#web服务器的全部输出
GHHGSSSAAA
输出2.fasta
>B1234
hhjjkklll

<>代码> 你现在已经接近了一个复杂程度,在这里使用BASH不再有意义,你应该考虑把它移植到一个更合适的脚本语言,IMO。另外,您没有正确转义$line,如果$line包含
&foo=bar
,会发生什么情况?curl不会将其解释为
dna\u序列的一部分,curl会认为它是一个名为
foo
的全新变量,包含
bar
。。这里有一个端口:

#/usr/bin/env-php

Bash解决方案:

#!/bin/env bash
i=0
while IFS=  read -r -d $'\n'
do
  ((i++))
  curl -s -d "dna_sequence=${REPLY}&output_format=fasta" 'https://web.expasy.org/cgi-bin/translate/dna2aa.cgi' > "./output_${i}.fasta"
done < <( sed '/^>/d' "./input.txt" )
exit 0
#/bin/env bash
i=0
而IFS=read-r-d$'\n
做
((i++)
curl-s-d“dna_序列=${REPLY}&output_格式=fasta”https://web.expasy.org/cgi-bin/translate/dna2aa.cgi“>”/output_${i}.fasta”
完成B1234
GGGCCCTTAAA
$i=0
$while IFS=read-r-d$'\n
>做
>((i++)
>curl-s-d“dna_序列=${REPLY}&output_格式=fasta”https://web.expasy.org/cgi-bin/translate/dna2aa.cgi“>”/output_${i}.fasta”
>完成VIRT-65321:3'5'帧2
克格勃
>VIRT-65321:3'5'帧3
卡Q
>VIRT-65321:5'3'帧1
IGPF
>VIRT-65321:5'3'帧2
LGL
>VIRT-65321:5'3'帧3
瓦夫
$cat./output_2.fasta
>VIRT-65327:3'5'帧1
FKG
>VIRT-65327:3'5'帧2
上帝军
>VIRT-65327:3'5'帧3
-全科医生
>VIRT-65327:5'3'帧1
GPL
>VIRT-65327:5'3'帧2
全科医生-
>VIRT-65327:5'3'帧3
ALK

<代码>添加一个SeBand然后粘贴脚本:你现在接近了一个复杂程度,在这里使用BASH不再有意义,你应该考虑把它移植到一个更好的脚本语言中,imo.@shome我很好奇-你选择了这个作为答案,但后来你把它改成了你问题中的预期输出。怎么搞的?
#!/usr/bin/env php
<?php
$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_URL => 'https://web.expasy.org/cgi-bin/translate/dna2aa.cgi',
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_ENCODING => ''
));
foreach (file('input.txt', FILE_SKIP_EMPTY_LINES) as $line) {
    $line = trim($line);
    if (!strlen($line) || $line[0] === '>') {
        continue;
    }
    curl_setopt_array($ch, array(
        CURLOPT_POST => 1,
        CURLOPT_POSTFIELDS => http_build_query(array(
            'dna_sequence' => $line,
            'output_format' => 'fasta'
        ))
    ));
    file_put_contents("my_{$line}.fasta", curl_exec($ch));
}
curl_close($ch);
$ cat tst.sh
#!/bin/env bash

i=0
while IFS= read -r line; do
    if [[ $line =~ ^\> ]]; then
        outfile="output_((++i)).fasta"
        printf '%s\n' "$line" > "$outfile"
    else
        curl -s -d 'dna_sequence="'"$line"'"&output_format=fasta' 'https://web.expasy.org/cgi-bin/translate/dna2aa.cgi' >> "$outfile"
    fi
done < input.txt
$ ./tst.sh
$ cat output_1.fasta
>A123
> VIRT-92094:3'5' Frame 1
KRPN
> VIRT-92094:3'5' Frame 2
KGP
> VIRT-92094:3'5' Frame 3
KAQ
> VIRT-92094:5'3' Frame 1
IGPF
> VIRT-92094:5'3' Frame 2
LGL
> VIRT-92094:5'3' Frame 3
WAF
$ cat output_2.fasta
>B1234
> VIRT-92247:3'5' Frame 1
FKG
> VIRT-92247:3'5' Frame 2
LRA
> VIRT-92247:3'5' Frame 3
-GP
> VIRT-92247:5'3' Frame 1
GPL
> VIRT-92247:5'3' Frame 2
GP-
> VIRT-92247:5'3' Frame 3
ALK
#!/bin/env bash
i=0
while IFS=  read -r -d $'\n'
do
  ((i++))
  curl -s -d "dna_sequence=${REPLY}&output_format=fasta" 'https://web.expasy.org/cgi-bin/translate/dna2aa.cgi' > "./output_${i}.fasta"
done < <( sed '/^>/d' "./input.txt" )
exit 0
$ cat ./input.txt
>A123
ATTGGGCCTTTT
>B1234
GGGCCCTTAAA
$ i=0
$ while IFS=  read -r -d $'\n'
> do
>   ((i++))
>   curl -s -d "dna_sequence=${REPLY}&output_format=fasta" 'https://web.expasy.org/cgi-bin/translate/dna2aa.cgi' > "./output_${i}.fasta"
> done < <( sed '/^>/d' "./input.txt" )
$ ls -1 ./output_*
./output_1.fasta
./output_2.fasta
$ cat ./output_1.fasta
> VIRT-65321:3'5' Frame 1
KRPN
> VIRT-65321:3'5' Frame 2
KGP
> VIRT-65321:3'5' Frame 3
KAQ
> VIRT-65321:5'3' Frame 1
IGPF
> VIRT-65321:5'3' Frame 2
LGL
> VIRT-65321:5'3' Frame 3
WAF
$ cat ./output_2.fasta
> VIRT-65327:3'5' Frame 1
FKG
> VIRT-65327:3'5' Frame 2
LRA
> VIRT-65327:3'5' Frame 3
-GP
> VIRT-65327:5'3' Frame 1
GPL
> VIRT-65327:5'3' Frame 2
GP-
> VIRT-65327:5'3' Frame 3
ALK