laxm
1
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!!!
$ 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
$
laxm
3
balajesuri,
Thanks for quick reply, whn I executed got below error
grep: illegal option -- q
Usage: grep -hblcnsviw pattern file . . .
Your version of grep doesn't support -q switch.
Try this:
echo $x | grep "Daily" >> /dev/null
laxm
5
balajesuri,
modified script as below
#! /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
./tst.sh: line 7: [: ti: integer expression expected
./tst.sh: line 7: [: wi: integer expression expected
clx
6
What do you want to do with those files?
If you are interested in just the filenames, a simple grep would do that !
$ 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?
laxm
7
anchal_khare , It worked.
Can you tell why I got error with script?
Thanks to all, your replies are highly appreciated
#!/bin/bash
[[ $# != 1 ]] && {
echo "ERROR: No argument"
exit 1
}
while read line
do
[[ $line =~ $1 ]] && echo $line
done < /home/list.txt