将我的Shell脚本的输出组织到文本文件内的表中 [英] Organizing the output of my shell script into tables within the text file

查看:192
本文介绍了将我的Shell脚本的输出组织到文本文件内的表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用unix shell脚本,该脚本进行基因组构建然后创建系统发育.根据您使用的基因组组装器,最终输出(系统发育)可能会发生变化.我希望比较使用各种基因组组装器的效果.我已经开发出一些度量标准进行比较,但是我需要组织它们的帮助,以便我可以进行有用的分析.我想将我的数据按列导入excel.

I am working with a unix shell script that does genome construction then creates a phylogeny. Depending on the genome assembler you use, the final output (the phylogeny) may change. I wish to compare the effects of using various genome assemblers. I have developed some metrics to compare them on, but I need help organizing them so I can run useful analyses. I would like to import my data into excel in columns.

这是我用来输出数据的脚本:

This is the script I am using to output data:

echo "Enter the size (Mb or Gb) of your data set:"
read SIZEOFDATASET
echo "The size of your data set is $SIZEOFDATASET"
echo "Size of Data Set:" >> metrics_file.txt 
echo $SIZEOFDATASET >> metrics_file.txt

echo "Enter the name of your assembler"
read NAMEOFASSEMBLER
echo "You are using $NAMEOFASSEMBLER as your assembler"
echo "Name of Assembler:" >> metrics_file.txt 
echo "$NAMEOFASSEMBLER" >> metrics_file.txt
echo "Time:" >> metrics_file.txt

输出当前如下所示:

Size of Data Set:
387 Mb
Name of Assembler:
Velvet
Genome Size:
1745690
Time:

我希望它看起来像这样:

I want it to look something like this:

预先感谢!

推荐答案

#!/bin/sh

in_file=in.txt      # Input file
params=3            # Parameters count
res_file=$(mktemp)  # Temporary file
sep=' '             # Separator character

# Print header
cnt=0
for i in $(cat $in_file | head -$((params*2))); do
    if [ $((cnt % 2)) -eq 0 ]; then
        echo $i
    fi
    cnt=$((cnt+1))
done | sed ":a;N;\$!ba;s/\n/$sep/g" >>$res_file

# Parse and print values
cnt=0
for i in $(cat $in_file); do
    # Print values, skip param names
    if [ $((cnt % 2)) -eq 1 ]; then
        echo -n $i >>$res_file
    fi

    if [ $(((cnt+1) % (params*2))) -eq 0 ]; then
        # Values line is finished, print newline
        echo >>$res_file
    elif [ $((cnt % 2)) -eq 1 ]; then
        # More values expected to be printed on this line
        echo -n "$sep" >>$res_file
    fi

    cnt=$((cnt+1))
done

# Make nice table format
cat $res_file | column -t
rm -f $res_file

说明

此脚本假定:

Explanation

This scripts assumes that:

  • 输入文件称为"in.txt"(请参见 in_file 变量)
  • 输入文件使用您所描述的格式
  • 结果表应具有3列(请参见 params 变量)
  • input file is called "in.txt" (see in_file variable)
  • input file uses format you described in question
  • result table should have 3 columns (see params variable)

大多数代码只是解析您的输入数据格式.实际的列格式是通过column工具完成的.

Most of the code is just parsing of your input data format. Actual column formatting is done by column tool.

如果要将此表导出为ex​​cel,只需将 sep 变量更改为','并将结果输出保存到 .csv 文件中.此文件可以轻松导入excel应用程序.

If you want to export this table to excel, just change sep variable to ',' and save result output to .csv file. This file can be easily imported in excel application.

输入文件:

Size
387
Name
Velvet
Time
13
Size
31415
Name
Minia
Time
18
Size
31337
Name
ABCDEF
Time
42

脚本输出:

Size   Name    Time
387    Velvet  13
31415  Minia   18
31337  ABCDEF  42

这篇关于将我的Shell脚本的输出组织到文本文件内的表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆