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开发者_StackOverflow中文版 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
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/'
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.
精彩评论