AWK/sed-在低于特定值的数字之前在文本文件中写入文本
我有一个文本文件。在这个文本文件中,我有从最低到最高的数字。 输入示例AWK/sed-在低于特定值的数字之前在文本文件中写入文本,awk,sed,text-processing,Awk,Sed,Text Processing,我有一个文本文件。在这个文本文件中,我有从最低到最高的数字。 输入示例 [ 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 1
[ 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
有了你们展示的样品,你们能试一下下面的吗
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
很好用,非常感谢。你有很好的编程技巧,非常感谢。你有很好的编程技巧。