AWK 输出格式和语法
我一直在尝试使用 AWK 整理输出,并且非常成功地完成了堆栈溢出的一些内容,直到我点击下面命令的最后一部分。
-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
输出如下所示:
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,
我希望它看起来像:
2010 Aug 28,11:51, Directory Tom,005,
2010 Aug 28,11:50, Directory Smith,004,
2010 Aug 28,11:46, Directory Jon,003,
我想从中删除“08”,有时不会丢失之前完成的排序。这将更改为下个月的 09 和 10 以下,我相信我可以使用 sed 来解决这个问题,但我不是这方面的专家。有人可以阐明我应该做什么来克服这个障碍吗?
我引用了这个问题来了解我需要做什么: 排序用awk输出,并格式化
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这部分到底想要完成什么?
我的意思是 1 美元,...
关于更新:
应该删除这个。
或者更通用:
What do want do accomplish exactly with this part?
I mean the $1, ...
Regarding the update:
should cut this out.
Or more generic:
您可以将 grep 合并到一个脚本中,并将所有 awks、sed 和 head 全部合并到一个
awk
脚本中。由于您只 grep 一个月和一年,因此不需要在find
中包含%Tm
,并且您实际上只按日期排序月份和时间。我假设$r
是年份。大约:
顺便说一句,您将如何执行您打算执行的操作:
您将显式打印第一个字段,而不是通过引用包含它。但是,使用我上面的建议,您不需要这样做。
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 thefind
and you're effectively only sorting by the day of month and time. I'm assuming$r
is the year.Approximately:
By the way, here's how you would do what you set out to do:
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.