Remove consecutive duplicate words from a file using awk or sed

前端 未结 6 1406
不思量自难忘°
不思量自难忘° 2021-01-16 16:53

My input file looks like below:

“true true, rohith Rohith;
cold burn, and fact and fact good good?”

Output shoud look like:



        
6条回答
  •  耶瑟儿~
    2021-01-16 17:34

    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?”
    

提交回复
热议问题