I want to find last value (ie. lo:) and just append a value to this.
ie.
i want to find the lo:3, in this case my last value and append a lo:4
File:
ifconfig lo:1
address x.x.x.x
netmask x.x.x.x
gateway x.x.x.x
ifconfig lo:2
address x.x.x.x
netmask x.x.x.x
gateway x.x.x.x
ifconfig lo:3
address x.x.x.x
netmask x.x.x.x
gateway x.x.x.x
What is the best way to do ? Split or Loop + Readline with check.
Without assuming too much on the structure of the file, you have to parse the file and take track of every “ifconfig lo:” line, e.g updating a running maximum. In this case:
Once you have seen it all, you will know the maximum value and will be able to compute the next:
and now you are ready to append the data to your file.
Obviously, if the file has more known in advance structure it’s easier (eg you know that the file is sorted: you don’t need to compute the maximum but it simply suffices to find the last value).