Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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

Posted on 2006-11-21
Medium Priority
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'
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.


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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

610 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