Aar*_*n_H 1 python bash shell awk sed
我有一个(制表符分隔的)文件,其中每行的第一个“单词”是行号。但是,缺少一些行号。我想插入新行(带有相应的行号),以便在整个文件中,打印在行上的数字与实际行号相匹配。(这是为了稍后使用 cut/awk 将其消耗到 readarray 中以获取行号之后的行。)
我已经在 python 中编写了这个逻辑并测试了它的工作原理,但是我需要在没有 python 的环境中运行它。实际文件大约有 10M 行。有没有办法使用 sed、awk 甚至只是普通的 shell/bash 来表示这种逻辑?
linenumre = re.compile(r"^\d+")
i = 0
for line in sys.stdin:
i = i + 1
linenum = int(linenumre.findall(line)[0])
while (i < linenum):
print(i)
i = i + 1
print(line, end='')
Run Code Online (Sandbox Code Playgroud)
测试文件看起来像:
1 foo 1
2 bar 1
4 qux 1
6 quux 1
9 2
10 fun 2
Run Code Online (Sandbox Code Playgroud)
预期输出如:
1 foo 1
2 bar 1
3
4 qux 1
5
6 quux 1
7
8
9 2
10 fun 2
Run Code Online (Sandbox Code Playgroud)
像这样,与awk:
awk '{while(++ln!=$1){print ln}}1' input.txt
Run Code Online (Sandbox Code Playgroud)
解释,作为一个多行脚本:
{
# Loop as long as the variable ln (line number)
# is not equal to the first column and insert blank
# lines.
# Note: awk will auto-initialize an integer variable
# with 0 upon its first usage
while(++ln!=$1) {
print ln
}
}
1 # this always expands to true, making awk print the input lines
Run Code Online (Sandbox Code Playgroud)