AWK / sed-在小于特定值的数字之前在文本文件中写入文本

问题描述

我有一个文本文件。在此文本文件中,我有从最低到最高的数字。 输入示例

[  Index 1  ]
1628 5704
32801 61605
71508 90612
102606

我想将此文件分为两部分,第一部分中的数字在1到58050之间,第二部分中的数字在58051 116100之间,因此当我的脚本找到一个大于58050的数字时,该程序将写[索引2]

预期产量

[  Index 1  ]
1628 5704
32801 
[  Index 2  ]
61605
71508 90612
102606

你有什么主意吗?

解决方法

使用显示的示例,请您尝试尝试以下操作。

awk '
/^\[/{ next }
{
  for(i=1;i<=NF;i++){
    if($i>=1 && $i<=58050){
      tempfirstGroup=(tempfirstGroup?tempfirstGroup OFS:"")$i
    }
    if($i>=58051 && $i<=116100){
      tempsecondGroup=(tempsecondGroup?tempsecondGroup OFS:"")$i
    }
  }
  if(tempfirstGroup){
      firstGroup=(firstGroup?firstGroup ORS:"")tempfirstGroup
  }
  if(tempsecondGroup){
      secondGroup=(secondGroup?secondGroup ORS:"") tempsecondGroup
  }
  tempsecondGroup=tempfirstGroup=""
}
END{
  print "[  Index 1  ]" ORS firstGroup ORS "[  Index 2  ]" ORS secondGroup
}
' Input_file

输出如下。

[  Index 1  ]
1628 5704
32801
[  Index 2  ]
61605
71508 90612
102606