Linux 如何使用bash从文本文件中提取列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10363996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to extract columns from a text file with bash
提问by Joel
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). I have to print resnames if sum >25 and sum<25. How can I get the output like this?
我想根据最后一列(总和)的值提取第一列(res)的名称。如果总和 > 25 和总和 < 25,我必须打印重命名。我怎样才能得到这样的输出?
采纳答案by nullpotent
This should do it:
这应该这样做:
awk 'BEGIN{FS=OFS=" "}{if( != 25) print }' bla.txt
回答by Tim Pote
While you can do this with a while read
loop in bash
, it's easier, and most likely faster, to use awk
虽然您可以使用while read
in 循环来做到这一点bash
,但使用起来更容易,而且很可能更快awk
awk ' != 25 { print }'
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<25
与print if sum != 25
.
回答by Will Demaine
Consider using awk
. Its a simple tool for processing columns of text (and much more). Here's a simple awk tutorialwhich will give you an overview. If you want to use it within a bash script, then thistutorial should help.
考虑使用awk
. 它是一个用于处理文本列(以及更多)的简单工具。这是一个简单的awk 教程,它将为您提供概述。如果您想在 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( > 25) print }'
> SER
> echo "SER A 1 161.15 138.3" | awk '{ if( > 140) print }'
>
回答by user unknown
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
回答by jpmuc
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? cut expects columns to be separated by a character or a string of fixed length (see documentation).
sed 在这个命令中做什么?cut 期望列由固定长度的字符或字符串分隔(请参阅文档)。