python - 跳转到txt文件中的特定行

标签 python linux

我有一个大文本文件,我想打开它并跳转到包含特定字符串的行。 我设法做的是跳转到特定的行号。

import os
os.system('sudo gedit +50 test.txt')

我如何编辑代码以使其搜索特定字符串而不是行号?

最佳答案

您可以调用该行作为您在下一行查找的文本的第一次出现:

   gedit +$(grep -n "id" test.txt  | head -n 1 | cut -f 1 -d ':')  test.txt

这个 grep -n "text_to_find"test.txt |头-n 1 | cut -f 1 -d ':' 意思是:

  1. grep ... 告诉我所有有“test_to_find”和行号前缀的行
  2. head ...:获取第一次出现
  3. cut ...获取行号

如果没有发生,你必须修复它

关于python - 跳转到txt文件中的特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34182249/

相关文章:

linux - find命令+根据目录时间戳删除旧目录

c - 在 Linux 中从 C 杀死所有进程

python - 我如何快速洗牌 Pandas 系列

python - python中的[...]是什么意思?

linux - docker-engine 运行级别 :/var/run/utmp: No such file or directory

linux - 如何升级 pg_dumpall

linux - 可以使用 Perl 传输压缩数据流

python - 在 Mac OS X 10.9 上使用 pip 安装 Python 图像库时出错

python - 如何使用 Python Requests 库在 post 请求中发送 cookie?

python - 如何在 Pandas 的每一行中插入?