get file names from the list


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting get file names from the list
# 1  
Old 02-02-2012
get file names from the list

Hi Experts,
Here is my scenario:
Am maintaining a file which has list of logs with complete path and file names like bleow

a/b/c/Daily/file1_20111012.log
d/e/f/Monthly/file1_20111001.log
g/h/Daily/file1_20110120.log
i/Daily/file1_20110220.log

How to copy the file names frm the list using script?

Month will be passed as runtime argument

two scenarios:
1) if the dir is Monthly than I have to get specfic month log (d/e/f/Monthly/file1_20111001.log) frm the list

2) if the dir is Daily than I have to get specfic month all logs (a/b/c/Daily/file1_20111001.log to a/b/c/Daily/file30_20111030.log) frm the list

If somebody can help me here would be highly appreciated!!!
# 2  
Old 02-02-2012
Code:
$ cat test.sh
#! /bin/bash

while read x
do
    echo $x | grep -q "Daily"
    if [ $? -eq 0 ]
    then
        [ `echo $x | cut -d_ -f2 | cut -c5-6` -eq $1 ] && echo $x
        continue
    fi
    echo $x | grep -q "Monthly"
    if [ $? -eq 0 ]
    then
        [ `echo $x | cut -d_ -f2 | cut -c5-6` -eq $1 ] && echo $x
    fi
done < inputfile
$
$ ./test.sh 10
a/b/c/Daily/file1_20111012.log
d/e/f/Monthly/file1_20111001.log
$

# 3  
Old 02-02-2012
balajesuri,

Thanks for quick reply, whn I executed got below error
Code:
grep: illegal option -- q
Usage: grep -hblcnsviw pattern file . . .


Last edited by Franklin52; 02-02-2012 at 06:21 AM.. Reason: Please use code tags for code and data samples, thank you
# 4  
Old 02-02-2012
Your version of grep doesn't support -q switch.
Try this:
Code:
echo $x | grep "Daily" >> /dev/null

# 5  
Old 02-02-2012
balajesuri,

modified script as below

Code:
#! /bin/bash
while read x
do
    echo $x | grep "DAILY"  >>/dev/null
    if [ $? -eq 0 ]
    then
        [ `echo $x | cut -d_ -f2 | cut -c5-6` -eq $1 ] && echo $x
        continue
    fi
    echo $x | grep "MONTHLY"  >>/dev/null
    if [ $? -eq 0 ]
    then
        [ `echo $x | cut -d_ -f2 | cut -c5-6` -eq $1 ] && echo $x
    fi
done < /home/list.txt

whn executed got below error
Code:
./tst.sh: line 7: [: ti: integer expression expected
./tst.sh: line 7: [: wi: integer expression expected


Last edited by Franklin52; 02-02-2012 at 07:44 AM.. Reason: Please use code tags for code and data samples, thank you
# 6  
Old 02-02-2012
Quote:
Originally Posted by laxm

two scenarios:
1) if the dir is Monthly than I have to get specific month log (d/e/f/Monthly/file1_20111001.log) frm the list

2) if the dir is Daily than I have to get specific month all logs (a/b/c/Daily/file1_20111001.log to a/b/c/Daily/file30_20111030.log) frm the list

What do you want to do with those files?
If you are interested in just the filenames, a simple grep would do that !

Code:
$ grep '10[0-9][0-9]\.log$' file
/a/b/c/Daily/file1_20111012.log
/d/e/f/Monthly/file1_20111001.log
$

Do you have any specific requirement?
# 7  
Old 02-02-2012
anchal_khare , It worked.

Can you tell why I got error with script?

Thanks to all, your replies are highly appreciated
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

While loop a file containing list of file names until the files are found?

Hi, I have a control file which will contain all filenames(300) files. Loop through all the file names in the control files and check the existence of this file in another directory(same server). I need to infinitely(2 hrs) run this while loop until all the files are found. Once a file is found,... (5 Replies)
Discussion started by: laknar
5 Replies

2. Shell Programming and Scripting

List the file names available on FTP server before selecting the required file

Below is my script code.which shows the environment name and then fetch the file from the ftp server but I am facing one issue.The script should be run in both way.We can pass the arguments with script and select the environment name then file name.Here the issue is I am not able to list the files... (1 Reply)
Discussion started by: anuragpgtgerman
1 Replies

3. UNIX for Dummies Questions & Answers

List Directory names which have the file

Hi All, Can any one help me to list out the directory names which contain the specified file. See for example File name : file.201307014.LKT Have the directory structure as below. /app/work/data/INDIA/file.201307014.LKT /app/work/data/AMERICA/file.201307014.KTP... (5 Replies)
Discussion started by: Balasankar
5 Replies

4. Shell Programming and Scripting

List the file names that differ

Hello, I have two directories - prev and current . They both have same multiple subdirectories and files. Now the current directory can have some updated files and some new files added that is not in prev. I want to find the list of file names that differ. I am doing this because i can not... (2 Replies)
Discussion started by: jakSun8
2 Replies

5. Shell Programming and Scripting

How to get the dates from a list of file names?

Hi All, I have a .txt file with the list of filenames as given below. /dev_data/dev3/ctl/20120622_Employee.txt /dev_data/dev3/ctl/20120623_Employee.txt /dev_data/dev3/ctl/20120624_Employee.txt I want to read this file & write the dates alone from the filenames into a .done file. ... (6 Replies)
Discussion started by: dsfreddie
6 Replies

6. Shell Programming and Scripting

List of file names

I have the following list of file names stored in $fnames, so that if I do foreach f ($fnames) echo "$f" end I will get n02-z30-sr65-rgdt0p50-dc0p002-16x12drw-run1 n02-z30-sr65-rgdt0p50-dc0p002-16x12drw-run2 n02-z30-sr65-rgdt0p50-dc0p002-16x12drw-run3... (3 Replies)
Discussion started by: kristinu
3 Replies

7. UNIX for Dummies Questions & Answers

Help with list of latest file names

Hi Gurus, I need to list only those files which are most recent and the latest log file. For example if I have the following file list: JOB001.LOG_00uv02_00006 - Jul 7 12:16 JOB001.LOG_00vi0t_00001 - Aug 4 21:58 JOB001.LOG_00vi0t_00002 - Aug 5 09:15 JOB001.LOG_00vi0t_00003 - Aug 5... (6 Replies)
Discussion started by: shash
6 Replies

8. UNIX for Dummies Questions & Answers

Get List of Unique File Names

I have a large directory of web pages. I am doing a search through the web pages using grep and would like to get a list of unique file names of search results. The following command works fine to give me a list of file names where term appears: grep -l term *.html However, since these are... (3 Replies)
Discussion started by: rjulich
3 Replies

9. AIX

find for specific content in file in the directory and list only file names

Hi, I am trying to find the content of file using grep and find command and list only the file names but i am getting entire file list of files in the directory find . -exec grep "test" {} \; -ls Can anyone of you correct this (2 Replies)
Discussion started by: madhu_Jagarapu
2 Replies

10. UNIX for Dummies Questions & Answers

How to Pass a list of file names to ls

Hi I have a list of file names generated from a find command. The list does not show complete file information. I would like to do this: generate the list of file names pass each file name generated to ls -l command what is the best way to do this without a script? I have tried... (2 Replies)
Discussion started by: GMMike
2 Replies
Login or Register to Ask a Question