concatenating the filenames in a directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting concatenating the filenames in a directory
# 1  
Old 11-11-2008
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
# 2  
Old 11-11-2008
Tools Could you approach this like:

pattern="xyz"
ls | grep "$pattern" > file_out.txt
# 3  
Old 11-11-2008
Quote:
Originally Posted by joeyg
pattern="xyz"
ls | grep "$pattern" > file_out.txt
this will add the files matching the patter to the file_out..

but i want them to concatenated using ":" and added.
# 4  
Old 11-11-2008
What the heck does concatenated mean. Provide an example of what you want to do.
# 5  
Old 11-11-2008
I did something similar using the current date (fetching Status pages from my cable modem). Don't know if this will help you:
Code:
CUR=`date | sed 's/\ /_/g'`.asp
wget http://192.168.100.1/Status.asp
mv Status.asp $CUR

# 6  
Old 11-11-2008
Quote:
Originally Posted by chatwizrd
What the heck does concatenated mean. Provide an example of what you want to do.
for the heck...i want the output to be in the form
DIR/filename: DIR/filename2: DIR/filename3: DIR/filename4..so on to be added to the output file.

[space between : and DIR is not required but the webpage was converting it to a smiley Smilie. so i have kept the space. in original output that space is not required]
where filename[1-4]..files matching the pattern 'filename' in the DIR

as i mentioned i was able to do this..but just wanted to know if there is any better way to do this....
# 7  
Old 11-11-2008
Quote:
Originally Posted by nvuradi
...
as i mentioned i was able to do this..but just wanted to know if there is any better way to do this....
If you don't mind the colon at the end of the line use:

Code:
$ dir=test
$ FILE_NAME=html

$ printf "%s:" `ls -p $dir/${FILE_NAME}*|grep -v '/$'` 

$ test/html_1:test/html_2:test/html_3:

removing the last colon:

Code:
$ printf "%s:" `ls -p $dir/${FILE_NAME}*|grep -v '/$'` | sed 's/:$//'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing filenames in my current directory

Can someone give me a tip on writing a script that, for each file in the working directory, prints the filename, the # of lines, and the # of words to stdout? (2 Replies)
Discussion started by: flash123
2 Replies

2. Shell Programming and Scripting

How to take the filenames from a directory and store into a file??

hi, how can i take the file names from a directory and store only the filenames in the file. suppose i have a directory which contains the following files and subdirectories. $ ls -ltr total 16 -rw-rw-r-- 1 adm etc 4 Aug 6 20:37 s1.txt -rw-rw-r-- 1 adm etc 4 Aug 6 20:37 s2.txt... (11 Replies)
Discussion started by: Little
11 Replies

3. Shell Programming and Scripting

Concatenating contents of a file with members in a directory

Hi, I have a unix file with the below structure - CustId1 CustName1 CustPhn1 /u/home/xmldata/A000001 CustId2 CustName2 CustPhn2 /u/home/xmldata/A000002 CustId3 CustName3 CustPhn3 /u/home/xmldata/A000003 Then I have another unix directory /u/home/xmldata This directory has... (3 Replies)
Discussion started by: Simanto
3 Replies

4. Shell Programming and Scripting

How to get filenames in a directory and write them in to a file?

I need to get the names of files which are starting with a string testfile. Also i want to create a XML file in the same location and write these file names into the XML. Ex: <path> <dir> <file>testfile1</file> </dir> <dir> <file>testfile2</file> </dir>... (4 Replies)
Discussion started by: vel4ever
4 Replies

5. Shell Programming and Scripting

concatenating similar files in a directory

Hi, I am new in unix. I have below requirement: I have two files at the same directory location File1.txt and File2.txt (just an example, real scenario we might have File2 and File3 OR File6 and File7....) File1.txt has : header1 record1 trailer1 File2.txt has: header2 record2... (4 Replies)
Discussion started by: Deepak62828r
4 Replies

6. UNIX for Dummies Questions & Answers

Filenames change in a directory

Hi I have abc_ahb_one.v abc_ahb_two.v abc_ahb_three.v ........l like this -----upto abc_ahb_ninety.v in some directory. I need to change those file names to like below. ... (5 Replies)
Discussion started by: praneethk
5 Replies

7. Shell Programming and Scripting

Getting filenames from a directory in single line

Hi All, I have a requirement where I need to get the all file names present in a particular directory in to a single separated by space, I have files in /home/RAAM/work directory as test1.xls test2.xls test3.xls I need to get all filenames from that directory in single line like ... (6 Replies)
Discussion started by: Raamc
6 Replies

8. Shell Programming and Scripting

Use filenames to create directory.

I have many files similar to this one: AC41_AC85_86_AC128_129_MC171_173_SF_207_FMV.pdf. I want a directory named AC41 and to put the file AC41_AC85_86_AC128_129_MC171_173_SF_207_FMV.pdf into the directory. Next, a directory named AC85 and put the file into it. Also, continue to cycle through... (1 Reply)
Discussion started by: ndnkyd
1 Replies

9. Shell Programming and Scripting

Change all filenames in a directory

I have a directory of files and each file has a random 5 digit string at the beginning that needs to be removed. Plus, there are some files that will be identically named after the 5 digit string is removed and I want those eliminated or moved. any ideas? (17 Replies)
Discussion started by: crumb
17 Replies

10. Shell Programming and Scripting

looping thru filenames in a directory

Hi, i am very new to UNIX, i am trying to loop thru the files in a directory. I got the filenames into a variable using $files=`ls` Here $files will contain <filename1> <filename2> <filename3> I want to get one filename at a time and append it to some some text. forexample, ... (1 Reply)
Discussion started by: silas.john
1 Replies
Login or Register to Ask a Question