AWK命令帮助
我有一个家庭作业,这就是问题。
使用 awk 创建一个命令来显示特定文件的每个字段。
在文件开头显示日期,并在输出的头部显示一个标题。
我读过这本书,但不太明白,这是我所拥有的:
BEGIN {
{"date" | getline d }
{ printf "\t %s\n,d }
{ print "Heading\n" }
{ print "=====================\n }
}
{ code to display each field of file??? }
I have a homework assignment and this is the question.
Using awk create a command that will display each field of a specific file.
Show the date at the beginning of the file with a line between and a title at the head of the output.
I have read the book and can't quite figure it out, here is what I have:
BEGIN {
{"date" | getline d }
{ printf "\t %s\n,d }
{ print "Heading\n" }
{ print "=====================\n }
}
{ code to display each field of file??? }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关于 awk 的一些提示:
awk 程序的格式为
如果表达式的计算结果为 true,则执行操作块。表达式的一些示例:
等。如果您希望在每一行上执行操作,则可以省略表达式。
因此,您的 BEGIN 块有太多大括号:
您还可以
在 awk 之外编写或执行 date 命令,并将结果作为 awk 变量传递。
print 命令隐式在输出中添加换行符,因此 print "foo\ n”; print "bar" 将在“foo”之后打印一个空行。 printf 命令要求您将换行符添加到格式字符串中。
无法通过“打印每个字段的代码”为您提供更多帮助。 Luuk 表明
print $0
将打印所有字段。如果这不能满足您的要求,您就必须更加具体。Some tips about awk:
The format of an awk program is
If the expression evaluates to true, then the action block is executed. Some examples of expressions:
etc. The expression can be omitted if you want the action to be performed on every line.
So, your BEGIN block has too many braces:
You could also write
or execute the date command outside of awk and pass the result in as an awk variable
The print command implicitly adds a newline to the output, so
print "foo\n"; print "bar"
will print a blank line after "foo". The printf command requires you to add newlines into your format string.Can't help you more with "code to print each field". Luuk shows that
print $0
will print all fields. If that doesn't meet your requirements, you'll have to be more specific.为什么不简单地打印当前日期
{ print strftime("%y-%m-%d %H:%M");对于
:
这样做
如果您只想要第一个字段,则应该
:如果您只想要第二个字段:
如果您只想要最后一个字段:
因为 NF 是一行中的字段数......
why not simply print current date
{ print strftime("%y-%m-%d %H:%M"); }
and for:
simply do
if you only wanted the first field you should do:
if you only want the second field:
if you want only the last field:
Because NF is the number of field on a line......