答案1
愿awk
与你同在
function splitAndPrint(lineId, line, separator) {
split(line, arr, separator)
for (v in arr){
print lineId" "arr[v]
}
}
{
if ($0 ~ /^[0-9] /) { # matches the digit before space
digitBeforeSpace = $1
sub(/^[0-9] +/, "") # removes that digit
}
if($0 ~ /,/){
# separator is ,
splitAndPrint(digitBeforeSpace, $0, ",")
}
else {
# separator is space
splitAndPrint(digitBeforeSpace, $0, " ")
}
}
答案2
使用sed
$ cat input_file
0 1,5,6,4,2,23,43,5,6
1 4,5,2,3
5 4,5,6,7,56,65
$ sed ':a;s/\([^ ]*\) \+\([^,]*\),/\1 \2\n\1 /;ta' input_file
0 1
0 5
0 6
0 4
0 2
0 23
0 43
0 5
0 6
1 4
1 5
1 2
1 3
5 4
5 5
5 6
5 7
5 56
5 65