使用awk或sed从文件中删除连续重复的单词 [英] Remove consecutive duplicate words from a file using awk or sed

查看:246
本文介绍了使用awk或sed从文件中删除连续重复的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的输入文件如下所示:

My input file looks like below:

"true true, rohith Rohith;
cold burn, and fact and fact good good?"

输出应该像这样:

"true, rohith Rohith;
cold burn, and fact and fact good?"

我正在用awk尝试相同的操作,但无法获得所需的结果.

i am trying the same with awk, but couldn't able to get the desired result.

awk '{for (i=1;i<=NF;i++) if (!a[$i]++) printf("%s ",$i,FS)}{printf("\n")}' input.txt

有人可以在这里帮我吗?

Could someone please help me here.

关于, 罗伊斯(Rohith)

Regards, Rohith

推荐答案

使用GNU awk作为第四个参数split():

With GNU awk for the 4th arg to split():

$ cat tst.awk
{
    n = split($0,words,/[^[:alpha:]]+/,seps)
    prev = ""
    for (i=1; i<=n; i++) {
        word = words[i]
        if (word != prev) {
            printf "%s%s", seps[i-1], word
        }
        prev = word
    }
    print ""
}

$ awk -f tst.awk file
"true, rohith Rohith;
cold burn, and fact and fact good?"

这篇关于使用awk或sed从文件中删除连续重复的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆