using awk:
awk -F| '{ if (map[NF-1]!="") { map[NF-1]=map[NF-1]","NR } else { map[NF-1]=NR } } END { for (i in map) { printf "lines %s have %s occurances of |
",map[i],i } }' file
Explanation:
awk -F| '{ # Set the field delimiter to |
if (map[NF-1]!="") {
map[NF-1]=map[NF-1]","NR # Create an array called map with the number of | occurrences (NF-1) as the index and line number (NR) as the value
}
else {
map[NF-1]=NR # We don't want to prefix a comma if this is the first entry in the array
}
map1[NF-1]++
}
END {
for (i in map) {
printf "line(s) %s have %s occurrence(s) of |
",map[i],i # At the end, print the contents of the array in the format required.
}
for (i in map1) {
printf "%s line(s) have %s occurrence(s) of |, ",map1[i],i
}
printf "
"
}' file
Output:
line(s) 4 have 1 occurrence(s) of |
line(s) 3 have 2 occurrence(s) of |
line(s) 1,2,5,6 have 3 occurrence(s) of |
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…