Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
cancel
Showing results for 
Search instead for 
Did you mean: 

awk issue

SOLVED
Go to solution
NDO
Super Advisor

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.
12 REPLIES
Victor Fridyev
Honored Contributor

Re: awk issue

ls -lrt | awk '{if($7 == "1")if ($7==1) print $9}'
Entities are not to be multiplied beyond necessity - RTFM
NDO
Super Advisor

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.
Victor Fridyev
Honored Contributor

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
NDO
Super Advisor

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.
Hein van den Heuvel
Honored Contributor

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
Victor Fridyev
Honored Contributor

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
James R. Ferguson
Acclaimed Contributor

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

Dennis Handly
Acclaimed Contributor
Solution

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
}'
NDO
Super Advisor

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.
Shibin_2
Honored Contributor

Re: awk issue

try this.

... | awk '
{
if ($6 == "Jan" && $7 == 1 && $8 != 2010)
print $9
}'
Regards
Shibin
NDO
Super Advisor

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.
NDO
Super Advisor

Re: awk issue

Thank you for your contributions. Already assign points

F.R.