LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 11-18-2013, 04:38 AM   #1
AntBla
LQ Newbie
 
Registered: Oct 2012
Posts: 16

Rep: Reputation: Disabled
Search text file and find first line that matches an existing directory name


I have a text file that contains a list of directory names
eg: list.txt
00010
00020
00030
00031
abcd

I want to be able to search through the file and set a variable the first time a match is made with a real directory.

So if my local directory listing is as follows
00009
00012
00030
00031
abcd

The variable $FOLDER would be set as 00030 as this is the first match with the file and the line number variable would be set to $LINE

I then want to use sed to remove $LINE from the text file. I know how to do this part.

Lastly, the text file may contain up to 300 lines and the real directory listing may contain 15000 sub directories.

Any help/clues would be greatly appreciated.

Ant.
 
Old 11-18-2013, 04:50 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
You can try something like this:
Code:
FOLDER=$(ls -d * | grep -f list.txt -m1)
The -m1 option of grep stops the execution after the first match found.
 
Old 11-18-2013, 05:21 AM   #3
AntBla
LQ Newbie
 
Registered: Oct 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Thanks for your quick reply.

Your suggestion is returning the first directory in my directory list, not the first match in the list.txt file.

This is the contents of my test script
#!/bin/bash
FOLDER=$(ls -d /usr/local/bin/test/* | grep -f /usr/local/bin/list.txt -m1)
echo $FOLDER

My /usr/local/bin/test/ directory has 10000 sub directories numbered 00001 to 10000. My /usr/local/bin/list.txt file has 300 lines with line 100 the only line that matches a directory in the listing ie: 05000

The result I get is 00001 not 05000
If I delete directory 00001, then run again, the result is 00002.

Also, if you would be so kind, how can I find the line number of this occurrence ie:
$FOLDER=05000
$LINE=100

TIA.
Ant.
 
Old 11-19-2013, 06:27 AM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,714

Rep: Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280
There could be two problems...


1. ls sorts the list...
2. your input file may not be sorted...

That makes for a slow search. But something like

Code:
ls -d | while read V; do grep $V file && echo $V && break; done
might work. The echo should only occur if the grep successfully locates an entry.

Now you might need to use "grep -E" instead - this would be because it is possible for two
files to match (00005 and 0005 for instance). Using that I think the pattern would be "^${V}\$" which would require the match to go from the beginning of the line to the end of the line.

Last edited by jpollard; 11-19-2013 at 06:31 AM. Reason: forgot the break...
 
  


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
[SOLVED] Search two text documents, eliminate matches, print third document with unmatched+. ramma Programming 15 07-12-2012 01:09 PM
search folders, find a line in a file, and replace it with a new line ithirdeye Programming 2 04-07-2012 05:27 PM
[SOLVED] echo text to a specific line in an existing file Jammyzx Programming 6 10-12-2011 01:17 AM
[SOLVED] Find Text After String Search in Text File redir Linux - Newbie 12 08-02-2011 04:57 PM
trying to search and replace text file for single & multiple line breaks separately brokenfeet Programming 7 08-29-2003 02:56 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:26 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