How to select the *.txt files using Loops


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to select the *.txt files using Loops
# 1  
Old 12-25-2008
How to select the *.txt files using Loops

Hi,

I need to select the txt files from one folder and pass this filename as parameter and the loop should discontinue after all the files are selected and the iteration should continue only once..

Viz., i have files like abc_2005_f1.txt, abc_2006_f1.txt, abc_2007_f1.txt... in /data folder.

I need to select this filename from the folder and pass this as parameter to other function inside the loop. viz.,
msh samplefile.msh abc_2005_f1.txt
msh samplefile.msh abc_2006_f1.txt...Like this the loop should continue and end after the last file..
Can you guide me in making the script..

Thanks,
Kumar
# 2  
Old 12-25-2008
If you're passing the name to one command
Code:
find /data/ -name '*.txt' -print | xargs -I'{}' -n1 msh samplefile.msh '{}'
or
find /data/ -name '*.txt' -exec msh samplefile.msh '{}' \;

If there's more to do
Code:
for file in `find /data/ -name '*.txt' -print`
do
    # stuff
done

# 3  
Old 12-26-2008
Thanks for your time in replying to that..
Actually the script which you told is selecting the entire path
when i give $file to pass the parameter..

But i need only the .txt file name to execute in some other function..

Can you please clarify me on this..

Thanks
Kumar
# 4  
Old 12-26-2008
just filename from the entire path

Code:
basename <full_path>

# 5  
Old 12-26-2008
for file in `find /data/ -name '*.txt' -print`
do
# stuff (here i need to see only the filename but not entire path)
done

basename /data/

As suggested i have used basename but it is displaying the path.
Moreover, i need to see the logs of each and every file executed in the loop.

if i give > logfilename ...it will overwrite everytime for each file. but i need to see the logfile for all the files selected in the loop.
if we use >> next time it will append the new log to the old log for the next run..

can you please provide me the example of how to capture the logs for all the files in this run and replace the same for next run..

Thanks
Kumar
# 6  
Old 12-26-2008
Quote:
Originally Posted by kumar16
for file in `find /data/ -name '*.txt' -print`
do
# stuff (here i need to see only the filename but not entire path)
done
Please use code tags
Quote:
basename /data/

As suggested i have used basename but it is displaying the path.
Moreover, i need to see the logs of each and every file executed in the loop.
That was an example

that should be used as
Code:
just_filename=`basename $full_path`

Quote:

if i give > logfilename ...it will overwrite everytime for each file. but i need to see the logfile for all the files selected in the loop.
if we use >> next time it will append the new log to the old log for the next run..

can you please provide me the example of how to capture the logs for all the files in this run and replace the same for next run..

Thanks
Kumar
For retaining all the logs ( which is good ! ) you need to append ' >> '

to make sure all the logs for each and every run is retained, append timestamp_datestamp to the log file which makes the log_filename unique
# 7  
Old 12-26-2008
Hi Madhan,

Thanks for quick suggestions!

Sorry if i not understood correctly from your reply..

Actually i need to have the log file like this..
viz., i have 4 .txt files and am executing command using for loop for all the four files..

when i use '>>' it will do for all four files today..

Again if i want to run the same script tomorrow, the existing log file should get replaced with the 4 .txt files run tomorrow..

can you suggest me on this..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare two txt files,mismatches will be in new txt files

Hi, Below are the sample data for txt files. txt file 1 Partnumber|catgroup_id 10001082|46016 10001093|4680 10001093|386003 10001093|463004 10003251|683 10003251|63005 10003252|463005 10003252|4683 10003260|463005 10003260|4683 10003264|4683 10003264|463005 13420000|67... (5 Replies)
Discussion started by: Ankita Talukdar
5 Replies

2. Shell Programming and Scripting

Nested loops for copying files on different machines

I want to run nested loop inside shell script using which I want to copy files on diff machines on round robin basis.Number of machiines is fixed as 4 but number of files may vary. For eg. Files: f1,f2,f3,f4,f5,f6 Machines: M1,M2,M3,M4 M1 - f1 M2 - f2 M3 - f3 M4 - f4 M1 - f5 M2 - f6 ... (1 Reply)
Discussion started by: baig_1988
1 Replies

3. UNIX for Dummies Questions & Answers

Select text between current date and output to new txt file

Hi guys, brand new to this thread and very very new to UNIX...so go easy please! Anyway I have a file that looks like this: >>-------------------------------------------------------------------------- Date/Time/Eng. : 2012-06-22 / 00:26 / DS Reported problem : (SD) ... (5 Replies)
Discussion started by: martin0852
5 Replies

4. UNIX Desktop Questions & Answers

awk using 2 input files instead of while loops

Hi Friends, I have two files as input with data that looks like this: file1.txt 1 2 3 4 file2.txt a,aa b,bb c,cc d,dd e,ee f,ff instead of me doing 2 while loops to get the combinations while read line_file1 (2 Replies)
Discussion started by: kokoro
2 Replies

5. UNIX for Dummies Questions & Answers

Nested for loops for checking duplicate files

I am very new to bash scripting and this is my first script. I am trying to write a script that takes an argument d as the directory. It looks through the files to find duplicates and delete them. Here's some sorta-pseudocode but am unsure how to implement it: #! /bin/bash #get... (1 Reply)
Discussion started by: shubham92
1 Replies

6. Shell Programming and Scripting

Select some lines from a txt file and create a new file with awk

Hi there, I have a text file with several colums separated by "|;#" I need to search the file extracting all columns starting with the value of "1" or "2" saving in a separate file just the first 7 columns of each row maching the criteria, with replacement of the saparators in the nearly created... (4 Replies)
Discussion started by: capnino
4 Replies

7. Solaris

list files .txt and .TXT in one command

Dear experts, In a directory i have both *.TXT and *.txt files. I have a script- for file in `ls *.txt`; do mv $file /tmp/$file How to list both *.txt and*.TXT file in one command so that script will move both .txt or .TXT whatever it find. br//purple (4 Replies)
Discussion started by: thepurple
4 Replies

8. Shell Programming and Scripting

Redirecting sql select query result to txt file

Hi Yogesh, Lucky that i caught you online. Yeah i read about DBI and the WriteExcel module. But the server is not supporting these modules. It said..."Cannot locate DBI"..."Cannot locate Spreadsheet::WriteExcel" I tried creating a simple text file to get the query output, but the... (1 Reply)
Discussion started by: dolphin123
1 Replies

9. Shell Programming and Scripting

Redirecting sql select query result to txt file

Hi , I just found you while surfing for the string 'Redirecting sql select query output from within a shell script to txt file/excel file' Could you find time sending me the code for the above question? It'll be great help for me. I have a perl file that calls the sql file... (1 Reply)
Discussion started by: dolphin123
1 Replies

10. Shell Programming and Scripting

Mutiple For loops - moving files to another directory

I need to clean out some application subdirectories from backup scripts we used to rename to various backup extensions just in case the script failed in production and we need to rollback. I will be moving these old scripts to a staging directory and then removing them after 30 days (I have the... (9 Replies)
Discussion started by: tekster757
9 Replies
Login or Register to Ask a Question