【问题标题】:Awk and head not identifying columns properlyawk 和 head 无法正确识别列
【发布时间】:2014-08-07 14:45:47
【问题描述】:

这是我想用来将 hist.txt 中的 3 列分隔为 2 个单独文件的代码,hist1.dat 包含第一列和第二列, hist2.dat 包含第一列和第三列。 hist.txt 中的列可以用多个空格分隔。我想在 histogram1.dat 和 histogram2.dat 中保存前 n 行,直到最后一个非零值。

脚本创建 histogram1.dat 正确,但 histogram2.dat 包含 hist2.dat 中的所有行。

hist.txt 就像: http://pastebin.com/JqgSKZrP

#!bin/bash

sed 's/\t/ /g' hist.txt | awk '{print $1 " " $2;}' > hist1.dat
sed 's/\t/ /g' hist.txt | awk '{print $1 " " $3;}' > hist2.dat

head -n $( awk 'BEGIN {last=1}; {if($2!=0) last=NR};END {print last}' hist1.dat) hist1.dat > histogram1.dat
head -n $( awk 'BEGIN {last=1}; {if($2!=0) last=NR};END {print last}' hist2.dat) hist2.dat > histogram2.dat

这个问题的原因是什么?可能是因为head 的一些特殊限制?

谢谢。

【问题讨论】:

  • FWIW,你的第一个 sed | awk 可以简化为awk '{print $1, $2}' hist.txt

标签: shell awk sed head


【解决方案1】:

对于您的第一个直方图,请尝试

awk '$2 ~ /000000/{exit}{print $1, $2}' hist.txt

第二次:

awk '$3 ~ /000000/{exit}{print $1, $3}' hist.txt

希望我理解正确...

【讨论】:

  • 最终我使用了: awk '{for(i = 1; i $dir/hist1.dat 然后我可以像 awk 'BEGIN {last=1}; { for(i = 2; i
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-16
  • 1970-01-01
  • 2013-07-06
  • 2021-05-13
  • 1970-01-01
  • 2020-05-03
  • 2019-02-24
相关资源
最近更新 更多