bash 使用 AWK 从文件中读取行并创建一个变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10181195/
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
Using AWK to read line from file and create a variable
提问by Sara
I have a text file with a list of filenames. I would like to create a variable from a specific line number using AWK. I get the correct output using:
我有一个包含文件名列表的文本文件。我想使用 AWK 从特定行号创建一个变量。我使用以下方法获得正确的输出:
awk "NR==$Line" /myPath/fileList.txt
I want to assign this output to a variable and from documentation I found I expected the following to work:
我想将此输出分配给一个变量,并且从文档中我发现我希望以下内容起作用:
INFILE=$(awk "NR==$Line" /myPath/fileList.txt)
or
或者
INFILE=`awk "NR==$Line" /myPath/fileList.txt`
However,
然而,
echo "$INFILE"
is blank. I am new to bash scripting and would appreciate any pointers.
是空白。我是 bash 脚本的新手,希望得到任何指点。
回答by Paused until further notice.
The output of the AWK command is assigned to the variable. To see the contents of the variable, do this:
AWK 命令的输出被分配给变量。要查看变量的内容,请执行以下操作:
echo "$INFILE"
You should use single quotes for your AWK command so you don't have to escape the literal dollar sign (the literal string should be quoted, see below if you want to substitute a shell variable instead):
你应该对你的 AWK 命令使用单引号,这样你就不必转义文字美元符号(文字字符串应该被引用,如果你想替换一个 shell 变量,请参见下文):
awk 'NR == "$Line"' /myPath/fileList.txt
The $()form is much preferred over the backtick form (I don't understand why you have the backticks escaped, by the way). Also, you should habitually use lowercase or mixed case variable names to avoid name collision with shell or environment variables.
这种$()形式比反引号形式更受欢迎(顺便说一下,我不明白为什么你要转义反引号)。此外,您应该习惯性地使用小写或大小写混合的变量名,以避免与 shell 或环境变量的名称冲突。
infile=$(awk 'NR == "$Line"' /myPath/fileList.txt)
echo "$infile"
If your intention is that the value of a variable named $Lineshould be substituted rather than the literal string "$Line" being used, then you should use AWK's -vvariable passing feature:
如果您的意图是$Line替换命名变量的值而不是使用文字字符串“$Line”,那么您应该使用 AWK 的-v变量传递功能:
infile=$(awk -v "line=$Line" 'NR == line' /myPath/fileList.txt)
回答by user unknown
Don't mask the dollar sign.
不要掩盖美元符号。
wrong:
错误的:
echo "$INFILE"
right:
对:
echo $INFILE
echo ${INFILE}
echo "$INFILE"
echo "${INFILE}"
The ${x} - construct is useful, if you like to glue texts together.
如果您想将文本粘合在一起,则 ${x} - 构造很有用。
echo $INFILE0
will look for a Variable INFILE0. If $INFILE is 5, it will not produce "50".
将查找变量 INFILE0。如果 $INFILE 为 5,则不会产生“50”。
echo ${INFILE}0
This will produce 50, if INFILE is 5.
如果 INFILE 为 5,这将产生 50。
The apostrophes are useful if you variable contains whitespace and for more or less unpredictable text.
如果变量包含空格和或多或少不可预测的文本,撇号很有用。
If your rownumber is a parameter:
如果您的 rownumber 是一个参数:
#!/bin/bash
Line=
INFILE=$(awk "NR==$Line" ./demo.txt)
echo "$INFILE"
If INFILE contains multiple spaces or tabs, echo $INFILE would condense them to single spaces, while "$INFILE" preserves them.
如果 INFILE 包含多个空格或制表符, echo $INFILE 会将它们压缩为单个空格,而 "$INFILE" 保留它们。

