P
P
Printip2021-04-29 14:00:47
linux
Printip, 2021-04-29 14:00:47

How to remove duplicates within a string?

Hello! There is a data.txt file data.txt

content:

001 002 001 00003
007 008 007 0000009

how to remove repetitions so that the output is:

001 002 00003
007 008 0000009

Answer the question

In order to leave comments, you need to log in

3 answer(s)
3
3vi1_0n3, 2021-04-29
@Printip

#!/bin/bash
IFS='
'
exec 3<>data.txt
while read -u 3 line
do
(
        IFS=' '
        result=()
        for word in $line
        do
                 && result=(${result[*]} $word)
        done
        echo ${result[*]}
)
done

S
Saboteur, 2021-04-29
@saboteur_kiev

If the order in the string is not important, then you can simply

while read;do echo $REPLY|xargs -n1|sort -u|xargs; done<data.txt

L
Linuxoid, 2021-05-12
@orahorn

those. it turns out that with the -u option of the sort command, it is not required to pass the data stream through the uniq command?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question