简体   繁体   English

如何使用bash从文本文件中提取列

[英]how to extract columns from a text file with bash

I have a text file like this.我有一个这样的文本文件。

 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 extract the names of first column(res) based on the values of last column(sum).我想根据最后一列(总和)的值提取第一列(res)的名称。 I have to print resnames if sum >25 and sum<25.如果总和 > 25 和总和 < 25,我必须打印重命名。 How can I get the output like this?我怎样才能得到这样的输出?

While you can do this with a while read loop in bash , it's easier, and most likely faster, to use awk虽然您可以使用bashwhile read循环执行此操作,但使用awk更容易,而且很可能更快

awk '$5 != 25 { print $1 }'

Note that your logic print resnames if sum >25 and sum<25 is the same as print if sum != 25 .请注意, print resnames if sum >25 and sum<25print if sum != 25相同,则您的逻辑print resnames if sum >25 and sum<25

这应该这样做:

awk 'BEGIN{FS=OFS=" "}{if($5 != 25) print $1}' bla.txt

Consider using awk .考虑使用awk Its a simple tool for processing columns of text (and much more).它是一个用于处理文本列(以及更多)的简单工具。 Here's a simple awk tutorial which will give you an overview.这是一个简单的awk 教程,它将为您提供概述。 If you want to use it within a bash script, then this tutorial should help.如果您想在 bash 脚本中使用它,那么教程应该会有所帮助。

Run this on the command line to give you an idea of how you could do it:在命令行上运行此命令,让您了解如何执行此操作:

> echo "SER A   1   161.15 138.3" | awk '{ if($5 > 25) print $1}'
> SER
> echo "SER A   1   161.15 138.3" | awk '{ if($5 > 140) print $1}'
> 
while read line
do 
v=($line)
sum=${v[4]}
((${sum/.*/} >= 25)) && echo ${v[0]}
done < file

You need to skip the first line.您需要跳过第一行。

Since bash doesn't handle floating point values, this will print 25 which isn't exactly bigger than 25.由于 bash 不处理浮点值,这将打印 25,它不完全大于 25。

This can be handled with calling bc for arithmetics.这可以通过调用 bc 进行算术来处理。

tail -n +2 ser.dat | while read line
do  
  v=($line)
  sum=${v[4]}
  gt=$(echo "$sum > 25" | bc) && echo ${v[0]}
done

what about the good old cut?好的旧剪裁怎么样? :) :)

say you would like to have the second column,说你想要第二列,

cat your_file.txt | sed 's, +, ,g' | cut -d" " -f 2

what is doing sed in this command? sed 在这个命令中做什么? cut expects columns to be separated by a character or a string of fixed length (see documentation). cut 期望列由固定长度的字符或字符串分隔(请参阅文档)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM