parpaa
asked on
Grep in Bash Script
Hi Experts,
I'm writing script to find out last files and its modified date - unfortunately am having problem with the below script.
Error message:
Any help is greatly appreciated
thank you
I'm writing script to find out last files and its modified date - unfortunately am having problem with the below script.
Error message:
"grep: sales.txt: No such file or directory"
#!/bin/bash
var=1
var1=`awk '{n++} END {print n}' sales.txt`
while [[ ${var} -le ${var1} ]]
do
prod=$var
member=`grep ^$prod, sales.txt | cut -d "," -f2 | tr -d "\15"`
due1=`grep ^$prod, sales.txt | tail -1 | cut -d "," -f3 | tr -d "\15"`
FTP=`grep ^$prod, sales.txt | tail -1 | cut -d "," -f4 | tr -d "\15"`
FolderName=`grep ^$prod, sales.txt | tail -1 | cut -d "," -f5 | tr -d "\15"`
curdate=`date +%Y-%m-%d`
duedate1=$( date +%Y-%m-$due1 )
keyword=`grep ^$prod, sales.txt | tail -1 | cut -d "," -f7 | tr -d "\15"`
#echo "$member","$due1","$FolderName","$keyword","$FTP"
if [[ "$FTP" == "ABC" ]]
then
echo $FolderName
cd /
cd /archive/$FolderName
file1_To_Copy=`ls -tr *"$keyword"* |tail -n 1`
last_mod_date1=`ls --time-style=+%Y-%m-%d -ld $file1_To_Copy | awk '{print $6}'`
else
echo "do nothing"
fi
echo "$member","$due1","$FolderName","$keyword","$FTP","$file_To_Copy","$last_mod_date1"
var=`expr $var + 1`
done
Any help is greatly appreciated
thank you
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
No It didn't worked. I tried commenting the lines inside IF statement and it is working.
#!/bin/bash
var=1
var1=`awk '{n++} END {print n}' sales.txt`
while [[ ${var} -le ${var1} ]]
do
prod=$var
member=`grep "^$prod", sales.txt | cut -d "," -f2 | tr -d "\15"`
due1=`grep "^$prod", sales.txt | tail -1 | cut -d "," -f3 | tr -d "\15"`
FTP=`grep "^$prod", sales.txt | tail -1 | cut -d "," -f4 | tr -d "\15"`
FolderName=`grep "^$prod", sales.txt | tail -1 | cut -d "," -f5 | tr -d "\15"`
curdate=`date +%Y-%m-%d`
duedate1=$( date +%Y-%m-$due1 )
keyword=`grep "^$prod", sales.txt | tail -1 | cut -d "," -f7 | tr -d "\15"`
if [[ "$FTP" == "ABC" ]]
then
echo "$FolderName"
#cd /
#cd /archive/$FolderName
#file1_To_Copy=`ls -tr *"$keyword"* |tail -n 1`
#last_mod_date1=`ls --time-style=+%Y-%m-%d -ld $file1_To_Copy | awk '{print $6}'`
else
echo "do nothing"
fi
echo "$member","$due1","$FolderName","$keyword","$FTP","$file_To_Copy","$last_mod_date1"
var=`expr $var + 1`
done
ASKER
Also- the script.sh and sales.txt are located in the same folder.
Looking at your setup, you might benefit of converting into a perl script or using awk to extract different sets of data.
Using set a b c d
$1 $2 $3 $4 will have those values.
set $(grep "$prod," sales.txt | tail -1| tr -d "\15" | awk -F, ' { print $2,$3,$4,5,$7 } '
member=$1
duel=$2
FTP=$3
FolderName=$4
keyword=$5
The above example will eliminate the need to run grep multiple times.
Using set a b c d
$1 $2 $3 $4 will have those values.
set $(grep "$prod," sales.txt | tail -1| tr -d "\15" | awk -F, ' { print $2,$3,$4,5,$7 } '
member=$1
duel=$2
FTP=$3
FolderName=$4
keyword=$5
The above example will eliminate the need to run grep multiple times.
Your pattern is "$prod,". You're placing a comma outside which would cause an issue if the last character is a space.
ASKER
Thank you so much for that awk example.. I will use it.
Comma placed outside, sorry it was typo in my earlier comment.
I believe the problem here is in IF statement
If I comment the below lines it works good and gets the correct folder name. but if uncomment it errors out.
Comma placed outside, sorry it was typo in my earlier comment.
I believe the problem here is in IF statement
If I comment the below lines it works good and gets the correct folder name. but if uncomment it errors out.
if [[ "$FTP" == "ABC" ]]
then
echo "$FolderName"
#cd /
#cd /archive/$FolderName
..
To terminate an if you need a fi
if condition
Then
Do something
else
Do something else
fi
Try using /bin/test instead of [[ ]]
if ( /bin/test "$FTP" == 'ABC' )
then
..
The error is not related to if.
You should double check what the variables are being set to.
You may want to test ! -z "$FTP" -a "$FTP"=='ABC'
The first will check to make sure $FTP is not an empty string...
if condition
Then
Do something
else
Do something else
fi
Try using /bin/test instead of [[ ]]
if ( /bin/test "$FTP" == 'ABC' )
then
..
The error is not related to if.
You should double check what the variables are being set to.
You may want to test ! -z "$FTP" -a "$FTP"=='ABC'
The first will check to make sure $FTP is not an empty string...
ASKER
Arnold, Thank you
I will re design my script using awk and will let you know.
I will re design my script using awk and will let you know.
ASKER
hi @Arnold,
I tried with below modified script, unfortunately am getting SSH details, Host details.. things like that as output.
Any idea whats happening here?
I tried with below modified script, unfortunately am getting SSH details, Host details.. things like that as output.
Any idea whats happening here?
#!/bin/bash
var=1
var1=`awk '{n++} END {print n}' sales.txt`
while [[ ${var} -le ${var1} ]]
do
prod="$var"
set $(grep "$^prod", sales.txt | tail -1| tr -d "\15" | awk -F, ' { print $2,$3,$4,$5,$7 } ')
due1=$3
FTP=$4
FolderName=$5
keyword=$7
curdate=`date +%Y-%m-%d`
duedate1=$( date +%Y-%m-$due1 )
echo "$member","$due1" ",""$FolderName","$keyword","$FTP"
var=`expr $var + 1`
done
Look at the grep, you still have grep "$prod", sales.txt
If the grep returns no value/data, set returns the current environment parameters.
If the grep returns no value/data, set returns the current environment parameters.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you all for your speedy responses. It worked for me .. Thanks again for your continuous support
ASKER