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 ...
![]() |
| | LinkBack | Thread Tools | Display Modes |
|
#1
| |||
| |||
| 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 |
|
#2
| |||
| |||
|
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 |
|
#3
| |||
| |||
|
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 |
|
#4
| |||
| |||
|
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 |
|
#5
| |||
| |||
|
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. |
|
#6
| |||
| |||
|
On Nov 11, 11:57*am, Naru > 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_ 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 |
|
#7
| |||
| |||
|
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 |
|
#8
| |||
| |||
|
Naru wrote: > > btw what does this "${var1#?}" do??? n how does it suppress the > unwanted ':" Removes the first character from var1. Janis |
|
#9
| |||
| |||
|
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?? |
|
#10
| |||
| |||
|
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?? |
![]() |
« Previous Thread
|
Next Thread »
| Thread Tools | |
| Display Modes | |
| |
All times are GMT -4. The time now is 01:05 PM.




Linear Mode