gpt4 book ai didi

linux - 将内容文件过滤到表格

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:51:13 24 4
gpt4 key购买 nike

这是我生成的输入,显示了 Jany 和 Marco 在不同时间的类(class)版本。

on 10:00 the course of jany 1 is :
course:theory:nothing
course:applicaton:onehour

on 10:00 the course of jany 2 is :
course:theory:math
course:applicaton:twohour

on 10:00 the course of Marco 1 is :
course:theory:geo
course:applicaton:halfhour

on 10:00 the course of Marco 2 is :
course:theory:history
course:applicaton:nothing

on 14:00 the course of jany 1 is :
course:theory:nothing
course:applicaton:twohours

on 14:00 the course of jany 2 is :
course:theory:music
course:applicaton:twohours

on 14:00 the course of Marco 1 is :
course:theory:programmation
course:applicaton:onehours

on 14:00 the course of Marco 2 is :
course:theory:philosophy
course:applicaton:nothing

使用 awk 命令我成功地对其进行了排序:

awk -F '[\ :]' '/the course of/{h=$2;m=$3} /theory/{print " "h":"m" theory:"$3}' f.txt
awk -F '[\ :]' '/the course of/{h=$2;m=$3} /application/{print " "h":"m" application :"$3}' f.txt
10:00 theory:nothing
14:00 theory:nothing

10:00 application:onehour
14:00 application:twohours

现在我想通过添加名称(jany、Marco)和版本(1 或 2)来改进过滤器,如下所示。

Jany 1,10:00,14:00
theory,nothing,nothing
application,onehour,twohour

Jany 2,10:00,14:00
theory,math,music
application,twohour,twohour

Marco 1,10:00,14:00
theory,geo,programmation
application,halfhour,onehour

Marco 2,10:00,14:00
theory,history,philosoohy
application,nothing,nothing

我一直纠结于如何提取“名称、编号”并在经过排序和过滤的表格中获取与他们的类(class)相关的信息。

最佳答案

使用 GNU awk 实现真正的多维数组和 sorted_in:

$ cat tst.awk
BEGIN{ RS=""; FS="[[:space:]:]+" }
{
for (i=11; i<=NF; i+=3) {
sched[$7" "$8][$2":"$3][$i] = $(i+1)
courses[$i]
}
}
END {
PROCINFO["sorted_in"] = "@ind_str_asc"
for (name in sched) {
printf "%s", name
for (time in sched[name]) {
printf ",%s", time
}
print ""
for (course in courses) {
printf "%s", course
for (time in sched[name]) {
printf ",%s", sched[name][time][course]
}
print ""
}
print ""
}
}

.

$ gawk -f tst.awk file
Marco 1,10:00,14:00
applicaton,halfhour,onehours
theory,geo,programmation

Marco 2,10:00,14:00
applicaton,nothing,nothing
theory,history,philosophy

jany 1,10:00,14:00
applicaton,onehour,twohours
theory,nothing,nothing

jany 2,10:00,14:00
applicaton,twohour,twohours
theory,math,music

它并没有完全产生您发布的预期输出,但我认为那是因为您发布的预期输出是错误的(例如,与您的输入相比,检查 jany 1 应用程序 14:00 的输出 - 输入是 twohours 就像我的脚本生成的一样,但你说预期的输出是 halfhour)。

关于linux - 将内容文件过滤到表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31070972/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com