Almost-as-easy:  How to test for the existence of a filename within a list.

Posted on 2006-11-21
Last Modified: 2010-05-18
Almost as easy, but still very easy (i think) --

This is the same example i gave from "Easy-Peasy", but there's an aspect that i still need answered:

: > FileList
find ./ -name XXX.YYY |
while read "b"
        c=`dirname "$b"`
        d=`sed -n '/"$c"/p' FileList`
        if [ "$d" = "$c" ]
                        echo "$c" >> List

What i'm interested in is taking that sed command, that defines the "d", and then testing to see if it exists within a given file (or stream).    I then want to build case- or if-loops off of that.

How do i do it?  I was under the impression here that sed'd be printing any line that already had the $c pattern in it, and then the script would continue;  whereas if the line didn't exist within the FileList then it'd get echoed into the list and continue.

Script didn't work out that way, though.  So i also tried this:

if [ "$d" = "" ]
            echo "$c" >> List

So i'm stymied;  how do i work out this "test" command?
Question by:kyle_in_taiwan
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2

Author Comment

ID: 17987763
And, oh, yeah -- a typo's been introduced since i transferred it to these boards.  The second "List" originally read "FileList".  That's just an editing mistake.  Sorry.
LVL 51

Expert Comment

ID: 17988084
> d=`sed -n '/"$c"/p' FileList`
i guess you want to write

  d=`sed -n /"$c"/p FileList`

Author Comment

ID: 17988682
Hm.  I tried that, and got this error (for each line in the first set of directories):

sed: -e expression #1, char 5: extra characters after command

And that then turned to this (which has a couple of characters truncated from the beginning):

sed: can't find label for jump to `QAnnReturn/p'
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.


Author Comment

ID: 17988691
My understanding is that the regex portions of sed scripts need to be escaped with single quotes so they can be properly interpreted by the shell;  but perhaps there are different ways of finagling the shell profile?  

LVL 48

Accepted Solution

Tintin earned 500 total points
ID: 17990092
I assume this is just a learning exercise as I can make any sense of why you'd want to do this (whatever it maybe achieving) with this script.

Part of the problem is the sed delimiter

Say you have XXX.YYY sitting in a subdirectory foobar

find ./ -name XXX.YYY

will output


doing a dirname on that will result in


which when you use in sed, comes out as

sed /./foobar/p

You need to change the sed delimiter to not clash with /'s, eg:

sed "#$c#p"

However, why use this script (unless it's purely for learning), when you simply could do

find ./ -name XXX.YYY -exec dirname {} >>FileList \;

LVL 51

Expert Comment

ID: 17993909
> sed: -e expression #1, char 5: extra characters after command
sounds like your $c contains spaces or other meta characters, try

d=`sed -n '/'"$c"'/p' FileList`

Author Comment

ID: 17994758

Yeah, i already said on another thread that this is a learning exercise;  i'm *very* new to playing around with this stuff.

Thanks for the heads-up on the find command; you're right, and i'm slapping myself in the head for not thinking about it before (never having used the -exec flag before, it's a light slap).

Thanks both.  I'll get back to this later this evening.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question