Bash的第一个元素列表识别 [英] Bash First Element in List Recognition
问题描述
我很新打坏,所以我很抱歉,如果这个问题其实很简单。我处理包含由2计数数2-32的许多垂直列表的文本文件,每个数字都有它后面行等文字。的问题是,一些清单的是丢失号码。任何指针为code,可以通过和检查,看看是否每个号码是存在的,如果没有添加一行,放在数。
I'm very new to Bash so I'm sorry if this question is actually very simple. I am dealing with a text file that contains many vertical lists of numbers 2-32 counting up by 2, and each number has a line of other text following it. The problem is that some of the lists are missing numbers. Any pointers for a code that could go through and check to see if each number is there, and if not add a line and put the number in.
一个列表看起来像:
2 djhfbadsljfhdsalkfjads;lfkjs
4 dfhadslkfjhasdlkfjhdsalfkjsahf
6 dsa;fghds;lfhsdalfkjhds;fjdsklj
8 daflgkdsakfjhasdlkjhfasdjkhf
12 dlsagflakdjshgflksdhflksdahfl
到32一路我怎么会在这种情况下,使它所以10被认为是失踪,然后加在12的上方?谢谢!
All the way down to 32. How would I in this case make it so the 10 is recognized as missing and then added in above the 12? Thanks!
推荐答案
这里有一个 AWK
为基础的解决方案(格式化的可读性,不一定是你将如何键入):
Here's one awk
-based solution (formatted for readability, not necessarily how you would type it):
awk ' { value[0 + $1] = $2 }
END { for (i = 2; i < 34; i+=2)
print i, value[i]
}' input.txt
这基本上只是记录在一个键/值对(关联数组)现有生产线,然后在最后,打印所有你关心的记录,与先前保存的(可能为空)值一起。
It basically just records the existing lines in a key/value pair (associative array), then at the end, prints all the records you care about, along with the (possibly empty) value saved earlier.
注意:如果第一列需要被看作是一个字符串而不是一个整数,此变体应该工作:
Note: if the first column needs to be seen as a string instead of an integer, this variant should work:
awk ' { value[$1] = $2 }
END { for (i = 2; i < 34; i+=2)
print i, value[i ""]
}' input.txt
这篇关于Bash的第一个元素列表识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!