awk issue (271 Views)
Reply
Super Advisor
NDO
Posts: 533
Registered: ‎12-10-2003
Message 1 of 13 (271 Views)
Accepted Solution

awk issue

Hi All

I´m not very familiar with scripting, Í am still learning.
Please can someone tell me what could be wrong with that command:
root@nrtrde1 # ls -lrt | awk '{if ($6 == "Jan") && ($7 == "1")print $9}'
awk: syntax error near line 1
awk: illegal statement near line 1

The idea is to list all files of 1st of january 2011. If I am able to do this I could write a script to transfer files from january 2011.
Your help is appreciated


F.R.
Please use plain text.
Honored Contributor
Victor Fridyev
Posts: 937
Registered: ‎07-01-1998
Message 2 of 13 (271 Views)

Re: awk issue

ls -lrt | awk '{if($7 == "1")if ($7==1) print $9}'
Entities are not to be multiplied beyond necessity - RTFM
Please use plain text.
Super Advisor
NDO
Posts: 533
Registered: ‎12-10-2003
Message 3 of 13 (271 Views)

Re: awk issue


>>ls -lrt | awk '{if($7 == "1")if ($7==1) print $9}'

Sorry the first if shouldn´t be "Jan" as opposed to what you wrote which is "1"?


F.R.
Please use plain text.
Honored Contributor
Victor Fridyev
Posts: 937
Registered: ‎07-01-1998
Message 4 of 13 (271 Views)

Re: awk issue

Sorry, 8(((
ls -lrt | awk '{if($6 == "Jan")if ($7==1) print $9}'
Entities are not to be multiplied beyond necessity - RTFM
Please use plain text.
Super Advisor
NDO
Posts: 533
Registered: ‎12-10-2003
Message 5 of 13 (271 Views)

Re: awk issue

Hi

It does work now, but how if I want to exclude from that list all january 2010 files, because now is listing me all january 1st 2010 files, but what I want is January 2011.


F.R.
Please use plain text.
Honored Contributor
Hein van den Heuvel
Posts: 6,585
Registered: ‎05-19-2003
Message 6 of 13 (271 Views)

Re: awk issue

It is still not 'proper' awk.

Proper awk IMHO is a series of conditions follows by code blocks.

The example unconditionally goes into a codeblock and then starts a condition.

Try this: $ ls -ltr | awk '($6=="Jan") && ($7==1) {print $9}'

Not for the real problem you do NOT want to use awk.
You want to use FIND.

Check out the -ctime option, or the usage of 'reference files' through the -newer and -older options.
Google and the MAN PAGES are your friend in this learning exercise.

Cheers,
Hein
Please use plain text.
Honored Contributor
Victor Fridyev
Posts: 937
Registered: ‎07-01-1998
Message 7 of 13 (271 Views)

Re: awk issue

So please define your request properly.
If you need some date filter, use find:
find /home/usersname -type f -mtime +5 -exec ls -ls {} \;
or something similar.

Good luck !
Entities are not to be multiplied beyond necessity - RTFM
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 8 of 13 (271 Views)

Re: awk issue

Hi:

# touch -amt 201012312359.59 /tmp/ref1
# touch -amt 201101012359.59 /tmp/ref2
# find . -type f -newer /tmp/ref1 -a ! -newer /tmp/ref2

...will list the files *last_modified* on January 1, 2011

See the manpages for 'touch(1)' and for 'find(1)'.

Regards!

...JRF...

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,901
Registered: ‎03-06-2006
Message 9 of 13 (271 Views)

Re: awk issue

>Hein: Proper awk IMHO is a series of conditions follow by code blocks.

True but you can do C style programming:
... | awk '
{
if ($6 == "Jan" && $7 == 1)
print $9
}'
Please use plain text.
Super Advisor
NDO
Posts: 533
Registered: ‎12-10-2003
Message 10 of 13 (271 Views)

Re: awk issue

Hi
Dennis:

Your suggestion give me this:
-rw-r--r-- 1 taprap other 60414 Jan 1 2010 CDNAM01MOZ0105105
-rw-r--r-- 1 taprap other 123 Jan 1 2010 CDMWICPMOZ0103249
-rw-r--r-- 1 taprap other 1698 Jan 1 2010 CDISRCLMOZ0103103
-rw-r--r-- 1 taprap other 129 Jan 1 01:34 CDINDA9MOZ0101336
-rw-r--r-- 1 taprap other 3516 Jan 1 01:34 CDZMBCEMOZ0103029
-rw-r--r-- 1 taprap other 129 Jan 1 01:35 CDINDJBMOZ0102144
-rw-r--r-- 1 taprap other 647 Jan 1 01:35 CDINDA2MOZ0101581
-rw-r--r-- 1 taprap other 123 Jan 1 01:36 CDGRCPFMOZ0105837
-rw-r--r-- 1 taprap other 123 Jan 1 01:36 CDPRYHTMOZ0101023

BUT I do not want the 2010 files, I just want the other files. How can I get rid of the Jan 1 2010 files?


F.R.
Please use plain text.
Honored Contributor
Shibin_2
Posts: 711
Registered: ‎11-11-2005
Message 11 of 13 (271 Views)

Re: awk issue

try this.

... | awk '
{
if ($6 == "Jan" && $7 == 1 && $8 != 2010)
print $9
}'
Regards
Shibin
Please use plain text.
Super Advisor
NDO
Posts: 533
Registered: ‎12-10-2003
Message 12 of 13 (271 Views)

Re: awk issue

Hi
Shibin!

Your suggestion worked perfectely, I havwe modified slightly to list all January files of current year as this:

ls -lrt | awk '{if ($6 == "Jan" && $8 != 2010)print $0}'


F.R.
Please use plain text.
Super Advisor
NDO
Posts: 533
Registered: ‎12-10-2003
Message 13 of 13 (271 Views)

Re: awk issue

Thank you for your contributions. Already assign points

F.R.
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation