在 bash 中,如何打印列表的前 n 个元素?

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

In bash, how can I print the first n elements of a list?

bashsyntax

提问by Frank

In bash, how can I print the first nelements of a list?

在 中bash,如何打印n列表的第一个元素?

For example, the first 10 files in this list:

例如,此列表中的前 10 个文件:

FILES=$(ls)

UPDATE:I forgot to say that I want to print the elements on one line, just like when you print the whole list with echo $FILES.

更新:我忘了说我想在一行上打印元素,就像你用echo $FILES.

回答by Ayman Hourieh

FILES=(*)
echo "${FILES[@]:0:10}"

Should work correctly even if there are spaces in filenames.

即使文件名中有空格也应该正常工作。

FILES=$(ls)creates a string variable. FILES=(*)creates an array. See this page for more examples on using arrays in bash. (thanks lhunath)

FILES=$(ls)创建一个字符串变量。FILES=(*)创建一个数组。有关在 bash 中使用数组的更多示例,请参阅此页面。(感谢 lhunath)

回答by Niglas

FILE="$(ls | head -1)"

Handled spaces in filenames correctly too when I tried it.

当我尝试时,也正确处理了文件名中的空格。

回答by Pat

Why not just this to print the first 50 files:

为什么不只是这样打印前 50 个文件:

ls -1 | head -50

回答by pik3y

My way would be:

我的方法是:

ls | head -10 | tr "\n" " "

This will print the first 10 lines returned by ls, and then tr replaces all line breaks with spaces. Output will be on a single line.

这将打印 ls 返回的前 10 行,然后 tr 用空格替换所有换行符。输出将在一行上。

回答by FeatureCreep

echo $FILES | awk '{for (i = 1; i <= 10; i++) {print $i}}'

Edit: AAh, missed your comment that you needed them on one line...

编辑:啊,错过了你在一行上需要它们的评论......

echo $FILES | awk '{for (i = 1; i <= 10; i++) {printf "%s ", $i}}'

That one does that.

那个就是这样做的。

回答by Idelic

to do it interactively:

以交互方式执行此操作:

set $FILES && eval eval echo \\\${1..10}

set $FILES && eval eval echo \\\${1..10}

to run it as a script, create foo.sh with contents

要将其作为脚本运行,请使用内容创建 foo.sh

N=$1; shift; eval eval echo \\\${1..$N}

N=$1; shift; eval eval echo \\\${1..$N}

and run it as

并运行它

bash foo.sh 10 $FILES

bash foo.sh 10 $FILES

回答by sunny256

FILES=$(ls)
echo $FILES | fmt -1 | head -10

回答by Marco

An addition to the answer of "Ayman Hourieh" and "Shawn Chin", in case it is needed for something else than content of a directory.

“Ayman Hourieh”和“Shawn Chin”的答案的补充,以防目录内容以外的其他内容需要它。

In newer version of bash you can use mapfile to store the directory in an array. See help mapfile

在较新版本的 bash 中,您可以使用 mapfile 将目录存储在数组中。看help mapfile

mapfile -t files_in_dir < <( ls )

If you want it completely in bash use printf "%s\n" *instead of ls, or just replace lswith any other command you need.

如果您希望它完全在 bash 中使用printf "%s\n" *而不是ls,或者只是替换ls为您需要的任何其他命令。

Now you can access the array as usual and get the data you need.

现在您可以像往常一样访问数组并获取所需的数据。

First element:

第一个元素:

${files_in_dir[0]}

Last element (do not forget space after ":" ):

最后一个元素(不要忘记 ":" 后的空格):

${files_in_dir[@]: -1}

Range e.g. from 10 to 20:

范围例如从 10 到 20:

${files_in_dir[@]:10:20}

Attention for large directories, this is way more memory consuming than the other solutions.

注意大目录,这比其他解决方案消耗更多内存。