AWK/bash 如何在 AWK 中包含文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10269181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:06:36  来源:igfitidea点击:

AWK/bash how to include a filename in AWK

linuxbashshellawk

提问by user1253622

I have the following awk script:

我有以下 awk 脚本:

#! /bin/awk -f

BEGIN { FS = ":" }

{ print  " "   " "   " "  " "  }

I want to code a bash script with "#! /bin/bash", but I need to include a file with it; as the program displays the files.

我想用“#!/bin/bash”编写一个 bash 脚本,但我需要包含一个文件;当程序显示文件时。

#! /bin/bash

awk -f, '
FILENAME= 
BEGIN { FS = ":" }

{ print  " "   " "   " "  " "  }

'

In above code, I tried to include the file but it doesnt work ?

在上面的代码中,我试图包含该文件,但它不起作用?

回答by DarkDust

It's not quite clear what you want to pass here. Do you want a variable that gets a filename as value? The easiest way do that would be to use -v var=value:

不太清楚你想在这里传递什么。你想要一个获取文件名作为值的变量吗?最简单的方法是使用-v var=value

#! /bin/bash

awk -v MYFILENAME="" '
BEGIN { FS = ":" }

{ print MYFILENAME " "  " "   " "   " "  " "  }
'

Note that FILENAME is a reserved variable, you cannot set it.

注意 FILENAME 是一个保留变量,你不能设置它。

Or do you want to pass input files ? In that case you simply pass them past the program, as in:

或者你想传递输入文件?在这种情况下,您只需将它们传递给程序,如下所示:

#! /bin/bash

awk '
BEGIN { FS = ":" }

{ print MYFILENAME " "  " "   " "   " "  " "  }
' ""

The -foption is to include an awkscript, btw. So -f,would ask AWK to include a file named ,.

-f方案是包括awk脚本,顺便说一句。所以-f,会要求 AWK 包含一个名为,.

On the shell level, be sure to always enclose the variables with "...", as in "$1"so you correctly handle filename with spaces.

在 shell 级别,请确保始终用 将变量括起来"...""$1"这样您就可以正确处理带有空格的文件名。

回答by ghoti

You don't actually need to use bash to pull in filename.

您实际上并不需要使用 bash 来拉入文件名。

[ghoti@pc ~]$ cat text
one:two:three:four
cyan:yellow:magenta:black
[ghoti@pc ~]$ cat doit.awk
#!/usr/bin/awk -f

BEGIN { FS=":" }

{
  printf("%s %s %s %s\n", , , , );
}

[ghoti@pc ~]$ ./doit.awk text     
one two three four
cyan yellow magenta black
[ghoti@pc ~]$ 

But as Tim suggested, it's difficult to figure out what results you're looking for, based on what you've included in your question. Do you REALLY need to be running your awk script inside bash for other reasons? Do you need the actually file NAME to be available within your awk script?

但正如蒂姆所建议的,根据您在问题中包含的内容,很难确定您正在寻找什么结果。由于其他原因,您真的需要在 bash 中运行 awk 脚本吗?您是否需要在 awk 脚本中提供实际文件 NAME?

The more detailed a question you provide, the more accurate the details in the responses will be.

您提供的问题越详细,回复中的详细信息就越准确。

回答by Tim Pote

It's hard for me to tell from the question, but I think what you want is

我很难从问题中看出,但我认为你想要的是

#!/usr/bin/env bash

awk -F, '
{ 
  #do stuff
}' ""

That will run awk on the file that you pass into the shell script.

这将在您传递给 shell 脚本的文件上运行 awk。

However, if you're wanting script variable replacement inside of awk, you can escape out of the literal string to get variable replacement. For example:

但是,如果您想在 awk 中替换脚本变量,则可以从文字字符串中转义以获取变量替换。例如:

#!/usr/bin/env bash

filename=""
awk -F, '
{ 
  print "'"$filename"'"
}' ""

will print out the filename you passed for as many lines as you have in the file. Note that this will cause awk to treat all of those variables as literal strings. So this would work as well:

将打印出您为文件中的行数传递的文件名。请注意,这将导致 awk 将所有这些变量视为文字字符串。所以这也可以工作:

#!/usr/bin/env bash

filename=""
awk -F, '
BEGIN {
  awksFileName="'"$filename"'"
}
{ 
  print awksFileName
}' ""