1

Ive been trying to sort out output using AWK, and have been pretty successful going through some of the stuff on stack overflow until i hit the last part of the command below.

-bash-3.2$ find /home/username/www/devdir -mindepth 2 -maxdepth 2 -type d -printf "%TY %Tm %Tb %Td,%TH:%TM,%p,\n" | grep "^$r" | grep Aug | sort -r | awk -F '/' '{print $1,$6","$7}' | awk -F " " '$1, { for (i=3; i<=NF; i++) printf("%s ", $i); printf("\n"); }' | head -10
awk: $1, { for (i=3; i<=NF; i++) printf("%s ", $i); printf("\n"); }
awk:     ^ syntax error

The output looks like the below:

2010 08 Aug 28,11:51, Directory Tom,005,
2010 08 Aug 28,11:50, Directory Smith,004,
2010 08 Aug 28,11:46, Directory Jon,003,

I want it to look like:

2010 Aug 28,11:51, Directory Tom,005,
2010 Aug 28,11:50, Directory Smith,004,
2010 Aug 28,11:46, Directory Jon,003,

I woud like to cut the "08" out of it, and sometimes without losing the sorting done earlier. This will change to 09 next month and 10 the following, I believe I can use sed to solve this, however I am not an expert with it. Can someone shed some light as to what I should do to overcome this obstacle?

I've referenced this question to get an idea of what I needed to do: Sorting output with awk, and formatting it

2 Answers 2

1

What do want do accomplish exactly with this part?

awk -F " " '$1, { }'

I mean the $1, ...

Regarding the update:

sed 's/^\([0-9]\+\) 08 \(.\+\)$/\1 \2/'

should cut this out.

Or more generic:

sed 's/^\([0-9]\+\) [0-9][0-9] \(.\+\)$/\1 \2/'
Sign up to request clarification or add additional context in comments.

2 Comments

I want to keep the year (2010).
I updated my question to clarify how I wanted the output to look like.
1

You can combine your greps into one and all your awks, seds and head all into one awk script. Since you're grepping only one month and one year, you don't need to include the %Tm in the find and you're effectively only sorting by the day of month and time. I'm assuming $r is the year.

Approximately:

find /home/username/www/devdir -mindepth 2 -maxdepth 2 -type d -printf "%TY %Tb %Td,%TH:%TM,%p,\n" | grep "^$r.*Aug" | sort -r | awk -F '/' 'NR<=10{print $1,$6","$7}'

By the way, here's how you would do what you set out to do:

printf($1" ");for (i=3; i<=NF; i++) printf("%s ", $i); printf("\n")

You would print the first field explicitly rather than include it by reference. However, using my suggestions above you shouldn't need to do this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.