簡體   English   中英

在bash中交替輸出來自兩個grep的循環

[英]Alternating output in bash for loop from two grep

我正在嘗試搜索文件,並在它們每次出現在文件中時提取出兩條相關信息。 我目前擁有的代碼:

#!/bin/bash
echo "Utilized reads from ustacks output" > reads.txt
str1="utilized reads:"
str2="Parsing"
for file in /home/desaixmg/novogene/stacks/sample01/conda_ustacks.o*; do
    reads=$(grep $str1 $file | cut -d ':' -f 3
    samples=$(grep $str2 $file | cut -d '/' -f 8
    echo $samples $reads >> reads.txt
done

它為文件做每一行(文件具有這些短語實例的數量不等),並為我提供每個文件每行的輸出:

PopA_15.fq 1081264
PopA_16.fq PopA_17.fq 1008416 554791
PopA_18.fq PopA_20.fq PopA_21.fq 604610 531227 595129
...

我希望它與每個實例匹配(即,兩個實例的第一個實例彼此相鄰):

PopA_15.fq 1081264
PopA_16.fq 1008416
PopA_17.fq 554791
PopA_18.fq 604610
PopA_20.fq 531227
PopA_21.fq 595129
...

我該怎么做呢? 謝謝

考慮到您的Input_file與顯示的示例相同,並且每行的列數是偶數,具有1個PopA值,而其他行將具有數字值。 跟隨awk可能會幫助您。

awk '{for(i=1;i<=(NF/2);i++){print $i,$((NF/2)+i)}}'  Input_file

輸出如下。

PopA_15.fq 1081264
PopA_16.fq 1008416
PopA_17.fq 554791
PopA_18.fq 604610
PopA_20.fq 531227
PopA_21.fq 595129

如果您想將命令的輸出傳遞給awk命令,則可以像your command | awk command... your command | awk command...無需在上述awk命令中添加Input_file。

這就是最終對我有用的...絕對歡迎使用任何更高效代碼的提示

#!/bin/bash
echo "Utilized reads from ustacks output" > reads.txt
str1="utilized reads:"
str2="Parsing"
for file in /home/desaixmg/novogene/stacks/sample01/conda_ustacks.o*; do
    reads=$(grep $str1 $file | cut -d ':' -f 3)
    samples=$(grep $str2 $file | cut -d '/' -f 8)
    paste <(echo "$samples" | column -t) <(echo "$reads" | column -t) >> reads.txt
done

這提供了上述所需的輸出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM