我正在运行Ubuntu 8.04,我的代码看起来像这样......
for (i=1;i<=n;i++)
{
if (arr[i] ~ /^[A-Z]{2,4}$/) printf(arr[i])
}
Run Code Online (Sandbox Code Playgroud)
我很快发现没有--posix开关,{n}表达式在gawk中不起作用.一旦启用,表达式就可以工作,但是它与caseA insenitive匹配AAAA和aaaa.这里发生了什么?
时间跟踪工具打卡生成一个时间日志,其中包含"yyyymmddThhmmss"中的任务和时间戳:
task started ended
---------------------------------------
task1 20121107T114348 20121107T120921
task2 20121107T121349 20121107T121430
task2 20121107T121658 20121107T124255
task1 20121107T140935 20121107T144153
task2 20121107T163449 20121107T180431
Run Code Online (Sandbox Code Playgroud)
我如何计算正则表达式中指定的每个任务所花费的总小时数和分钟数?例如,为了增加在包含的任务上花费的时间my-regex,awk或者gawk命令将是这样的:
awk '/my-regex/ { summing-of-corresponding-timestamps }' logfile.log
Run Code Online (Sandbox Code Playgroud)
这是我之前的问题的补充- 我意识到我需要一个更"强大",基于正则表达式的解决方案.但我不是编码员,所以在AWK中总结分钟和小时让我感到非常困惑.谢谢你的帮助!
我经常看到人们将"awk"称为编程语言.根据我的直觉,我对awk的理解是它是一个用于某些特定任务的工具.
那么为什么有些人会把它称为编程语言呢?
我有以下类型的数据:
3869|Jennifer Smith
10413 NE 71st Street
Vancouver, WA
98662
360-944-9578
jsmith@yahoo.com|1234567890123456|03-2013|123
--
3875|Joan L Doe
422 1/2 14th Ave E
Seattle, WA
98112
206-322-7666
jldoe@comcast.net|1234-1234-1234-1234|03-2013|123
--
3862|Dana Doe
24235 NE 7th Pl
Sammamish, WA
98074
425 868-2227
jsmith@hotmail.com|1234567890123456|03-2013|123
--
3890|John Smith
10470 SW 67th Ave
Tigard, OR
97223
5032205213
john.smith@gmail.com|1234567890123456|03-2013|123
Run Code Online (Sandbox Code Playgroud)
我需要将其转换为:
3869|Jennifer Smith|10413 NE 71st Street|Vancouver, WA|98662|360-944-9578|jsmith@yahoo.com|1234567890123456|03-2013|123
3875|Joan L Doe|422 1/2 14th Ave E|Seattle, WA|98112|206-322-7666|jldoe@comcast.net|1234-1234-1234-1234|03-2013|123
3862|Dana Doe|24235 NE 7th Pl|Sammamish, WA|98074|425 868-2227|jsmith@hotmail.com|1234567890123456|03-2013|123
3890|John Smith|10470 SW 67th Ave|Tigard, OR|97223|5032205213|john.smith@gmail.com|1234567890123456|03-2013|123
Run Code Online (Sandbox Code Playgroud)
或更好: …
我有一个文件:
fakdfal
14867
kgjafdfrf
8423685
Run Code Online (Sandbox Code Playgroud)
我想得到结果:
fakd.fal
14.867
kgjafd.frf
8423.685
Run Code Online (Sandbox Code Playgroud) 我在命令行了解perl,请帮帮我
perl中的等价物
awk '{for(i=1;i<=NF;i++)printf i < NF ? $i OFS : $i RS}' file
awk '!x[$0]++' file
awk 'FNR==NR{A[$0];next}($0 in A)' file1 file2
awk 'FNR==NR{A[$1]=$5 OFS $6;next}($1 in A){print $0,A[$1];delete A[$1]}' file1 file1
请有人帮帮我......
有一个主文件(Master.txt),其中每一行都是一个定义HTML页面的字符串,每个字段都以制表符分隔.记录布局如下:
<item_ID> <field_1> <field_2> <field_3>
1 1.html <html>[content for 1.html in HTML format]</html> <EOF>
2 2.html <html>[content for 2.html in HTML format]</html> <EOF>
3 3.html <html>[content for 3.html in HTML format]</html> <EOF>
Run Code Online (Sandbox Code Playgroud)
HTML页面定义于<field_2>.<field_3>可能没有必要,但此处包含以指示end_of_file的逻辑位置.
如何使用awk为<item_ID>新文件的内容和新文件<field_2>的名称所在的每一行(以其开头)生成文件<field_1>?
正在运行GNUwin32下Windows 7,将配置一个awk解决方案的执行.bat文件.不幸的是不能在Windows中做管道衬里,所以希望有一个单awk程序解决方案.
TY提前.
正在运行GnuWin32下Windows 7.拥有这种结构的文件:
|<text_0>
<text_1>
<text_2>
until
<text_16>
|<text_0>
<text_1>
<text_2>
until
<text_12>
|<text_0>
<text_1>
<text_2>
until
<text_31>
< more of the same >
Run Code Online (Sandbox Code Playgroud)
行之间存在可变数量的行,以pipe(分隔符号)开头.
期望的输出:
|<text_0><text_1><text_2> until <text_16>
|<text_0><text_1><text_2> until <text_12>
|<text_0><text_1><text_2> until <text_31>
Run Code Online (Sandbox Code Playgroud)
在Windows(因此双引号)尝试过(来自aypal singh和Ed Morton)
awk "{ ORS = (NR%2 ? FS : RS) } 1" < in.txt > out.txt
Run Code Online (Sandbox Code Playgroud)
但是,如果该行以a开头,则不会"跳过"向前一行附加一行pipe.
如何修改awk程序以将所有行附加到上一行,直到awk遇到记录分隔符pipe(并继续处理直到文件末尾)?
我想出了下面的脚本来列出超过10000000微秒的呼叫(我们的日志中的ServiceDuration以微秒记录的值),从服务器日志文件中存储所有来到服务的呼叫.
servicedurationlimit.awk
#!/bin/sh
dir1=$1/*.log
# for each log file in the input dir
for file1 in $dir1
do
echo $file1":"
awk '/#BeginLogEntry|ServiceDuration/ {
#get slow running service's information
if ($1 == "#BeginLogEntry")
{
split($0, a, "\t");
servinfo=a[3]" ServiceAt:"a[2];
} else {
getline;
if ($0 > 10000000)
{
print servinfo", ServDur:"$0
}
}
}' $file1
done
Run Code Online (Sandbox Code Playgroud)
在运行脚本时,我得到以下错误:
./servicedurationlimit.awk /path/to/server/logs/
./servicedurationlimit.awk: line 12: syntax error near unexpected token `$0,'
./servicedurationlimit.awk: line 12: ` split($0, a, "\t"); '
Run Code Online (Sandbox Code Playgroud)
你能帮我理解可能导致这种情况的原因吗?
下面是示例日志文件(有2个日志条目):
#BeginLogEntry 04.13 20:11:11.671 …Run Code Online (Sandbox Code Playgroud) 我有一个标签分隔文件,如下所示
John 1,0 3,2 5,6
Mike 3,2 4,5 0,0
James 3,0 5,3 4,5
Run Code Online (Sandbox Code Playgroud)
我想添加3个字段的所有第一个元素,并添加3个字段的第二个元素输出如下
John 9,8
Mike 9,7
James 12,8
Run Code Online (Sandbox Code Playgroud)
在awk中是否有解决方案,我可以使用多个字段分隔符?