Hi,
I'm not really sure what you're trying to do, but I *think *this will give
you your results easily:
for fileName in $( find . -name "google_transit.zip" )
do
echo " <bean
class=\"org.opentripplanner.graph_builder.model.GtfsBundle\">"
echo " <property name=\"path\" value=\"
${fileName} \" />"
echo " <property name=\"defaultAgencyId\"
value=\"${fileName%google_transit.zip} \" />"
echo " </bean>"
done
The % will strip the shortest match of the following text from the end of
the variable. so this:
/path/to/google_transit.zip
becomes
/path/to/
If you're just trying to get the directory name it is sitting in, then I'd
do something like this:
for fileName in $( find . -name "google_transit.zip" )
do
agencyString=$( dirname ${fileName} )
agencyString=${agencyString##*/}
echo " <bean
class=\"org.opentripplanner.graph_builder.model.GtfsBundle\">"
echo " <property name=\"path\" value=\"
${fileName} \" />"
echo " <property name=\"defaultAgencyId\"
value=\"${agencyString} \" />"
echo " </bean>"
done
The ## strips the longest match from the front of the string, so what I've
done is wildcard "remove everything up to the last /"
Also, if you are building XML you might consider using printf or \t to get
your tabbing to be consistent and readable. So this is an easy example
with echo:
for fileName in $(find . -name "google_transit.zip" )
do
agencyString=$( dirname ${fileName} )
agencyString=${agencyString##*/}
echo -e "\t<bean
class=\"org.opentripplanner.graph_builder.model.GtfsBundle\">"
echo -e "\t\t<property name=\"path\" value=\"${fileName}\" />"
echo -e "\t\t<property name=\"defaultAgencyId\" value=\"${agencyString}
\" />"
echo -e "\t</bean>"
done
A good page for beginners in bash scripting is here:
http://tldp.org/LDP/abs/html/
On Friday, January 25, 2013 7:22:46 AM UTC-5, Andrew Taylor wrote:
>
> Yes that's right, thanks for the advice.
>
> I'll look into that command now
>
> On Friday, 25 January 2013 10:35:27 UTC, tid wrote:
>>
>> have a look at the 'dirname' command - you can extract a file's
>> directory name with that. In your example
>> below you look as if you're trying to extract the directory two levels
>> above your file - is that what you want?
>>
>> Tid
>>
>> On 25 January 2013 03:34, Andrew Taylor <[email protected]> wrote:
>> > Thank you so much for your help, it seems so obvious to see it written
>> > down!.
>> >
>> > Could you help with one other thing? I'm trying to extract the
>> directory
>> > name to form a description for each path returned. This is what I have
>> so
>> > far:
>> >
>> > for OUTPUT in $(find `pwd` -name google_transit.zip)
>> > do
>> > while IFS='/' read -ra $OUTPUT; do
>> > echo " <bean
>> > class=\"org.opentripplanner.graph_builder.model.GtfsBundle\">"
>> > echo " <property name=\"path\"
>> value=\""$OUTPUT"\"
>> > />"
>> > echo " <property name=\"defaultAgencyId\"
>> > value=\"""${OUTPUT[6]}""\" />"
>> > echo " </bean>"
>> > done
>> > done
>> >
>> > What am I doing wrong?
>> >
>> > I was trying to follow this example:
>> >
>> http://stackoverflow.com/questions/918886/split-string-based-on-delimiter-in-bash
>>
>> >
>> > A line in OUTPUT could for example be the below. In this case I'd like
>> to
>> > extract "OId_LG"
>> >
>> "/home/andyt/projects/django-stringer/txc/OId_LG/GTFS/google_transit.zip"
>> >
>> >
>> > Thanks!
>> >
>> >
>> >
>> > On Thursday, 24 January 2013 13:24:45 UTC, Hermenegildo Konstantin
>> wrote:
>> >>
>> >>
>> >>
>> >> Dana četvrtak, 24. siječnja 2013. 02:58:42 UTC+1, korisnik Andrew
>> Taylor
>> >> napisao je:
>> >>>
>> >>> Hi,
>> >>>
>> >>> I'm struggling to figure out if I can do this in bash - can anyone
>> offer
>> >>> me some advice? Within this folder:
>> >>>
>> >>> /projects/django-stringer/txc
>> >>>
>> >>> I have about 30 sub-folders. each one contains a folder within a
>> folder
>> >>> that contains a file called "google_transit.zip" I'd like to print
>> out this
>> >>> list of paths in full if I can. An example path would be:
>> >>>
>> >>>
>> /home/andyt/projects/django-stringer/txc/OId_CW/GTFS/google_transit.zip
>> >>>
>> >>> Thanks!
>> >>>
>> >>> Andy
>> >>
>> >>
>> >> see here :
>> >>
>> >>
>> http://stackoverflow.com/questions/246215/how-can-i-list-files-with-their-absolute-path-in-linux
>>
>> >>
>> >> find `pwd` -name google_transit.zip
>> >>
>> >>
>> > --
>> > --
>> > You received this message because you are subscribed to the Linux Users
>> > Group.
>> > To post a message, send email to [email protected]
>> > To unsubscribe, send email to [email protected]
>> > For more options, visit our group at
>> > http://groups.google.com/group/linuxusersgroup
>> > References can be found at: http://goo.gl/anqri
>> > Please remember to abide by our list rules (
>> http://tinyurl.com/LUG-Rules or
>> > http://cdn.fsdev.net/List-Rules.pdf)
>> >
>> >
>> >
>>
>
--
--
You received this message because you are subscribed to the Linux Users Group.
To post a message, send email to [email protected]
To unsubscribe, send email to [email protected]
For more options, visit our group at
http://groups.google.com/group/linuxusersgroup
References can be found at: http://goo.gl/anqri
Please remember to abide by our list rules (http://tinyurl.com/LUG-Rules or
http://cdn.fsdev.net/List-Rules.pdf)