LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris
User Name
Password
Solaris / OpenSolaris This forum is for the discussion of Solaris, OpenSolaris, OpenIndiana, and illumos.
General Sun, SunOS and Sparc related questions also go here. Any Solaris fork or distribution is welcome.

Notices


Reply
  Search this Thread
Old 09-22-2011, 07:55 AM   #1
Sha_unix
Member
 
Registered: Sep 2011
Posts: 46

Rep: Reputation: Disabled
Question variable defined in awk, not working. !!!


Hi,

i have to fetch files created on the same day, so i am writting a script .... here it goes.

dte=`date | cut -f3 -d" "` -- > this will take only date (exp:22)
ls -ltrh protrace* | awk '{if ($7 -eq $dte) print $9}' > /tmp/pro

in the next line i am trying to collect protrace* files created on the same day. But i am not able to see $dte resolved.

Please help.

Last edited by Sha_unix; 09-22-2011 at 07:59 AM. Reason: typo mistakes.
 
Old 09-22-2011, 08:08 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Try one of the following:
Code:
awk '{if ($7 -eq '$dte') print $9}'
or:
Code:
awk -v dte="${dte}" '{if ($7 -eq dte) print $9}'
Hope this helps.

PS: This dte=`date | cut -f3 -d" "` can be done easier: dte=`date '+%d'`
 
1 members found this post helpful.
Old 09-22-2011, 08:32 AM   #3
Sha_unix
Member
 
Registered: Sep 2011
Posts: 46

Original Poster
Rep: Reputation: Disabled
Thanks duuna,

code:
awk '{if ($7 -eq '$dte') print $9}'

Above code resovled "dte" variable but is taking all the files irrespective of the date.

code:
awk -v dte="${dte}" '{if ($7 -eq dte) print $9}'

above says syntax error.

Its better i clearly explain wat i am doing.. here it goes.

i am trying to collect protrace* files of the same day and trying to zip and move it to a folder....

DIR=/home/user/sha
dte=`date | awk '{print $3}'`
mnt=`date | awk '{print $2}'`
cd $DIR
mkdir $dte${mnt}2011_files
cd $dte${mnt}2011_files
mkdir pro stp

ls -ltrh protrace* | awk -v dte="${dte}" '{if ($7 -eq dte) print $9}' > /tmp/tpro
/usr/bin/mv -fi `cat /tmp/tpro` $DIR/$dte${mnt}2011_files/pro/
cd DIR/$dte${mnt}2011_files/pro/
zip -19q $dte${mnt}2011_files_protrace.zip *


HOpe you got an idea.. about my work.
 
Old 09-22-2011, 08:44 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

I already told you twice before: USE CODE TAGS!!!

Quote:
Originally Posted by Sha_unix View Post
Code:
DIR=/home/user/sha
dte=`date | awk '{print $3}'`
mnt=`date | awk '{print $2}'`
cd $DIR
mkdir $dte${mnt}2011_files
cd $dte${mnt}2011_files
mkdir pro stp

ls -ltrh protrace* | awk -v dte="${dte}" '{if ($7 -eq dte) print $9}' > /tmp/tpro
/usr/bin/mv -fi `cat /tmp/tpro` $DIR/$dte${mnt}2011_files/pro/
cd DIR/$dte${mnt}2011_files/pro/
zip -19q $dte${mnt}2011_files_protrace.zip *
Looking at the above I don't think you will get the results you are looking for.

After this step cd $dte${mnt}2011_files you are standing in /home/user/sha/22Sep2011_files. The ls -ltrh protrace* command is done from inside that directory. The result will always be zero files because you just created that directory.

You are probably missing part of the ls command: ls -ltrh /path/to/protrace*

Hope this helps.
 
Old 09-22-2011, 08:52 AM   #5
Sha_unix
Member
 
Registered: Sep 2011
Posts: 46

Original Poster
Rep: Reputation: Disabled
I just missed that cd /path/ while pasting. its going on right path creating right directories zipping everything is going well..just that its taking all files of protrace irrespective of date.

Hmmm i dont know how to use BB code exactly.. but as you told in my previous question gone through some doc and i believe i have to use that before i start my script ie.,
Code:
restofthescript
? isnt it. if this is not the method then i really have to learn first BBcode.

Please just make that awk command to take only today's file that would resolve my prob.
 
Old 09-22-2011, 09:09 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Yep, that is how code tags are used

This should do what you ask for:
Code:
ls -ltrh protrace* | awk -v mnt="${mnt}" -v dte="${dte}" '{if ($6 == mnt && $7 == dte ) print $9}'
I do have to warn you about using ls this way: The ls output is _not_ consistent when it comes to dates, you might get into trouble when looking for older files. Also: if you have files with spaces in them the above will also not work (instead of $9, you might need to print $9, $10 etc).

Try looking at the find command when time permits: find - examples

Hope this helps.
 
Old 09-22-2011, 09:34 AM   #7
Sha_unix
Member
 
Registered: Sep 2011
Posts: 46

Original Poster
Rep: Reputation: Disabled
Thanks for your smily reply

Now i wrote
Code:
 before writting "#!/bin/sh" and ended with
.

But error still exist. i even tried giving single quote to $6 == 'mnt' it also didnt work out.
quote:
ls -ltrh protrace* | awk -v mnt="${mnt}" -v dte="${dte}" '{if ($6 == mnt && $7 == dte ) print $9}'

output(error):

++ ls -ltrh protrace.old protrace.t1 protrace.t2 protrace.t3
++ awk -v mnt=Sep -v dte=22 '{if ($6 == mnt && $7 == dte) print $9}'
awk: syntax error near line 1
awk: bailing out near line 1

ls -ltrh protrace*
-rw-r--r-- 1 progress progress 22 Jun 1 08:28 protrace.old
-rw-r--r-- 1 progress progress 0 Sep 22 10:16 protrace.t1
-rw-r--r-- 1 progress progress 0 Sep 22 10:16 protrace.t2
-rw-r--r-- 1 progress progress 0 Sep 22 10:16 protrace.t3

It suppose to pick only protrace.t1 protrace.t2 protrace.t3 only.
 
Old 09-22-2011, 09:59 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Can you post the complete script you are using (without the typo's).
 
Old 09-22-2011, 10:04 AM   #9
Sha_unix
Member
 
Registered: Sep 2011
Posts: 46

Original Poster
Rep: Reputation: Disabled
Here it is ...

Code:
#!/bin/sh
#to check date and month cut
set -x
dte=`date '+%d'`
mnt=`date '+%b'`

DIR=/export/home/progress/sha
cd $DIR
mkdir $dte${mnt}2011_files
cd $dte${mnt}2011_files
mkdir pro stp
cd $DIR

ls -ltrh protrace* | awk -v mnt="${mnt}" -v dte="${dte}" '{if ($6 == mnt && $7 == dte) print $9}' > /tmp/tpro
/usr/bin/mv -fi `cat /tmp/tpro` $DIR/$dte${mnt}2011_files/pro/
cd DIR/$dte${mnt}2011_files/pro/
zip -19q $dte${mnt}2011_files_protrace.zip *
sleep 10
mv *.zip ../
 
Old 09-22-2011, 10:22 AM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Maybe the awk line isn't accepted by SUN's awk, try this:
Code:
ls -l protrace* | awk '{ if ( $6 == '$mnt' && $7 == '$dte' ) print $9 }' > /tmp/tpro
BTW1: ls -ltrh -> then trh part isn't needed (especially the r part), ls -l will do.
BTW2: cd DIR/$dte${mnt}2011_files/pro/ should be cd $DIR/$dte${mnt}2011_files/pro/

Hope this helps.
 
Old 09-22-2011, 03:43 PM   #11
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
On Solaris 10 and older, avoid to use /usr/bin/awk which is only there not to break compatibility with legacy scripts that might stay around, use /usr/bin/nawk or /usr/xpg4/bin/awk instead.
 
Old 09-23-2011, 01:44 AM   #12
Sha_unix
Member
 
Registered: Sep 2011
Posts: 46

Original Poster
Rep: Reputation: Disabled
HI,

The $dte is resolved. But only the prob exists is its taking all the protrace files irrespective of the date.
 
Old 09-23-2011, 02:51 AM   #13
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,
Quote:
Originally Posted by Sha_unix View Post
The $dte is resolved. But only the prob exists is its taking all the protrace files irrespective of the date.
I have to assume that both dte _and_ mnt work and that you changed the ls -ltrh to ls -l as I suggested (did you?)

I've recreated the environment as mentioned in the script and tested it: It works on my side (up to the cd DIR/$dte${mnt}2011_files/pro/ part).

Pleae post the output of the following (bold) commands:
Code:
$ cd /export/home/progress/sha
$ which awk
$ ls -l protrace*
$ dte=`date '+%d'`
$ mnt=`date '+%b'`
$ set -x
$ ls -l protrace* | awk '{ if ( $6 == "'$mnt'" && $7 == "'$dte'" ) print $9 }'
$ set +x
 
1 members found this post helpful.
Old 09-23-2011, 04:54 AM   #14
Sha_unix
Member
 
Registered: Sep 2011
Posts: 46

Original Poster
Rep: Reputation: Disabled
which awk:
/bin/awk

ls -l protrace*
-rw-r--r-- 1 progress progress 0 Sep 23 03:14 protrace.n1
-rw-r--r-- 1 progress progress 0 Sep 23 03:14 protrace.n2
-rw-r--r-- 1 progress progress 0 Sep 23 03:14 protrace.n3
-rwxr-xr-x 1 progress progress 163 Aug 23 07:12 protrace.old

ls -l protrace* | awk '{ if ( $6 == "'$mnt'" && $7 == "'$dte'" ) print $9 }'
protrace.n1
protrace.n2
protrace.n3

NOw its working.. The difference was "" (double quotes for $mnt and $dte Earlier code:
ls -l protrace* | awk '{ if ( $6 == '$mnt' && $7 == '$dte' ) print $9 }' > /tmp/tpro

Present code:
ls -l protrace* | awk '{ if ( $6 == "'$mnt'" && $7 == "'$dte'" ) print $9 }'
 
Old 09-23-2011, 04:57 AM   #15
Sha_unix
Member
 
Registered: Sep 2011
Posts: 46

Original Poster
Rep: Reputation: Disabled
Lightbulb

Quote:
Originally Posted by Sha_unix View Post
which awk:
/bin/awk

ls -l protrace*
-rw-r--r-- 1 progress progress 0 Sep 23 03:14 protrace.n1
-rw-r--r-- 1 progress progress 0 Sep 23 03:14 protrace.n2
-rw-r--r-- 1 progress progress 0 Sep 23 03:14 protrace.n3
-rwxr-xr-x 1 progress progress 163 Aug 23 07:12 protrace.old

ls -l protrace* | awk '{ if ( $6 == "'$mnt'" && $7 == "'$dte'" ) print $9 }'
protrace.n1
protrace.n2
protrace.n3

NOw its working.. The difference was "" (double quotes for $mnt and $dte Earlier code:
ls -l protrace* | awk '{ if ( $6 == '$mnt' && $7 == '$dte' ) print $9 }' > /tmp/tpro

Present code:
ls -l protrace* | awk '{ if ( $6 == "'$mnt'" && $7 == "'$dte'" ) print $9 }'
Thank you very much for your time and effort.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
problem while comparing awk field variable with input variable entered using keyboard vinay007 Programming 12 08-23-2011 12:44 AM
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 02:18 AM
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 12:08 AM
How to acess Variable defined in perl script inside an awk call sumin Programming 3 04-26-2007 05:19 AM
Replace variable with user defined variable ce124 Programming 10 04-13-2007 09:29 AM

LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris

All times are GMT -5. The time now is 12:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration