awk - 在文件中查找以下部分重复的行
问题描述
使用 shell 命令,打印包含前一行的文件的任何行。
示例文件:
i love potatoes
i love shoes
i love super shoes
i love shoe
i love shoes
命令必须打印:“i love shoes”因为它是唯一包含前一行内容的行(因为“i love shoes”包含“i love shoe”)
任何想法 ?
解决方案
输入:
$ cat input
i love potatoes
i love shoes
i love super shoes
i love apple
i love apples
i eat melon
i eat melons
i eat apples and i eat melons
命令:
awk '{if(NR>1 && length(previous)>0 && index($0,previous)>0){print};previous=$0;}' input
输出:
i love apples
i eat melons
i eat apples and i eat melons
说明:
{
#skip the first line as by definition there is no line before it
#if the previous line is not empty and if the current line contains the previous line
# (e.g. you can find an index >0 of the previous line string in the current line),
#print the current line
if (NR>1 && length(previous) > 0 && index($0, previous) > 0) {
print $0
}
#assign the current line to previous line and continue the processing
previous = $0
}
推荐阅读
- javascript - 在 react 和 JavaScript 中获取正确格式的数据
- javascript - 在 MS Edge 中检测覆盖模式
- julia - 在 Julia/DifferentialEquations 中使用 VectorContinuousCallback 时输出解中事件的索引
- .net - Zip 文件的文件类型在复制到 AWS S3 时发生变化
- android - 使用 adb 和活动管理器在 Android 上捕获具有指定名称的 JPEG 图像
- spring - 为每个端点设置 spring.security.oauth2.resourceserver.jwt.jwk-set-uri
- flutter - 是否可以在 Flutter 的函数中将类作为参数传递?
- laravel - 如何将多个关联数组条目发布到邮递员以及如何在 laravel 控制器中访问它
- java - Springboot JPA 将列连接为实体
- android - 异常的 WRAP_CONTENT 行为