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:
"grep: sales.txt: No such file or directory"

Open in new window


#!/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

Open in new window


Any help is greatly appreciated
thank you
parpaaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arnoldCommented:
You need to out $prod, into "" to make sure if there are spaces it will be passed as a pattern to grep as opposed to separating.

Depending on where/under what conditions this script is run, you would be benefit from suffering by using full paths /bin/grep or /usr/bin/grep
And make sure you use the full path to where sales.txt can be found.  In a relative as you have, the script must e run while pwd is the directory where sales.txt is.
0
parpaaAuthor Commented:
Thanks @arnold, I will try that.
0
parpaaAuthor Commented:
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

Open in new window

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

parpaaAuthor Commented:
Also- the script.sh and sales.txt are located in the same folder.
0
arnoldCommented:
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.
0
arnoldCommented:
Your pattern is "$prod,". You're placing a comma outside which would cause an issue if the last character is a space.
0
parpaaAuthor Commented:
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.
if [[ "$FTP" == "ABC" ]]
then
echo "$FolderName"

#cd /
#cd /archive/$FolderName

..

Open in new window

0
arnoldCommented:
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...
0
parpaaAuthor Commented:
Arnold, Thank you

I will re design my script using awk  and will let you know.
0
parpaaAuthor Commented:
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?
#!/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

Open in new window

0
arnoldCommented:
Look at the grep, you still have grep "$prod", sales.txt

If the grep returns no value/data, set returns the current environment parameters.
0
simon3270Commented:
"$prod", and "$prod," will work the same way - there's no whitepace between the "d" of prod and the comma, so in both cases, the shell will treat them as a single argument.

The ^ in "$^prod", is misplaced - it should be "^$prod",

Also, is the first field in the input file really the line number?

Couple of other changes:
- the "awk" script to get $var1 is too complex - just do
    var1=`wc -l < sales.txt`

- is $due1 always in the same month as the current month?  If not, the duedate1 calculation may fail.

- The reason for the inital problem (sales.txt not found) was the "cd" command in the "if" test - you moved away from the directory that sales.txt was in, and didn't move back.  You shoudl remember your original location and go back to it, so have
    OLDPWD=`pwd`
before the while loop, and
    cd "$OLDPWD"
after you have dealt with the file in /archive/$FolderName
(an alternative, enclosing the commands in parentheses so that they run in a subshell won't be much use, because any variables you set in that subshell wil be lost when you go back to the main shell, and the aim of the code is to set variables!)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
parpaaAuthor Commented:
Thank you all for your speedy responses. It worked for me .. Thanks again for your continuous support
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.