将 awk 设置为变量

发布于 2024-12-09 03:55:31 字数 232 浏览 0 评论 0原文

我的代码如下所示:

awk -F'|' '{if($1 in a)print "duplicate found:" $2 " AND "a[$1];else a[$1]=$2 }' dump.txt

我需要将 $2 和 a[$2] 设置为变量。我该怎么做呢?

我正在获取一个包含以下内容的文件: 值“|” filename,然后我想将文件名和值设置为两个不同的变量。

I have code that looks like this:

awk -F'|' '{if($1 in a)print "duplicate found:" $2 " AND "a[$1];else a[$1]=$2 }' dump.txt

I need to set $2 and a[$2] to a variable. How would I go about doing this?

I am taking a file that contains: a value "|" filename and then I want to set the filename and the value to two different variables.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

携余温的黄昏 2024-12-16 03:55:31

设置这些变量是什么意思?这些是环境变量。在 Awk 中,以美元符号开头且为数字的变量是为 AWK 保留的。这些是每行的字段值。例如:

test.txt =
   this is line one
   this is line two
   this is line three

命令 awk '{print $4}' test.txt 将打印出第四个字段:

$ awk '{print $4}' test.txt
one
two
three

$ awk '{print $3}' test.txt
line
line
line

如您所见:它们不必设置。它们由 Awk 自动设置。

如果要设置环境变量,可以使用 -v 参数

awk -v search="foo" '{
    if (search = $1) {
       print "Found your string in record " NR
    }'

在上面,search 是一个 Awk 变量,设置等于 foo >。

由于 Awk 是一种编程语言,因此有时通过正确格式化程序可以更轻松地了解发生的情况:

awk -F'|' '{
    if($1 in a) {
      print "duplicate found:" $2 " AND " a[$1]
    }
    else {
       a[$1] = $2
    } 
}' dump.txt

程序正在读取每一行。每行由 | 分隔的两部分组成。看起来密钥的第一部分和第二部分是数据。我创建了一个如下所示的文本文件:

This is a|test
That is a|line
who moans for|anchovies
whom moans for|anchovies
This is a|test again

第一行和最后一行应显示为重复项

我获取了您的程序并添加了一些调试行。这将帮助我跟踪它在程序中的位置:

awk -F\| '{
    if ($1 in a) {
        print "DEBUG: In If clause"
        print "duplicate found:" $2 " and " a[$1]
    } else {
        print "DEBUG: In else clause"
        a[$1] = $2
        print "DEBUG: a[" $1 "] = " a[$1]
    }
    print "DEBUG: $1 = " $1
    print "DEBUG: $2 = " $2 "\n"
}' test.txt

而且,这是我的输出

DEBUG: In else clause
DEBUG: a[This is a] = test
DEBUG: $1 = This is a
DEBUG: $2 = test

DEBUG: In else clause
DEBUG: a[That is a] = line
DEBUG: $1 = That is a
DEBUG: $2 = line

DEBUG: In else clause
DEBUG: a[who moans for] = anchovies
DEBUG: $1 = who moans for
DEBUG: $2 = anchovies

DEBUG: In else clause
DEBUG: a[whom moans for] = anchovies
DEBUG: $1 = whom moans for
DEBUG: $2 = anchovies

DEBUG: In If clause
duplicate found: test again and test
DEBUG: $1 =This is a
DEBUG: $2 = test again

取出调试行:

awk -F\| '{
if ($1 in a) {
    print "duplicate found:" $2 " and " a[$1]
} else {
    a[$1] = $2
}
}' test.txt

duplicate found: test again and test

正如他们所说:

IT WORKS ON MY COMPUTER

(rimshot)

说真的,你的程序应该做什么,你认为它会做什么?有没有错误?您的程序看起来像广告中那样工作。

What do you mean Set those variables? Are these environment variables. In Awk, variables that start with a dollar sign and are numeric are reserved for AWK. These are the field values for each line. For example:

test.txt =
   this is line one
   this is line two
   this is line three

The command awk '{print $4}' test.txt will print out the fourth field:

$ awk '{print $4}' test.txt
one
two
three

$ awk '{print $3}' test.txt
line
line
line

As you can see: They don't have to be set. They're automatically set by Awk.

If you want to set environment variables, you can use the -v parameter

awk -v search="foo" '{
    if (search = $1) {
       print "Found your string in record " NR
    }'

In the above, search is an Awk variable that is set equal to foo.

Since Awk is a programming language, it is sometimes easier to see what's going on by correctly formatting your program:

awk -F'|' '{
    if($1 in a) {
      print "duplicate found:" $2 " AND " a[$1]
    }
    else {
       a[$1] = $2
    } 
}' dump.txt

The program is taking each line. Each line consists of two parts separated by the |. It appears that the first part in the key and the second part is the data. I've created a text file that looks like this:

This is a|test
That is a|line
who moans for|anchovies
whom moans for|anchovies
This is a|test again

The first and last line should show up as duplicates

I took your program and added a few debug lines. This will help me trace where it is in your program:

awk -F\| '{
    if ($1 in a) {
        print "DEBUG: In If clause"
        print "duplicate found:" $2 " and " a[$1]
    } else {
        print "DEBUG: In else clause"
        a[$1] = $2
        print "DEBUG: a[" $1 "] = " a[$1]
    }
    print "DEBUG: $1 = " $1
    print "DEBUG: $2 = " $2 "\n"
}' test.txt

And, this is my output

DEBUG: In else clause
DEBUG: a[This is a] = test
DEBUG: $1 = This is a
DEBUG: $2 = test

DEBUG: In else clause
DEBUG: a[That is a] = line
DEBUG: $1 = That is a
DEBUG: $2 = line

DEBUG: In else clause
DEBUG: a[who moans for] = anchovies
DEBUG: $1 = who moans for
DEBUG: $2 = anchovies

DEBUG: In else clause
DEBUG: a[whom moans for] = anchovies
DEBUG: $1 = whom moans for
DEBUG: $2 = anchovies

DEBUG: In If clause
duplicate found: test again and test
DEBUG: $1 =This is a
DEBUG: $2 = test again

Taking out the debug lines:

awk -F\| '{
if ($1 in a) {
    print "duplicate found:" $2 " and " a[$1]
} else {
    a[$1] = $2
}
}' test.txt

duplicate found: test again and test

As they say:

IT WORKS ON MY COMPUTER

(rimshot)

Seriously, what is your program suppose to be doing, and what do you see it do? Are there any errors? Your program appears to work as advertised.

情绪 2024-12-16 03:55:31

将它们设置为如下环境变量:

awk -F'|' '{if($1 in a)print "duplicate found:" '"$2"' " AND "a[$1];else a[$1]='"$2"' }' dump.txt

请注意,我所做的是“禁用” $2 部分周围的单引号,并在它们周围添加双引号,以防环境变量包含空格 (哪个 awk 不希望看到将其参数分成几部分)。

Set them to an environment variable like this:

awk -F'|' '{if($1 in a)print "duplicate found:" '"$2"' " AND "a[$1];else a[$1]='"$2"' }' dump.txt

Note that what I did was to "disable" the single-quotes around the $2 parts, and add double-quotes around them in case the environment variable contains spaces (which awk wouldn't want to see splitting its argument into pieces).

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