I am curious if there’s an easy solution for this seemingly complex task:
Say I have this file:
lineone
linetwo
linethree
linefour
lineone
linetwo
linethree
linefour
lineone
lineone
lineone
linetwo
linethree
linefour
Is there a simple way you can think of to get this result:
lineone[1]
linetwo[1]
linethree[1]
linefour[1]
lineone[2]
linetwo[2]
linethree[2]
linefour[2]
lineone[3]
lineone[4]
lineone[5]
linetwo[3]
linethree[3]
linefour[3]
To put it in words, is there a simple bash algorithm that would let me number each line in the order of it’s occurence in a file? I cannot figure out a way to do this without some complex solution involving additional storage, and the usual commands I use for line replacement are useless here. sed would just replace all occuring lines with the same value, and the string replace ( ${string/substring/replace} ) would not help me since it will not maintain the order of the lines.
Much appreciated.
The following
awkcommand works by keeping an array of lines and their counts.Example: