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. |
Hi,
Try one of the following: Code:
awk '{if ($7 -eq '$dte') print $9}' Code:
awk -v dte="${dte}" '{if ($7 -eq dte) print $9}' PS: This dte=`date | cut -f3 -d" "` can be done easier: dte=`date '+%d'` |
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. |
Hi,
I already told you twice before: USE CODE TAGS!!! Quote:
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. |
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 Please just make that awk command to take only today's file that would resolve my prob. :( |
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}' Try looking at the find command when time permits: find - examples Hope this helps. |
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. |
Hi,
Can you post the complete script you are using (without the typo's). |
Here it is ...
Code:
#!/bin/sh |
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 BTW2: cd DIR/$dte${mnt}2011_files/pro/ should be cd $DIR/$dte${mnt}2011_files/pro/ Hope this helps. |
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.
|
HI,
The $dte is resolved. But only the prob exists is its taking all the protrace files irrespective of the date. :( |
Hi,
Quote:
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:
/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 }' |
Quote:
|
All times are GMT -5. The time now is 04:57 PM. |