AWK 输出格式和语法

发布于 2024-09-16 20:34:29 字数 1032 浏览 1 评论 0原文

我一直在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦一生花开无言 2024-09-23 20:34:29

这部分到底想要完成什么?

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

我的意思是 1 美元,...

关于更新:

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

应该删除这个。

或者更通用:

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

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/'
执着的年纪 2024-09-23 20:34:29

您可以将 grep 合并到一个脚本中,并将所有 awks、sed 和 head 全部合并到一个 awk 脚本中。由于您只 grep 一个月和一年,因此不需要在 find 中包含 %Tm,并且您实际上只按日期排序月份和时间。我假设 $r 是年份。

大约:

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}'

顺便说一句,您将如何执行您打算执行的操作:

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

您将显式打印第一个字段,而不是通过引用包含它。但是,使用我上面的建议,您不需要这样做。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文