搜索2个文件并输出公共文件并打印额外的行unix脚本

时间:2022-06-05 00:28:08

I have this code, but it's giving me an error

我有这个代码,但它给了我一个错误

awk '
    FNR == NR {
     # reading get_ids_only.txt
     values[$1] = ""
     next
    }
BEGIN {
  # reading default.txt
  for (elem in values){
    if ($0 ~ elem){
      if (values[elem] == ""){
        values[elem] = "\"" $0 "\""
        getline;  
        values[elem] = "\n"" $0 ""\n"
        }
      else{
        values[elem] = values[elem] ", \"" $0 "\""
         getline; 
         values[elem] = values[elem] "\n"" $0 ""\n"
         }
    }
 }
END {
  for (elem in values)
    print elem " [" values[elem] "]"
    }
' get_ids_only.txt default.txt

The error says

错误说

awk: syntax error at source line 23
 context is
    >>>  END <<<  {
awk: illegal statement at source line 24
awk: illegal statement at source line 24
    missing }

This is where my END{ } function starts...

这是我的END {}函数启动的地方......

What I'm trying to do is.. compare the string.... in file 1.. if the string is found in file 2, print the string and print the line after it as well., then skip a space.

我正在尝试做的是..比较文件1中的字符串....如果在文件2中找到字符串,则打印字符串并在其后打印该行。然后跳过一个空格。

input1:

 message id "hello"
 message id "good bye"
 message id "what is cookin"

input2:

 message id "hello"
 message value "greetings"

 message id "good bye"
 message value "limiting"

 message id "what is there"
 message value "looking for me"

 message id "what is cooking"
 message value "breakfast plate"

output:

 should print out all the input1, grabbing the message value from input 2.

can anyone guide me on why this error is occurring?

任何人都可以指导我为什么会出现这个错误?

I'm using the terminal on my mac.

我在我的Mac上使用终端。

1 个解决方案

#1


1  

Here's your BEGIN block with recommended indention and comments, can you see the problem?

这是您推荐的缩进和注释的BEGIN块,您能看到问题吗?

BEGIN {
  # reading default.txt
  for (elem in values){
    if ($0 ~ elem){
      if (values[elem] == ""){
        values[elem] = "\"" $0 "\""
        getline;  
        values[elem] = "\n"" $0 ""\n"
      }
      else{
        values[elem] = values[elem] ", \"" $0 "\""
        getline; 
        values[elem] = values[elem] "\n"" $0 ""\n"
      } # End inner if
    } # End outer if
  } # End for loop

Your missing a closing brace. Note that in the final concatenation with $0, $0 is actually quoted.

你错过了一个右大括号。请注意,在$ 0的最终串联中,实际引用$ 0。

There are some other issues with this, I'm not sure what you are trying to do, but it seems a very un-awky approach. Usually if you find yourself overusing getline, you should be thinking about spreading the code into separate blocks with appropriate conditions. See this article on the uses an misuses of getline for more.

还有其他一些问题,我不确定你要做什么,但这似乎是一个非常不灵活的方法。通常,如果您发现自己过度使用getline,那么您应该考虑将代码分散到具有适当条件的单独块中。请参阅此文章,了解更多使用getline的误用。

A more awky way to solve it

If I understand you correctly, this is the way I would solve this task:

如果我理解正确,这就是我解决这个任务的方法:

extract.awk

FNR==NR  { id[$0]; next }  # Collect id lines in the `id' array
$0 in id { f=1 }           # Use the `f' as a printing flag 
f                          # Print when `f' is 1
NF==0    { f=0 }           # Stop printing after an empty line

Run it like this:

像这样运行:

awk -f extract.awk input1 input2

Output:

message id "hello"
message value "greetings"

message id "good bye"
message value "limiting"

#1


1  

Here's your BEGIN block with recommended indention and comments, can you see the problem?

这是您推荐的缩进和注释的BEGIN块,您能看到问题吗?

BEGIN {
  # reading default.txt
  for (elem in values){
    if ($0 ~ elem){
      if (values[elem] == ""){
        values[elem] = "\"" $0 "\""
        getline;  
        values[elem] = "\n"" $0 ""\n"
      }
      else{
        values[elem] = values[elem] ", \"" $0 "\""
        getline; 
        values[elem] = values[elem] "\n"" $0 ""\n"
      } # End inner if
    } # End outer if
  } # End for loop

Your missing a closing brace. Note that in the final concatenation with $0, $0 is actually quoted.

你错过了一个右大括号。请注意,在$ 0的最终串联中,实际引用$ 0。

There are some other issues with this, I'm not sure what you are trying to do, but it seems a very un-awky approach. Usually if you find yourself overusing getline, you should be thinking about spreading the code into separate blocks with appropriate conditions. See this article on the uses an misuses of getline for more.

还有其他一些问题,我不确定你要做什么,但这似乎是一个非常不灵活的方法。通常,如果您发现自己过度使用getline,那么您应该考虑将代码分散到具有适当条件的单独块中。请参阅此文章,了解更多使用getline的误用。

A more awky way to solve it

If I understand you correctly, this is the way I would solve this task:

如果我理解正确,这就是我解决这个任务的方法:

extract.awk

FNR==NR  { id[$0]; next }  # Collect id lines in the `id' array
$0 in id { f=1 }           # Use the `f' as a printing flag 
f                          # Print when `f' is 1
NF==0    { f=0 }           # Stop printing after an empty line

Run it like this:

像这样运行:

awk -f extract.awk input1 input2

Output:

message id "hello"
message value "greetings"

message id "good bye"
message value "limiting"