Ubuntu Awk脚本不';我跑不动,好像我';我跳过了括号,但我';我数过了,他们的数目是偶数
我正在ubuntu终端上写一个awk脚本。但它并没有开始起作用。似乎我忽略了这些符号中的一些:“,”,(,),{,},但我已经计算了这些符号的数量,它们的数量是偶数。 所以我真的不明白这些脚本哪里失败了Ubuntu Awk脚本不';我跑不动,好像我';我跳过了括号,但我';我数过了,他们的数目是偶数,ubuntu,awk,Ubuntu,Awk,我正在ubuntu终端上写一个awk脚本。但它并没有开始起作用。似乎我忽略了这些符号中的一些:“,”,(,),{,},但我已经计算了这些符号的数量,它们的数量是偶数。 所以我真的不明白这些脚本哪里失败了 (前言,我写了cal>database/calendar.txt) scipt的伪代码便于理解脚本本身: //首先,我从系统中获取当前日期和当前星期日 //然后我将calendar.txt设为一行,字段之间有一个空格(用awk术语) //然后,我首先查找当前工作日(星期一等) //然后我找到当
(前言,我写了cal>database/calendar.txt)
scipt的伪代码便于理解脚本本身: //首先,我从系统中获取当前日期和当前星期日
//然后我将calendar.txt设为一行,字段之间有一个空格(用awk术语)
//然后,我首先查找当前工作日(星期一等)
//然后我找到当前日期(16、17等)
//毕竟,我将发现的值等同于变量(结果日和结果周日)
毕竟,我正试图启动它,不幸的是,它不起作用 根本并没有输出,脚本只是在等待进一步的命令输入,就像我忽略了括号之类的东西 脚本:
awk -v day_searched="$(date +%"d")" -v week_day_searched="$(date +"%a")"
'BEGIN{FS=" ";RS="NOT_EXISTING_dELIMITER"} {
current_field = 0
resulted_day = "not_found_yet"
resulted_week_day= "not_found_yet"
max_file_lenght = 100
while (current_field < max_file_lenght) {
if ($(current_field) == week_day_searched) {
resulted_week_day = $(current_field)
}
current_field++
}
current_field_second = 0
while (current_field_second < max_file_lenght) {
if ($(current_field_second) == day_searched) {
resulted_day = $(current_field_second) }
curent_field_second++
}
printf "TOday is %s %s March", resulted_week_day, resulted_day}' database/calendar.txt
awk-v day_searched=“$(日期+%d”)”-v week_day_searched=“$(日期+%a”)”
'BEGIN{FS=”“;RS=“非现有分隔符”}{
当前_字段=0
结果\u day=“尚未找到”
结果周日=“尚未找到”
最大文件长度=100
while(当前字段<最大文件长度){
如果($(当前字段)=搜索的周日){
结果\周\日=$(当前\字段)
}
电流场++
}
当前\u字段\u秒=0
while(当前字段秒<最大文件长度){
如果($(当前字段\u秒)=搜索日期){
结果\u天=$(当前\u字段\u秒)}
电流场秒++
}
printf“今天是%s%s三月”,resulted\u week\u day,resulted\u day}数据库/calendar.txt
使用awk作为基础:
BEGIN {
FS=" "
RS="" # read upto an empty record
}
{
gsub(/[^A-Za-z0-9 ]/,"") # remove special chars reading file
current_field = 1 # start from 1
resulted_day = "not_found_yet"
resulted_week_day= "not_found_yet"
# max_file_lenght = 100 # use NF instead
week_day_searched=substr(week_day_searched,1,2) # take only 2 first chars of weekday
while (current_field <= NF) {
if ($current_field ==week_day_searched) {
resulted_week_day = $current_field
}
current_field++
}
current_field_second = 1 # start from 1
while (current_field_second <= NF) {
if ($current_field_second == day_searched) {
resulted_day = $current_field_second }
current_field_second++ # fix typo
}
printf "TOday is %s %s March\n", resulted_week_day, resulted_day
}
cal
产生特殊字符(至少对我来说,当前日期突出显示,请查看下面的第18条):
我看到一些打字错误(
current\u field\u second++
)和一些可能错误的地方(current\u field=0…如果($(current\u field)==week\u day\u searched)…current\u field++
)。你知道在awk中,$0
是什么意思,对吗?修复该输入错误至少会使程序停止。POSIX awkRS=“NOT_EXISTING\u dELIMITER”
中的FYI将RS
设置为字符N
。如果您使用的awk支持多字符RS,则可以通过RS=“^$”
将RS设置为输入中不存在的值。如果不是,则RS=“\0”
可能足够接近。此外,date+%a“
输出Fri
和cal
Fr
。这将永远不会匹配:如果($(当前字段秒)=搜索的天数)
为cal
可能会为今天的日期加上颜色,如果您使用hextdump-C
或类似的颜色查看它,您将看到:5f0831 5f0837…\。1.7
“根本没有输出,它只是让我输入更多的命令,比如,如果我忘记了一个括号”——awk代码中缺少一个括号将是一个语法错误,并将导致来自awk的错误消息。
$ awk -v day_searched="$(date +"%d")" -v week_day_searched="$(date +"%a")" -f program.awk file
TOday is Sa 18 March
$ cal | hexdump -C
00000000 20 20 20 20 20 41 70 72 69 6c 20 32 30 32 30 20 | April 2020 |
00000010 20 20 20 20 20 20 0a 53 75 20 4d 6f 20 54 75 20 | .Su Mo Tu |
00000020 57 65 20 54 68 20 46 72 20 53 61 20 20 0a 20 20 |We Th Fr Sa . |
00000030 20 20 20 20 20 20 20 20 31 20 20 32 20 20 33 20 | 1 2 3 |
00000040 20 34 20 20 0a 20 35 20 20 36 20 20 37 20 20 38 | 4 . 5 6 7 8|
00000050 20 20 39 20 31 30 20 31 31 20 20 0a 31 32 20 31 | 9 10 11 .12 1|
00000060 33 20 31 34 20 31 35 20 31 36 20 31 37 20 5f 08 |3 14 15 16 17 _.|
00000070 31 5f 08 38 20 20 0a 31 39 20 32 30 20 32 31 20 |1_.8 .19 20 21 |
00000080 32 32 20 32 33 20 32 34 20 32 35 20 20 0a 32 36 |22 23 24 25 .26|
00000090 20 32 37 20 32 38 20 32 39 20 33 30 20 20 20 20 | 27 28 29 30 |
000000a0 20 20 20 20 0a 20 20 20 20 20 20 20 20 20 20 20 | . |
000000b0 20 20 20 20 20 20 20 20 20 20 20 0a | .|
000000bc