Kalin Borisov
Kalin Borisov

Reputation: 1120

Order repeated values to the new row

Have values which the are repeated with different string, want them after every second column to be on new row.

Data:

10.1.1.192 wef.xml 10.1.1.156 fwe.xml 10.1.1.159 few.xml 10.1.1.159 vz.xml 10.1.1.159 vsd.xml 10.1.1.209 vsd.xml 10.1.1.216 vsd.xml 10.1.1.195 vsd.xml 10.1.1.195 vsd.xml 10.1.1.194 vsvds.xml 10.1.1.192 vsdh.xml 10.1.1.198 here.xml 10.1.1.200 herrrr.xml

Output which one to complete:

10.1.1.192 wef.xml
10.1.1.156 fwe.xml
10.1.1.159 few.xml
10.1.1.159 vz.xml
10.1.1.209 vsd.xml
and etc...

Upvotes: 1

Views: 70

Answers (3)

Kent
Kent

Reputation: 195169

xargs -n2

test:

kent$  xargs -n2 <<< "10.1.1.192 wef.xml 10.1.1.156 fwe.xml 10.1.1.159 few.xml 10.1.1.159 vz.xml 10.1.1.159 vsd.xml 10.1.1.209 vsd.xml 10.1.1.216 vsd.xml 10.1.1.195 vsd.xml 10.1.1.195 vsd.xml 10.1.1.194 vsvds.xml 10.1.1.192 vsdh.xml 10.1.1.198 here.xml 10.1.1.200 herrrr.xml"     
10.1.1.192 wef.xml
10.1.1.156 fwe.xml
10.1.1.159 few.xml
10.1.1.159 vz.xml
10.1.1.159 vsd.xml
10.1.1.209 vsd.xml
10.1.1.216 vsd.xml
10.1.1.195 vsd.xml
10.1.1.195 vsd.xml
10.1.1.194 vsvds.xml
10.1.1.192 vsdh.xml
10.1.1.198 here.xml
10.1.1.200 herrrr.xml

Upvotes: 7

glenn jackman
glenn jackman

Reputation: 246992

while read line; do 
    printf "%s %s\n" $line   # the variable is specifically unquoted here 
done <<END
10.1.1.192 wef.xml 10.1.1.156 fwe.xml 10.1.1.159 few.xml 10.1.1.159 vz.xml 10.1.1.159 vsd.xml 10.1.1.209 vsd.xml 10.1.1.216 vsd.xml 10.1.1.195 vsd.xml 10.1.1.195 vsd.xml 10.1.1.194 vsvds.xml 10.1.1.192 vsdh.xml 10.1.1.198 here.xml 10.1.1.200 herrrr.xml
END
10.1.1.192 wef.xml
10.1.1.156 fwe.xml
10.1.1.159 few.xml
...

Another approach is to transform spaces to newlines and then paste corresponding lines together:

tr " " "\n" <<< "$line" | paste - -

Upvotes: 1

choroba
choroba

Reputation: 241948

Bash solution:

read -a ar < input
for ((i=0 ; i<${#ar[@]} ; i+=2)) ; do
    echo ${ar[i]} ${ar[i+1]}
done

It just reads the whole line into an array, then prints the pairs in a loop.

Sed solution:

sed -e 's/\([^ ]\+ [^ ]\+\) /\1\n/g' < input

It replaves a space by a newline, if there is still one space before it.

Perl one-liner:

perl -ne 'print $_, ($ff = ! $ff) ? " " : "\n" for split' < input

It uses $ff as a flip-flop.

Upvotes: 2

Related Questions