bash 使用变量作为行号从bash脚本中的文件中读取特定行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21651310/
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 09:31:04  来源:igfitidea点击:

reading a particular line from a file in bash script using variable as line number

bashshellsed

提问by user3286661

I have a csv file which contains a large number of entries. I have to read certain lines from the file. The line number is stored in a variable called lineno.
How to accomplish this?It may look something like:

我有一个包含大量条目的 csv 文件。我必须从文件中读取某些行。行号存储在名为lineno的变量中。
如何做到这一点?它可能看起来像:

line=$(sed -n "($lineno)p")

I want to know the correct syntax to do this.
Thanks

我想知道正确的语法来做到这一点。
谢谢

回答by kojiro

You pretty much have it.

你几乎拥有它。

line=$(sed -n "${lineno}p" "$file")