linux - 在文本中找到特殊 block ,使用 shell 脚本

标签 linux shell

我必须获取特定版本(例如 v2.0.1 或 v2.0.2 等)的特殊行

例如,对于输入“v2.0.2”,我想要以下输出:

==2012-01-02 v2.0.2

some information about B..

这是我的文本文件 sample.txt:

==2012-01-01 v2.0.1

some information about A..

==2012-01-02 v2.0.2

some information about B..

==2012-01-03 v2.0.4

some information about C..

最佳答案

awk -v s="$ver" 'BEGIN{gsub(/\./,"[.]",s)}$0~s{p=7;print;next}p&&/^==/{p=0}p' file

$ver 是一个 shell 变量,包含搜索字符串。

BEGIN{} block 用于转义 。 (点),因为稍后我们会将其用作动态正则表达式。

关于linux - 在文本中找到特殊 block ,使用 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25263203/

相关文章:

linux - 如何grep多个匹配一行?

linux - 系统重启/关机时未运行初始化脚本

linux - 设置变量以供稍后在案例构造中使用

c++ - Ubuntu Linux 的编译问题

linux - 在生产环境中永远运行 Spark Streaming

linux - 如何在 Ubuntu 中启动 Thunderbird 并最小化启动窗口

PHP exec() 后台进程的返回值 (linux)

linux - 用于在 AWS s3 中部署的 Shell 脚本

python - 在 Docker 中运行多个 python 进程

linux - 如何使用 linux 命令显示文件中每一行的第一个单词?