English 中文(简体)
多个文本档案中的一栏有印本
原标题:extract columns from multiple text files with bash
  • 时间:2012-05-03 02:03:06
  •  标签:
  • linux
  • bash

I am trying to extract columns from multiple text files(3000 files). The sample of my text file is shown below.

res          ABS    sum
SER A   1   161.15 138.3 
CYS A   2    66.65  49.6
PRO A   3    21.48  15.8 
ALA A   4    77.68  72.0 
ILE A   5    15.70   9.0
HIS A   6    10.88   5.9

I would like to print 1) resnames(first column) only if the sum(last column) is >25. 2) I would like to store the output in to one file 3) I would like to add a new column to the outputfile with the name of the txt file from where the data was extracted and also need to print the total number of resnames( from all text files only if sum is >25)

我谨获得以下产出:

SER   AA.txt
CYS   AA.txt
ALA   AA.txt
SER   BB.txt

Total numberof  SER- 2
Total number of ALA- 1
Total number of CYS- 1

我怎样才能以巴什获得这一产出? 我尝试了以下法典:

for i in  files/*.txt
do
awk  BEGIN{FS=OFS=" "}{if($5 > 25) print $1,i} 
done

Any suggestions please?

最佳回答
awk  {
    if ($NF ~ /([0-9])+(.)?([0-9])+/ && $NF > 25) {
        print $1, FILENAME;
        res[$1]++;
    }
}
END {
     for (i in res) {
          print "Total number of ", i, "-", res[i];
    }
}  res.txt

这里,我以你的榜样取得了以下成果:

SER res.txt
CYS res.txt
ALA res.txt
Total number of  SER - 1
Total number of  CYS - 1
Total number of  ALA - 1
问题回答

Try:

awk  { a[$1]++ } 
     END { for (k in a) print "Total number of " k " - " a[k] }  FILES

(无试验)





相关问题
Signed executables under Linux

For security reasons, it is desirable to check the integrity of code before execution, avoiding tampered software by an attacker. So, my question is How to sign executable code and run only trusted ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签