Shell script to loop through files

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Shell script to loop through files
# 1  
Old 01-03-2017
Shell script to loop through files

Hi Team,

I am new to shell scripting. I have the below requirement

1) Say if i am searching for 20160815 in a directory /dir

2) Now i need to get the files present in dir whose time stamp in greater than or equal to 20160815

3) Then i need to find the string 20160815 from the set of files received in point 2

4) Now i need to get the final file name as output


I visited many forums to understand how to loop through files. Please help me in this regard

Thanks and Regards
Raj
# 2  
Old 01-03-2017
Quote:
Originally Posted by Rajendra Kalepu
Hi Team,

I am new to shell scripting. I have the below requirement

1) Say if i am searching for 20160815 in a directory /dir

2) Now i need to get the files present in dir whose time stamp in greater than or equal to 20160815

3) Then i need to find the string 20160815 from the set of files received in point 2

4) Now i need to get the final file name as output


I visited many forums to understand how to loop through files. Please help me in this regard

Thanks and Regards
Raj
Is this a homework assignment? Homework and coursework questions can only be posted in the Homework and Coursework forum under special homework rules.

If this is not homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

If this is not homework, please explain what "searching for 20160815 in a directory" means. Are you looking for a file named 20160815? Are you looking for a file with a name containing the string 20160815? Are you looking for a file containing the string 20160815? Are you looking for a file in the directory /dir or are you looking for a file in a directory somewhere in the file hierarchy rooted in /dir?

Standards conforming filesystems have at least three timestamps (last file status change time, last access time, and last file modification time). For which time stamp are you looking?

How do you "find the string 20160815 from the set of files received in point 2"? Are you looking for a file named 20160815? Are you looking for a file with a name containing the string 20160815? Are you looking a file containing the string 20160815?
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 01-03-2017
Hi Don

I am working for a retail firm. During test phase i need to get the file which is having a particular string



1) Say if i am searching for 20160815 in a directory /dir
Here i am referring to string '20160815' which is actually a date in YYYYMMDD format

2) Now i need to get the files present in dir whose time stamp in greater than or equal to '20160815'
I mean to say , i want to search the for the files which are generated on 20160815 (ex : file1.txt),20160816 (file2.txt),20160817 (file3.txt) and so on
Here i am looking for a created date > = 20160815

3) Then i need to find the string '20160815' from the set of files received in point 2
file1.txt, file2.txt,file3.txt

4) Now i need to get the final file name as output
Say the string is present in file3.txt. I need to output it

I visited many forums to understand how to loop through files. Please help me in this regard

Thanks and Regards
Raj
# 4  
Old 01-03-2017
I'm afraid your answers to Don Cragun's question didn't help too much. Paraphrasing questions or statements sometimes helps to achieve a common understanding, but it didn't add any relevant and important information, and in this case posting more structures and details had put us in a position to deal with your quite vague explanations and circumscriptions.

1) Yes, understood, but please look the last paragraph in post#2.
2) Just guessing now: you are looking at files' contents, as the file name "file1.txt" doesn't contain the search string, and assuming that string represents the "creation date". Is that the ONLY string in the file that has this structure (8 digits possibly in YYYYMMDD format)? Where resp. when to stop matching (Today? 1.January? 2017? 2020?)?
3) What is different here from item 1) ?
4) "The string" actually is sort of a range of dates (mayhap with an open end). So why not use the result of 3) [or even 1) ] ?

Please rephrase your request more precisely, and supply input data (directory structure to be searched, and a representative file sample).
Or, simply, wouldn't just a grep with a smartly fitted regex do what you need?

Last edited by RudiC; 01-04-2017 at 06:06 PM.. Reason: typos
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Loop in shell script

Hi Friends, I have a file. the content is as below: file1: /A/B/C/abc.txt 2013-07-28 13:50:00,2013-07-31 01:00:00,5,710 /A/B/C/xyz.txt 2011-09-21 18:30:00,2013-07-30 06:15:00,15,65135 2009-11-09 18:00:00,2011-09-02 09:00:00,5,12345 2013-07-28 13:50:00,2013-07-31 01:00:00,5,710 ... (2 Replies)
Discussion started by: vsachan
2 Replies

2. Shell Programming and Scripting

Help with the For loop shell script

Hi, I have multiple files in a directory. Each file will have a header.I have to check if any of the files has 0 rows other than the header then I have to delete the files. Here “ Empty file” in my case means a file has header information but no data. I have to delete such files. If the file... (2 Replies)
Discussion started by: ganesnar
2 Replies

3. UNIX for Dummies Questions & Answers

Using Shell Script To Loop Program Through Multiple Text Files

Hello, So I have approximately 300 files of raw data (.txt) files that I am using to perform statistical analysis. I have been able to construct a Fortran program that is able to perform my statistical analysis on a file by file basis. However, I now want to be able to loop program through... (19 Replies)
Discussion started by: Jimmyd24
19 Replies

4. UNIX for Dummies Questions & Answers

Writing a loop to process multiple input files by a shell script

I have multiple input files that I want to manipulate using a shell script. The files are called 250.1 through 250.1000 but I only want the script to manipulate 250.300 through 250.1000. Before I was using the following script to manipulate the text files: for i in 250.*; do || awk... (4 Replies)
Discussion started by: evelibertine
4 Replies

5. Shell Programming and Scripting

Shell script using loop

Hi everyone, I have n number of data in my file "temp" in following order.In each line table_name and column_name are different.input data is in same format each query in three lines. ALTER TABLE table_name ADD ( column_name1 VARCHAR2(10), column_name2 VARCHAR2(70) ); ... (23 Replies)
Discussion started by: alisha
23 Replies

6. Shell Programming and Scripting

Loop in shell script

Dear experts, i am quite new to shell script please any one can help me in this regard i would like write a script which takes input in the form >./Test.sh a,10,b,20,c,30... in this way i can give input in any number which is not constant in the end through loop i want to... (3 Replies)
Discussion started by: vin_pll
3 Replies

7. Shell Programming and Scripting

Help with loop in a shell script

I just want to write a little script, that reads the lines from a file, echos somthing in a new tmp.file and then do some commands whith the tmp.files. while read -r line do echo "TEST=" > tmp.$$ echo "$line" >> tmp.$$ any_command < tmp.$$ done < $INPUTFILE But I think I have to... (2 Replies)
Discussion started by: elifchen
2 Replies

8. AIX

how to loop through non-empty files with shell script on AIX

I have av script that loops through some statistic files to create a report. We would like to only loop through non-empty files as these files create an empty report-line. I have figured out how to find the non-empty files, but not how to loop through only those files. Here is the code that finds... (4 Replies)
Discussion started by: Tessa
4 Replies

9. Shell Programming and Scripting

Help shell script to loop through files update ctl file to be sql loaded

I am currently trying to find a way to loop through files in a given directory and for each file modify a ctl file and sql load it. I have been using the sed command to change the infile, badfile parameters of the control file. I have not yet tried to sql load it. Requirement: files are ftp to... (1 Reply)
Discussion started by: dba_nh
1 Replies

10. Shell Programming and Scripting

If then else loop in Shell script

Hi Following is the code . When I give input as Bangalore,its dospalying Welcome to Hitech City. But say , if I select Delhi or US, its not displaying the corresponding message. Its still says Welcome to Hitech City. Seems that it not entering in the elif part. Please suggest. #!... (4 Replies)
Discussion started by: pankajkrmishra
4 Replies
Login or Register to Ask a Question