AWK命令帮助

发布于 2024-11-03 06:29:14 字数 299 浏览 1 评论 0原文

我有一个家庭作业,这就是问题。

使用 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 技术交流群。

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

发布评论

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

评论(2

古镇旧梦 2024-11-10 06:29:15

关于 awk 的一些提示:

awk 程序的格式为

expression { action; ... }
expression { action; ... }
...

如果表达式的计算结果为 true,则执行操作块。表达式的一些示例:

BEGIN       # true before any lines of input are read
END         # true after the last line of input has been read
/pattern/   # true if the current line matches the pattern
NR < 10     # true if the current line number is less than 10

等。如果您希望在每一行上执行操作,则可以省略表达式。

因此,您的 BEGIN 块有太多大括号:

BEGIN {
    "date" | getline d 
    printf("\t %s\n\n",d)
    print "Heading" 
    print "====================="
}

您还可以

BEGIN {
    system("date")
    print ""
    print "Heading" 
    print "====================="
}

在 awk 之外编写或执行 date 命令,并将结果作为 awk 变量传递。

awk -v d="$(date)" '
    BEGIN {
        printf("%s\n\n%s\n%s\n", 
            d,
            "heading",
            "======")
    }

print 命令隐式在输出中添加换行符,因此 print "foo\ n”; print "bar" 将在“foo”之后打印一个空行。 printf 命令要求您将换行符添加到格式字符串中。

无法通过“打印每个字段的代码”为您提供更多帮助。 Luuk 表明 print $0 将打印所有字段。如果这不能满足您的要求,您就必须更加具体。

Some tips about awk:

The format of an awk program is

expression { action; ... }
expression { action; ... }
...

If the expression evaluates to true, then the action block is executed. Some examples of expressions:

BEGIN       # true before any lines of input are read
END         # true after the last line of input has been read
/pattern/   # true if the current line matches the pattern
NR < 10     # true if the current line number is less than 10

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:

BEGIN {
    "date" | getline d 
    printf("\t %s\n\n",d)
    print "Heading" 
    print "====================="
}

You could also write

BEGIN {
    system("date")
    print ""
    print "Heading" 
    print "====================="
}

or execute the date command outside of awk and pass the result in as an awk variable

awk -v d="$(date)" '
    BEGIN {
        printf("%s\n\n%s\n%s\n", 
            d,
            "heading",
            "======")
    }

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.

娜些时光,永不杰束 2024-11-10 06:29:15
{"date" | getline d }

为什么不简单地打印当前日期
{ print strftime("%y-%m-%d %H:%M");对于

{ code to display each field of file??? }

这样做

{ print $0; }

如果您只想要第一个字段,则应该

{ print $1; }

:如果您只想要第二个字段:

{print $2; }

如果您只想要最后一个字段:

{print $NF;}

因为 NF 是一行中的字段数......

{"date" | getline d }

why not simply print current date
{ print strftime("%y-%m-%d %H:%M"); }

and for:

{ code to display each field of file??? }

simply do

{ print $0; }

if you only wanted the first field you should do:

{ print $1; }

if you only want the second field:

{print $2; }

if you want only the last field:

{print $NF;}

Because NF is the number of field on a line......

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