awk输出匹配到单独的文件

时间:2015-10-09 17:59:10

标签: awk

我正在尝试将$2中的文本组合在一起,并将它们输出到单独的文件中,匹配项是新文件的名称。由于实际文件非常大,我打开每个文件,然后关闭以节省速度和内存,我的尝试在下面。谢谢你:)。

awk '{printf "%s\n", $2==$2".txt"; close($2".txt")}' input.txt **'{ print $2 > "$2.txt" }'**

input.txt中

chr19:41848059-41848167 TGFB1:exon.2;TGFB1:exon.3;TGFB1:exon.4 284.611 108 bases
chr15:89850833-89850913 FANCI:exon.20;FANCI:exon.27;FANCI:exon.32;FANCI:exon.33;FANCI:exon.34 402.012 80 bases
chr15:31210356-31210508 FANC1:exon.6;FANC1:exon.7 340.914 152 bases
chr19:41850636-41850784 TGFB1:exon.1;TGFB1:exon.2;TGFB1:exon.3 621.527 148 bases

TGFB1.txt的所需输出

chr19:41848059-41848167 TGFB1:exon.2;TGFB1:exon.3;TGFB1:exon.4 284.611 108 bases
chr19:41850636-41850784 TGFB1:exon.1;TGFB1:exon.2;TGFB1:exon.3 621.527 148 bases

FANC1.txt的所需输出

chr15:89850833-89850913 FANCI:exon.20;FANCI:exon.27;FANCI:exon.32;FANCI:exon.33;FANCI:exon.34 402.012 80 bases
chr15:31210356-31210508 FANC1:exon.6;FANC1:exon.7 340.914 152 bases

编辑:

awk -F '[ :]' '{f = $3 ".txt";  close($3 ".txt")} print > f}' BMF_unix_loop_genes_IonXpress_008_150902_loop_genes_average_IonXpress_008_150902.bed > /home/cmccabe/Desktop/panels/BMF **/"$f".txt;**
bash: /home/cmccabe/Desktop/panels/BMF: Is a directory

2 个答案:

答案 0 :(得分:2)

您需要将第二个字段拆分为所需的字段名称。这应该

String urlAddress = "https://graph.facebook.com/" + postId + "/comments?access_token=" + "{my_access_token}";
        try {
            URL url = new URL(urlAddress);
            HttpsURLConnection httpURLConnection = (HttpsURLConnection) url
                    .openConnection();
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setReadTimeout(10000);
            httpURLConnection.setConnectTimeout(10000);
            httpURLConnection.setRequestMethod("GET");
            String response = ConnectionController.getResponse(httpURLConnection);
            Log.v(Constants.TAG, urlAddress);
            Log.v(Constants.TAG, response);

        } catch (IOException e) {
            e.printStackTrace();
        }

请注意,由于您在其中一个字段

中输入了拼写错误,因此无法准确生成输出
$ awk 'BEGIN{close(p)} {split($2,f,":"); p=f[1]".txt"; print $0 > p }' file

答案 1 :(得分:2)

您可以重新定义字段分隔符以包含冒号,然后文件名将为$ 3

<?php
$resultSet = $db->query("SELECT * FROM Articles");
if ($resultSet->num_rows != 0) {
    $count = 0;
    while ($rows = $resultSet->fetch_assoc()) {
        $num_in_row = 4; // Number of items you want in each row
        if($count % $num_in_row == 0){
            echo '<div class="row">'; // if the row already has 4 items, add a new row.
        }
        $image = $rows["image"];
        $text = $rows["text"];
        echo "<span class='wrapper'>";
        echo '<img class="images" src='.$image.'> <div class="texts">'.$text.'</p>';
        echo "</span>";
        if($count % $num_in_row == ($num_in_row-1)){
            echo '</tr>';
        }
        $count++;
    }
} 
?>

我遇到了一些问题,在重定向右侧构建文件名是有问题的,这就是我使用变量的原因。然而,星期五下午的啤酒车已经存在,我无法回忆具体的细节:/

除非您预计会生成数百或数千个新文件,否则我不会费心关闭文件。