LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-29-2015, 02:22 AM   #1
AntBla
LQ Newbie
 
Registered: Oct 2012
Posts: 16

Rep: Reputation: Disabled
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
 
Old 06-29-2015, 03:14 AM   #2
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
A awk script instead of sed would solve this.
 
Old 06-29-2015, 05:26 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
yes, you can use awk/perl/python or more or less anything but grep and sed. which one do you prefer?
 
Old 06-29-2015, 06:28 AM   #4
AntBla
LQ Newbie
 
Registered: Oct 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
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.
 
Old 06-29-2015, 06:33 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
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....
 
Old 06-29-2015, 07:15 AM   #6
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Xubuntu, Manjaro
Posts: 745
Blog Entries: 3

Rep: Reputation: 347Reputation: 347Reputation: 347Reputation: 347
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)

Last edited by HMW; 06-29-2015 at 07:26 AM.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Search text file and find first line that matches an existing directory name AntBla Linux - Newbie 3 11-19-2013 06:27 AM
[SOLVED] Using a variable in sed to pick a specific line from a text file, bash Sunvic Linux - Newbie 10 08-12-2012 12:03 PM
PHP: Find a string in a text file and add something to that line aocferreira Programming 1 05-26-2011 02:52 PM
Python: find defined text string in a file, and replace the whole line Dark Carnival Programming 6 05-22-2007 07:02 AM
find a Nth line in text file mahabooba Programming 2 07-25-2006 12:56 AM


All times are GMT -5. The time now is 10:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration