LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Find line in text file where variable changes (https://www.linuxquestions.org/questions/linux-newbie-8/find-line-in-text-file-where-variable-changes-4175546695/)

AntBla 06-29-2015 01:22 AM

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

veerain 06-29-2015 02:14 AM

A awk script instead of sed would solve this.

pan64 06-29-2015 04:26 AM

yes, you can use awk/perl/python or more or less anything but grep and sed. which one do you prefer?

AntBla 06-29-2015 05:28 AM

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.

pan64 06-29-2015 05:33 AM

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....

HMW 06-29-2015 06:15 AM

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

"""
http://www.linuxquestions.org/questions/linux-newbie-8/find-line-in-text-file-where-variable-changes-4175546695/
"""

tmpNum = ""
lineNum = 0

with open("pyline.txt") as afile: # Open the file
    for line in afile: # Read in a loop line by line
        lineNum += 1 # Increment lineNum for each loop
        splitList = line.split("/") # Split each line (on the '/' char) into a list
        if tmpNum != splitList[1]: # Compare tmpNum to 2nd field ([01], [02], [03] etc...) in list...
            tmpNum = splitList[1] # if it's not equal to previous print line number
            print("{0:s} starts at line {1:d}".format(tmpNum, lineNum))

Output:
Code:

./pyline.py
01 starts at line 1
02 starts at line 6
03 starts at line 11
04 starts at line 14
05 starts at line 18



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

"""
http://www.linuxquestions.org/questions/linux-newbie-8/find-line-in-text-file-where-variable-changes-4175546695/
"""

tmpNum = ""
lineNum = 0

with open("pyline.txt") as afile: # Open the file
    for line in afile: # Read in a loop line by line
        lineNum += 1 # Increment lineNum for each loop
        splitList = line.split("/") # Split each line into a list
        if tmpNum != splitList[1]: # Compare tmpNum to 2nd field in list...
            tmpNum = splitList[1] # if it's not equal to previous print line number
            print tmpNum + " starts at line " + str(lineNum)



All times are GMT -5. The time now is 06:54 PM.