LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Solaris / OpenSolaris (https://www.linuxquestions.org/questions/solaris-opensolaris-20/)
-   -   variable defined in awk, not working. !!! (https://www.linuxquestions.org/questions/solaris-opensolaris-20/variable-defined-in-awk-not-working-904416/)

Sha_unix 09-22-2011 07:55 AM

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.

druuna 09-22-2011 08:08 AM

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'`

Sha_unix 09-22-2011 08:32 AM

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.

druuna 09-22-2011 08:44 AM

Hi,

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

Quote:

Originally Posted by Sha_unix (Post 4479146)
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.

Sha_unix 09-22-2011 08:52 AM

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. :(

druuna 09-22-2011 09:09 AM

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.

Sha_unix 09-22-2011 09:34 AM

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.

druuna 09-22-2011 09:59 AM

Hi,

Can you post the complete script you are using (without the typo's).

Sha_unix 09-22-2011 10:04 AM

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 ../


druuna 09-22-2011 10:22 AM

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.

jlliagre 09-22-2011 03:43 PM

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.

Sha_unix 09-23-2011 01:44 AM

HI,

The $dte is resolved. But only the prob exists is its taking all the protrace files irrespective of the date. :(

druuna 09-23-2011 02:51 AM

Hi,
Quote:

Originally Posted by Sha_unix (Post 4479885)
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


Sha_unix 09-23-2011 04:54 AM

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 }'

Sha_unix 09-23-2011 04:57 AM

Quote:

Originally Posted by Sha_unix (Post 4480002)
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. :)


All times are GMT -5. The time now is 04:57 PM.