linux或Python提取一定范围的行
发布网友
发布时间:2022-04-22 09:43
我来回答
共3个回答
热心网友
时间:2023-10-08 22:21
usage Python
#!/usr/bin/env python
# coding: utf-8
"""
"""
import re
context = """
1 214321 454 4325
2 3214 43214 543
3 432 54325 43543
6 324 34 4324
5 43 324 4324
""" # (行号不连贯,并且有缺失)
rangedefine = """
1 2
3 5
"""
def iter_parser_context(strstream):
patt = re.compile(r"^(?P<sno>\d+)\s+(?P<content>.*)")
for ln in strstream:
matched = patt.match(ln)
if matched:
yield matched.groupdict()
def getrangedefine(context):
datarange = []
for ln in context.splitlines():
datarange.append(re.findall("\d+", ln))
return filter(None, datarange)
define = getrangedefine(rangedefine)
for data in iter_parser_context(context.splitlines()):
if any([minsno <= data["sno"] <= maxsno for (minsno, maxsno) in define ]):
print "%(sno)s %(content)s" % data
热心网友
时间:2023-10-08 22:21
#!/bin/bash
while read begin end
do
for ((i=$begin; i<=$end; i++))
do
grep "^$i\>" 文本文件
done
done <范围文件
热心网友
时间:2023-10-08 22:21
#!/bin/bash
while read beg end
do
awk '$1>='"$beg"' && $1<='"$end"'{print $0}' ~/data.txt
done < ~/linenum