dbaspot
Tags Register FAQ Calendar Search Today's Posts Mark Forums Read

concatenating the filenames in a directory - shell

This is a discussion on concatenating the filenames in a directory - shell ; hi all, I have a requirement where in i have to read all the filenames based on a pattern from a directory and concatenate all these file names and write it to another file. i am using the following code ...


Home > Database Forum > Operating Systems > shell > concatenating the filenames in a directory

Reply

 

LinkBack Thread Tools Display Modes
  #1  
Old 11-11-2008, 12:49 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default concatenating the filenames in a directory

hi all,

I have a requirement where in i have to read all the filenames based
on a pattern from a directory and concatenate all these file names and
write it to another file.


i am using the following code to do this

var1=''
for filename in $_DIR/${FILE_NAME}*
do
if
if [ X${var1} = "X" ]
then
var1=$var1$filename
else
var1=$var1:$filename
fi

done

`echo "$var1"` >> $DIR/$filename_out.txt


its taking really long time to execute this script if there are more
number files matching the pattern.


can any one suggest a better appraoch, than above one?

Thanks,
Narendar
Reply With Quote
  #2  
Old 11-11-2008, 01:08 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: concatenating the filenames in a directory

Naru wrote:
> hi all,
>
> I have a requirement where in i have to read all the filenames based
> on a pattern from a directory and concatenate all these file names and
> write it to another file.
>
>
> i am using the following code to do this
>
> var1=''
> for filename in $_DIR/${FILE_NAME}*
> do
> if
> if [ X${var1} = "X" ]
> then
> var1=$var1$filename
> else
> var1=$var1:$filename
> fi
>
> done
>
> `echo "$var1"` >> $DIR/$filename_out.txt
>
>
> its taking really long time to execute this script if there are more
> number files matching the pattern.
>
>
> can any one suggest a better appraoch, than above one?


To stay close to your context I'd maybe do something like...

var1=$( printf ":%s" "$_DIR/${FILE_NAME}"* )
printf "%s\n" "${var1#?}" >> "$DIR/$filename_out.txt"


Janis

>
> Thanks,
> Narendar

Reply With Quote
  #3  
Old 11-11-2008, 01:14 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: concatenating the filenames in a directory

On Nov 11, 11:08*am, Janis Papanagnou
wrote:
> Naru wrote:
> > hi all,

>
> > I have a requirement where in i have to read all the filenames based
> > on a pattern from a directory and concatenate all these file names and
> > write it to another file.

>
> > i am using the following code to do this

>
> > var1=''
> > for filename in $_DIR/${FILE_NAME}*
> > do
> > if
> > if [ X${var1} = "X" ]
> > * * * * then
> > * * * * * * * * var1=$var1$filename
> > * * * * else
> > * * * * * * * * var1=$var1:$filename
> > * * * * fi

>
> > done

>
> > `echo "$var1"` >> $DIR/$filename_out.txt

>
> > its taking really long time to execute this script if there are more
> > number files matching the pattern.

>
> > can any one suggest a better appraoch, than above one?

>
> To stay close to your context I'd maybe do something like...
>
> var1=$( printf ":%s" "$_DIR/${FILE_NAME}"* )
> printf "%s\n" "${var1#?}" >> "$DIR/$filename_out.txt"
>
> Janis
>
>
>
> > Thanks,
> > Narendar


thanks for the reply,

the reason i have included if in the above code is i am getting a ':'
before the first file which i want to avoid.

Thanks,
Narendar
Reply With Quote
  #4  
Old 11-11-2008, 01:23 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: concatenating the filenames in a directory

Naru wrote:
> On Nov 11, 11:08 am, Janis Papanagnou
> wrote:
>
>>Naru wrote:
>>
>>>hi all,

>>
>>>I have a requirement where in i have to read all the filenames based
>>>on a pattern from a directory and concatenate all these file names and
>>>write it to another file.

>>
>>>i am using the following code to do this

>>
>>>var1=''
>>>for filename in $_DIR/${FILE_NAME}*
>>>do
>>>if
>>>if [ X${var1} = "X" ]
>>> then
>>> var1=$var1$filename
>>> else
>>> var1=$var1:$filename
>>> fi

>>
>>>done

>>
>>>`echo "$var1"` >> $DIR/$filename_out.txt

>>
>>>its taking really long time to execute this script if there are more
>>>number files matching the pattern.

>>
>>>can any one suggest a better appraoch, than above one?

>>
>>To stay close to your context I'd maybe do something like...
>>
>>var1=$( printf ":%s" "$_DIR/${FILE_NAME}"* )
>>printf "%s\n" "${var1#?}" >> "$DIR/$filename_out.txt"
>>
>>Janis
>>
>>
>>
>>
>>>Thanks,
>>>Narendar

>
>
> thanks for the reply,
>
> the reason i have included if in the above code is i am getting a ':'
> before the first file which i want to avoid.


And the reason why I have included "${var1#?}" was to remove that
spurious ':'.

Janis

>
> Thanks,
> Narendar

Reply With Quote
  #5  
Old 11-11-2008, 01:57 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: concatenating the filenames in a directory

On Nov 11, 11:23*am, Janis Papanagnou
wrote:
> Naru wrote:
> > On Nov 11, 11:08 am, Janis Papanagnou
> > wrote:

>
> >>Naru wrote:

>
> >>>hi all,

>
> >>>I have a requirement where in i have to read all the filenames based
> >>>on a pattern from a directory and concatenate all these file names and
> >>>write it to another file.

>
> >>>i am using the following code to do this

>
> >>>var1=''
> >>>for filename in $_DIR/${FILE_NAME}*
> >>>do
> >>>if
> >>>if [ X${var1} = "X" ]
> >>> * * * *then
> >>> * * * * * * * *var1=$var1$filename
> >>> * * * *else
> >>> * * * * * * * *var1=$var1:$filename
> >>> * * * *fi

>
> >>>done

>
> >>>`echo "$var1"` >> $DIR/$filename_out.txt

>
> >>>its taking really long time to execute this script if there are more
> >>>number files matching the pattern.

>
> >>>can any one suggest a better appraoch, than above one?

>
> >>To stay close to your context I'd maybe do something like...

>
> >>var1=$( printf ":%s" "$_DIR/${FILE_NAME}"* )
> >>printf "%s\n" "${var1#?}" >> "$DIR/$filename_out.txt"

>
> >>Janis

>
> >>>Thanks,
> >>>Narendar

>
> > thanks for the reply,

>
> > the reason i have included if in the above code is i am getting a ':'
> > before the first file which i want to avoid.

>
> And the reason why I have included *"${var1#?}" *was to remove that
> spurious ':'.
>
> Janis
>
>
>
> > Thanks,
> > Narendar


my bad..did notice that part of the code...thanks again..its working
fine.
Reply With Quote
  #6  
Old 11-11-2008, 02:10 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: concatenating the filenames in a directory

On Nov 11, 11:57*am, Naru wrote:
> On Nov 11, 11:23*am, Janis Papanagnou
> wrote:
>
>
>
> > Naru wrote:
> > > On Nov 11, 11:08 am, Janis Papanagnou
> > > wrote:

>
> > >>Naru wrote:

>
> > >>>hi all,

>
> > >>>I have a requirement where in i have to read all the filenames based
> > >>>on a pattern from a directory and concatenate all these file names and
> > >>>write it to another file.

>
> > >>>i am using the following code to do this

>
> > >>>var1=''
> > >>>for filename in $_DIR/${FILE_NAME}*
> > >>>do
> > >>>if
> > >>>if [ X${var1} = "X" ]
> > >>> * * * *then
> > >>> * * * * * * * *var1=$var1$filename
> > >>> * * * *else
> > >>> * * * * * * * *var1=$var1:$filename
> > >>> * * * *fi

>
> > >>>done

>
> > >>>`echo "$var1"` >> $DIR/$filename_out.txt

>
> > >>>its taking really long time to execute this script if there are more
> > >>>number files matching the pattern.

>
> > >>>can any one suggest a better appraoch, than above one?

>
> > >>To stay close to your context I'd maybe do something like...

>
> > >>var1=$( printf ":%s" "$_DIR/${FILE_NAME}"* )
> > >>printf "%s\n" "${var1#?}" >> "$DIR/$filename_out.txt"

>
> > >>Janis

>
> > >>>Thanks,
> > >>>Narendar

>
> > > thanks for the reply,

>
> > > the reason i have included if in the above code is i am getting a ':'
> > > before the first file which i want to avoid.

>
> > And the reason why I have included *"${var1#?}" *was to remove that
> > spurious ':'.

>
> > Janis

>
> > > Thanks,
> > > Narendar

>
> my bad..did notice that part of the code...thanks again..its working
> fine.


Hi i have a similar requirement,

basically i have to read the a file which will contain two lines as:

xxxx DIR/filename_.dat
yyyy DIR/pattern

now i have to replace the date with the today's date in the first line
and in the second line i have to replace the "DIR/pattern" with the
list of all files concatenated in the DIR as in above example.

i could do both of them using multiple reads of the input file, which
i want to avoid, i would like to do this in only one read and one
write.

any solutions/comments

thanks,
amar
Reply With Quote
  #7  
Old 11-11-2008, 03:05 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: concatenating the filenames in a directory

On Nov 11, 11:23*am, Janis Papanagnou
wrote:
> Naru wrote:
> > On Nov 11, 11:08 am, Janis Papanagnou
> > wrote:

>
> >>Naru wrote:

>
> >>>hi all,

>
> >>>I have a requirement where in i have to read all the filenames based
> >>>on a pattern from a directory and concatenate all these file names and
> >>>write it to another file.

>
> >>>i am using the following code to do this

>
> >>>var1=''
> >>>for filename in $_DIR/${FILE_NAME}*
> >>>do
> >>>if
> >>>if [ X${var1} = "X" ]
> >>> * * * *then
> >>> * * * * * * * *var1=$var1$filename
> >>> * * * *else
> >>> * * * * * * * *var1=$var1:$filename
> >>> * * * *fi

>
> >>>done

>
> >>>`echo "$var1"` >> $DIR/$filename_out.txt

>
> >>>its taking really long time to execute this script if there are more
> >>>number files matching the pattern.

>
> >>>can any one suggest a better appraoch, than above one?

>
> >>To stay close to your context I'd maybe do something like...

>
> >>var1=$( printf ":%s" "$_DIR/${FILE_NAME}"* )
> >>printf "%s\n" "${var1#?}" >> "$DIR/$filename_out.txt"

>
> >>Janis

>
> >>>Thanks,
> >>>Narendar

>
> > thanks for the reply,

>
> > the reason i have included if in the above code is i am getting a ':'
> > before the first file which i want to avoid.

>
> And the reason why I have included *"${var1#?}" *was to remove that
> spurious ':'.
>
> Janis
>
>
>
> > Thanks,
> > Narendar


btw what does this "${var1#?}" do??? n how does it suppress the
unwanted ':"

Thanks,
Narendar

Reply With Quote
  #8  
Old 11-11-2008, 03:17 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: concatenating the filenames in a directory

Naru wrote:
>
> btw what does this "${var1#?}" do??? n how does it suppress the
> unwanted ':"


Removes the first character from var1.

Janis
Reply With Quote
  #9  
Old 11-12-2008, 06:19 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: concatenating the filenames in a directory

On Nov 11, 1:17*pm, Janis Papanagnou
wrote:
> Naru wrote:
>
> > btw what does this "${var1#?}" do??? n how does it suppress the
> > unwanted ':"

>
> Removes the first character from var1.
>
> Janis


Hi when i write this line to a file and while opening the file it is
saying last line incomplete..hence i am not able to use this file in
any other process

any idea how to resolve this??
Reply With Quote
  #10  
Old 11-12-2008, 06:19 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: concatenating the filenames in a directory

On Nov 11, 1:17*pm, Janis Papanagnou
wrote:
> Naru wrote:
>
> > btw what does this "${var1#?}" do??? n how does it suppress the
> > unwanted ':"

>
> Removes the first character from var1.
>
> Janis


Hi when i write this line to a file and while opening the file it is
saying last line incomplete..hence i am not able to use this file in
any other process

any idea how to resolve this??
Reply With Quote
Reply

Thread Tools
Display Modes



All times are GMT -4. The time now is 01:05 PM.