如何将新行字符转换为制表符,然后在bash的循环中插入新行字符
我的输入目录中有一些如何将新行字符转换为制表符,然后在bash的循环中插入新行字符,bash,for-loop,Bash,For Loop,我的输入目录中有一些bam文件,对于每个bam文件,我想计算映射读取的数量(使用Samtools view命令),并将该数字与bam文件的名称一起打印到输出文件中。虽然它在工作,但我没有得到我想要的输出 下面是我的代码的样子 for file in input/*; do echo $file >> test.out; samtools view -F 4 $file | wc -l >> output; d
bam
文件,对于每个bam
文件,我想计算映射读取的数量(使用Samtools view
命令),并将该数字与bam
文件的名称一起打印到输出文件中。虽然它在工作,但我没有得到我想要的输出
下面是我的代码的样子
for file in input/*;
do
echo $file >> test.out;
samtools view -F 4 $file | wc -l >> output;
done
这很好,但问题是它输出文件名和不同行中的读取次数。这里有一个例子
sample_data/wgEncodeUwRepliSeqBg02esG1bAlnRep1.bam
1784867
sample_data/wgEncodeUwRepliSeqBg02esG2AlnRep1.bam
2280544
我试图通过这样做将新行字符转换为制表符
for file in input/*;
do
echo $file >> output;
samtools view -F 4 $file | wc -l >> output;
tr '\n' '\t' < output > output2
done
现在如何在每行后面插入新行字符?比如说
sample_data/wgEncodeUwRepliSeqBg02esG1bAlnRep1.bam 1784867
sample_data/wgEncodeUwRepliSeqBg02esG2AlnRep1.bam 2280544
谢谢如果每个文件的输出都明确包含一个文件名和一个数字,我想您可以轻松更改
tr '\n' '\t' < output > output2
tr'\n'\t'
到
tr'\n'\t'
它将匹配后跟制表符的数字,然后添加一个新行字符。您可以通过将所有内容写入一行来获得所需的输出。比如:
echo -e "$file\t$(samtools view -F 4 $file | wc -l)" >> output;
如果要分两部分执行,请注意,echo
有一个-n
选项来抑制尾随的换行符,还有-e
来解释转义,如\t
,因此您可以执行以下操作:
echo -ne "$file\t" >> $output
samtools view -F 4 $file | wc -l >> output
第一次写入所需内容比试图对输出进行后期处理更简洁。只需使用命令替换即可:
for file in input/*
do
printf '%s\t%d\n' "$file" "$(samtools view -F 4 $file | wc -l)"
done >> output
你为什么不建一条线呢
echo-e“$file\t$var”
,其中$var
包含samtools视图的输出…
。运行良好。是,所有文件的输出都相同。我怎么忘记了sed的强大功能呢。顺便问一下,\1
在那里做什么?它是否用于捕获与R
类似的组?谢谢..是的,\1
指的是匹配的第一个组件,即([0-9]+\t),使用括号将其括起来。:-)
echo -ne "$file\t" >> $output
samtools view -F 4 $file | wc -l >> output
for file in input/*
do
printf '%s\t%d\n' "$file" "$(samtools view -F 4 $file | wc -l)"
done >> output