Find line in text file where variable changes
I have a files that is a listing of folders like below.
I am looking for a way to find the line number when the top level folder name changes. Eg: When ./02 changes forward to ./03 it is line 11 When ./02 changes backwards to /.01 it is line 5 I got as far as using this sed command to extract the folder |sed 's:/[^/]*$::' BUT I am stuck on the next step. Any help would be greatly appreciated. Thanks. Ant. ./01/100.mp3 ./01/101.mp3 ./01/102.mp3 ./01/103.mp3 ./01/104.mp3 ./02/105.mp3 ./02/106.mp3 ./02/107.mp3 ./02/108.mp3 ./02/109.mp3 ./03/110.mp3 ./03/111.mp3 ./03/112.mp3 ./04/113.mp3 ./04/114.mp3 ./04/115.mp3 ./04/116.mp3 ./05/117.mp3 ./05/118.mp3 ./05/119.mp3 |
A awk script instead of sed would solve this.
|
yes, you can use awk/perl/python or more or less anything but grep and sed. which one do you prefer?
|
Dear Pan64.
Thanks for your reply. As a newbie, I did not understand that I couldn't use sed. I guess I would like to use awk as it is mentioned in both replies. Would I also use awk for the first part that I already did in sed? I am pretty sure I could stitch together the script if you would be kind enough to give me a few pointer. TIA. A. |
sed is not really useful if you want to compare lines to each other. Not impossible to solve it (I guess), but not really simple too.
From my side awk (or perl) would be practical (yes, without sed, without that first part you already made), but if you were not familiar with them.... |
Here is how I would do this in Python(3). I am sure there are tons of other ways - that are more efficient as well - but this seems to do the trick:
Code:
#!/usr/bin/env python3 Code:
./pyline.py Best regards. EDIT: If you don't have (or don't want to install) Python 3, use this (I just changed the print part): Code:
#!/usr/bin/env python |
All times are GMT -5. The time now is 06:54 PM. |