Home > Software design >  File Create Unix
File Create Unix

Time:06-29

I have this data set ( txt) of a undirected graph. Each line corresponds to a single edge, with two columns showing the IDs of the two corresponding nodes, such that for a directed edge a→b we have a in the first column and b in the second column. For example:

input:
0 275,276,277,278,279,280,281,282,283
1 442,22,455,0,456,457,458
75 62,263,264,265,266,267
80 0,516,294,517,518,519

I would like as output

output:
0 275
0 276
0 277
...
1 442
1 22
...
80 454

I use unix terminal. Let me know if you have some ideas. Thanks

CodePudding user response:

Ignoring the last part you mentioned "80 454", I found a solution to print as required.

Suppose all these values are stored in a file names "stack.txt", the following bash code will be useful.

#!/bin/bash

while read i;do
        f=$(awk -F" " '{print $1}' <<< $i)
        line=$(cut -d" " -f2 <<<$i)
        for m in $(echo $line | sed "s/,/ /g"); do
                echo $f" "$m
        done
        echo "..."
done<stack.txt

Output will be

0 275
0 276
0 277
0 278
0 279
0 280
0 281
0 282
0 283
...
1 442
1 22
1 455
1 0
1 456
1 457
1 458
...
75 62
75 263
75 264
75 265
75 266
75 267
...
80 0
80 516
80 294
80 517
80 518
80 519
...

CodePudding user response:

Using a perl one-liner:

perl -lane 'print "$F[0] $_" for split /,/, $F[1]' input.txt
  • Related