linux 系统中输出匹配特定字符至末尾的行

 

001、

root@PC1:/home/test2# ls
a.txt
root@PC1:/home/test2# cat a.txt
aa dd ss
dd ff xv
ef 33 cc
xx ee ww
df ff zc
xx xx ff
er ed ww
xx xx ee
er uy vv
root@PC1:/home/test2# sed -n '/xx/,$p' a.txt    ## 输出匹配xx至末尾的行
xx ee ww
df ff zc
xx xx ff
er ed ww
xx xx ee
er uy vv

 

002、

root@PC1:/home/test2# ls
a.txt
root@PC1:/home/test2# cat a.txt
aa dd ss
dd ff xv
ef 33 cc
xx ee ww
df ff zc
xx xx ff
er ed ww
xx xx ee
er uy vv
root@PC1:/home/test2# sed -n '/ww$/, $p' a.txt   ## 输出以ww结尾至文本末尾的数据
xx ee ww
df ff zc
xx xx ff
er ed ww
xx xx ee
er uy vv

 

003、

root@PC1:/home/test2# ls
a.txt
root@PC1:/home/test2# cat a.txt
aa dd ss
dd ff xv
ef 33 cc
xx ee ww
df ff zc
xx xx ff
er ed ww
xx xx ee
er uy vv
root@PC1:/home/test2# awk 'BEGIN{idx = 0} {if(idx > 0 || $0 ~ /xx/) {idx++; print $0}}' a.txt    ## 输出匹配xx 至末尾的数据
xx ee ww
df ff zc
xx xx ff
er ed ww
xx xx ee
er uy vv
root@PC1:/home/test2# awk 'BEGIN{idx = 0} {if(idx > 0 || $2 == "xx" && $3 == "ee") {idx++; print $0}}' a.txt ## 输出第2列为xx 同时第三列为ee至末尾的数据
xx xx ee
er uy vv

 

004、python实现

01、

root@PC1:/home/test2# ls
a.txt  test.py
root@PC1:/home/test2# cat a.txt
aa dd ss
dd ff xv
ef 33 cc
xx ee ww
df ff zc
xx xx ff
er ed ww
xx xx ee
er uy vv
root@PC1:/home/test2# cat test.py        ## 匹配包含xx的行至文本末尾的数据
#!/usr/bin/python

in_file = open("a.txt", "r")
out_file = open("result.txt", "w")

lines = in_file.readlines()

idx = 0
for i in lines:
    if "xx" in i or idx > 0:
        idx = idx + 1
        out_file.write(i)

out_file.close()
root@PC1:/home/test2# python test.py
root@PC1:/home/test2# ls
a.txt  result.txt  test.py
root@PC1:/home/test2# cat result.txt
xx ee ww
df ff zc
xx xx ff
er ed ww
xx xx ee
er uy vv

 

02、

root@PC1:/home/test2# ls
a.txt  test.py
root@PC1:/home/test2# cat a.txt
aa dd ss
dd ff xv
ef 33 cc
xx ee ww
df ff zc
xx xx ff
er ed ww
xx xx ee
er uy vv
root@PC1:/home/test2# cat test.py    ## 匹配以xx开头、ff结尾的行至文本末尾的数据
#!/usr/bin/python

in_file = open("a.txt", "r")
out_file = open("result.txt", "w")

lines = in_file.readlines()

idx = 0
for i in lines:
    if i.startswith("xx") and i.endswith("ff\n") or idx > 0:
        out_file.write(i)
        idx = idx + 1
out_file.close()
root@PC1:/home/test2# python test.py
root@PC1:/home/test2# ls
a.txt  result.txt  test.py
root@PC1:/home/test2# cat result.txt   ## 结果
xx xx ff
er ed ww
xx xx ee
er uy vv

 

03、

root@PC1:/home/test2# ls
a.txt  test.py
root@PC1:/home/test2# cat a.txt
aa dd ss
dd ff xv
ef 33 cc
xx ee ww
df ff zc
xx xx ff
er ed ww
xx xx ee
er uy vv
root@PC1:/home/test2# cat test.py   ## 提取以xx开头,同时以ff结尾的数据至文本末尾的数据
#!/usr/bin/python
in_file = open("a.txt", "r")
out_file = open("result.txt", "w")
lines = in_file.readlines()

idx = 0
for i in lines:
    i = i.split()
    if i[0] == "xx" and i[2] == "ff" or idx > 0:
        out_file.write(" ".join(i))
        out_file.write("\n")
        idx = idx + 1

in_file.close()
out_file.close()
root@PC1:/home/test2# python test.py
root@PC1:/home/test2# ls
a.txt  result.txt  test.py
root@PC1:/home/test2# cat result.txt
xx xx ff
er ed ww
xx xx ee
er uy vv

 

posted @ 2022-08-02 23:21  小鲨鱼2018  阅读(624)  评论(0编辑  收藏  举报