读取输入号码和它在一个文件重定向到列 [英] Read an input number and redirect it to a column in a file

查看:187
本文介绍了读取输入号码和它在一个文件重定向到列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的表文件:

我已经做了我的程序返回我的值(最大,最小和平均),从我在寻找基因的线。现在我的目标是相同的,但不是的话,用户将打印列数。自我到只有从一列获得这些值,但

在这里有我的code:

 #!/斌/庆典FICHERO =affy.txt功能选项
{
   回声_____________OPTIONS_____________
   回声
   回声1.选择一个基因和它​​的测试结果
   回声2.选择一列,其测试结果
   回声3.退出
}功能基因
{
   如果[-eaffy.txt];然后#硅EL fichero existe ...
      回声打印您正在寻找基因的名字:
      阅读-p姓名:NAME
      OLDIFS = $ IFS
      IFS =,;
      #Calcular最大值,最小值Y平均值。
      分=`grep的-M1$ NAMEaffy.txt | TR -s','''。 | TR -s''的'\\ n'|切-d'-f3- |排序-n |头-1`
      最大=`grep的-M1$ NAMEaffy.txt | TR -s''''| TR -s',''。 |切-d'-f3- | TR -s''的'\\ n'|排序-n |尾-1`
      平均=`grep的-M1$ NAMEaffy.txt | TR -s''''| TR -s',''。 |切-d'-f3- | TR -s''的'\\ n'| AWK'{总和+ = $ 1} END {打印总和/ NR}'`      回声最小值:$分钟
      回声最大值:$最大
      回声平均值$意思
   其他
      回声无效的基因的名字!
   科幻   回声
}功能栏
{
   如果[-e $ FICHERO]。然后
      回声打印您正在寻找的列数:
      阅读-p姓名:NAME
   其他
      回声厄尔尼诺fichero没有existe O否contiene入住日期恩拉议程
   科幻
}OPC = 0
退出= 5而[$ OPC -ne $退出]

   明确
   选项​​#Dibujamos埃尔菜单连接pantalla
   阅读-pOpcion:......OPC#Escogemos拉opcion deseada   如果[$ OPC -ge 1]安培;&安培; [$ OPC -le 5];然后
      明确
      在#Acciones对拉斯维加斯diferentes opciones德尔菜单的情况下$ OPC         1)基因
            ;;         2)柱
            ;;
      ESAC
  其他
  回声插入正确的选项!  科幻
  回声preSS任意键......
  读
  DONE

选项号1正在工作。

我想这样的事情在指定的列的功能,但它不工作...:

 功能栏
{
   如果[-s $ FICHERO]。然后
      回声打印您正在寻找的列数:
      阅读-p列:列
      因为我在$栏
      做
         COL + =$ {I# - },
         回声:你跟列号工作:$ COL
      DONE   其他
      回声厄尔尼诺fichero没有existe O否contiene入住日期恩拉议程
   科幻   如果[$ COL=];然后
          回声插入列,请!
   其他
      因为我在$关口;
      做
      回声
      回声微量columna =`TR -s',''。 affy.txt | TR -s''的'\\ n'|切-d''-f$山坳|排序-n |头-1`
      回声格言columna$ I=`grep的$ COLaffy.txt | TR -s''''| TR -s',''。 |切-d''-f$ I|排序-n |尾-1`
      回声平均columna$ I=`grep的$ COLaffy.txt | TR -s''''| TR -s',''。 |切-d''-f$ I| AWK'{总和+ = $ 0} END {打印总和/ NR}'`      转移
      DONE
   科幻


解决方案

有一个简单的方法。忘掉整个程序,并只专注于手头的问题。你会如何​​普遍得到一栏打印?鉴于你的文件。

 基因RESULT1 RESULT2 RESULT3
基因1 1 6 9
基因2 2 6 7
基因3 2 4 9
GENE4 1 6 9

目标:获取由位置数的结果值(1,2,3,等等。)。
输入:正整数> = 1,但< = 3 {X | 1所述; = X&下; = 3}
输出:一列数字

隐式的真理:第1列($ 1)是用于标签和它没有返回的列应该永远都头信息。这使得下面的真正公平的竞争环境:

  1 6 9
2 6 7
2 4 9
1 6 9

解决方案:

用户输入。


  1. 你的程序应该做使人们有可能用数字1眼下的第一本节中,$ 1将引用GENE标贴的列。因此,输入的任何值时,加1,这样你总能得到你真正想要的列。因此,零和负号不从用户接受的答复。另外,还有一些只有三个在场上总列。因此,数字3是你的程序应该处理(现在)数量最多的。


  2. 你应该使用一个命令,将打印所有行考虑的第二件事除了的第一行。头部和尾部的命令有很多选择。

    尾-n 4


这是yeilds ...

 基因1 1 6 9
基因2 2 6 7
基因3 2 4 9
GENE4 1 6 9

因此​​,当命令上述通过管道输送到awk的

 阅读$ REPLY(2)*< ----用户输入2 *REPLY = $(($ REPLY + 1))尾-n 4 | awk的{$打印}答复(其中$ REPLY真的= 3)6
6
4
6

轰!第二列被打印。
我想你的可能的preFER集成这个解决方案到你的程序比这里列出的其他人。

I have a file with a table like this:

I have done that my program returns me the values (max, min and mean) from the line of the gene that I'm looking for. Now my goal is the same, but instead of words, the user will print the number of the column. Ego to obtain these values but only from one column.

Here there is my code:

#!/bin/bash

FICHERO="affy.txt"

function OPTIONS
{
   echo "_____________OPTIONS_____________"
   echo ""
   echo "   1. Select one gene and its test results"
   echo "  2. Select one column and its test results"
   echo "               3. Exit"
}

function gene
{
   if [ -e "affy.txt" ]; then  # Si el fichero existe...
      echo "Print the name of the gene you are looking for: "
      read -p "Name:" NAME
      OLDIFS=$IFS
      IFS=","; 
      # Calcular max, min y mean.
      min=` grep -m1 "$NAME" affy.txt |tr -s ',' '.' | tr -s ' ' '\n' | cut -d' ' -f3- |  sort -n | head -1`
      max=` grep -m1 "$NAME" affy.txt  | tr -s '  ' ' ' |tr -s ',' '.' | cut -d ' ' -f3- | tr -s ' ' '\n' | sort -n | tail -1`
      mean=` grep -m1 "$NAME" affy.txt | tr -s '  ' ' ' |tr -s ',' '.' | cut -d ' ' -f3- | tr -s ' ' '\n' | awk '{sum+=$1} END {print sum/NR}'`

      echo "Min value: "$min
      echo "Max value: "$max
      echo "Mean value: "$mean


   else
      echo "Invalid gene name!"
   fi

   echo
}

function column
{   
   if [ -e $FICHERO ]; then
      echo "Print the column number you are looking for: "
      read -p "Name: " NAME


   else
      echo "El fichero no existe o no contiene entradas en la agenda"
   fi
}

opc=0
exit=5

while [ $opc -ne $exit ];
do   
   clear
   OPTIONS  # Dibujamos el menu en pantalla
   read -p "Opcion:..." opc  # Escogemos la opcion deseada

   if [ $opc -ge 1 ] && [ $opc -le 5 ]; then
      clear
      case $opc in   # Acciones para las diferentes opciones del menu

         1)gene   
            ;;

         2)column
            ;;
      esac
  else
  echo "Insert a correct option!!"

  fi
  echo "Press any key..."
  read
  done

OPTION NUMBER 1 IS WORKING.

I tried something like this in the function named column, but it doesn't works...:

    function column
{   
   if [ -s $FICHERO ]; then
      echo "Print the column number you are looking for: "
      read -p "column: " column
      for i in "$column"
      do
         col+="${i#-}"","
         echo "You are working with column number:" $col
      done

   else
      echo "El fichero no existe o no contiene entradas en la agenda"
   fi

   if [ "$col" = "" ]; then
          echo "Insert Columns please!"
   else
      for i in $col; 
      do 
      echo
      echo minim columna= `tr -s ',' '.' affy.txt | tr -s ' ' '\n' | cut -d' ' -f"$col" |  sort -n | head -1`
      echo maxim columna "$i"= `grep "$col" affy.txt | tr -s '  ' ' ' |tr -s ',' '.' | cut -d ' ' -f"$i" | sort -n | tail -1`
      echo average columna "$i"= `grep "$col" affy.txt | tr -s '  ' ' ' |tr -s ',' '.' | cut -d ' ' -f"$i" | awk '{sum+=$0} END {print sum/NR}'`

      shift
      done
   fi

解决方案

There is a simpler way. Forget about the entire program and just focus on the problem at hand. How would you generally get a column to print? Given your file.

GENE  RESULT1 RESULT2 RESULT3
GENE1    1       6       9
GENE2    2       6       7
GENE3    2       4       9
GENE4    1       6       9

GOAL: Obtain RESULT values by positional number (1, 2, 3, etc..). INPUT: A positive integer >= 1, yet <= 3 {x| 1 <= x <= 3} OUTPUT: A column of numbers.

Implicit truths: Column 1 ($1) is for labels and no returned column should ever have header information in it. That makes the following the real playing field:

1       6       9
2       6       7
2       4       9
1       6       9

Solution:

User input.

  1. The first this section of your program should do make it possible to use the number 1. Right now, $1 would refer to the column of GENE lables. Thus, when any value is entered, add 1 to it so that you always get the column that you really want. Thus, zero and negative numbers are not acceptable replies from the user. Also, there are only three total columns in play. Thus, the number 3 is the largest number your program should handle (right now).

  2. The second thing you should consider is using a command that will print all lines except the first line. The head and tail commands have many options.

    tail -n 4

This is yeilds ...

GENE1    1       6       9
GENE2    2       6       7
GENE3    2       4       9
GENE4    1       6       9

Thus, when the a command above is piped to awk

read $REPLY  (2)  *<----User inputs 2*

REPLY=$(($REPLY + 1))

tail -n 4 | awk "{print $REPLY}"       (where $REPLY really = 3)

6
6
4
6

Boom! The second column is printed. I think you may prefer integrating this solution into your program than others listed here.

这篇关于读取输入号码和它在一个文件重定向到列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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