AWK/sed - 在低于特定值的数字之前在文本文件中写入文本
AWK/sed - write a text in a text file before number which is lower than specific value
我有一个文本文件。在这个文本文件中,我有从最低到最高的数字。
输入示例
[ 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
我有一个文本文件。在这个文本文件中,我有从最低到最高的数字。 输入示例
[ 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