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'
Technology Partners: 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!


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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Optimize bash script 20 484
Package is intended for a different operating system on doing yum install an rpm 4 861
linux / python expert needed 3 111
mounting drive a in a linux 14 63
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…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below.…
Suggested Courses

739 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