Linux 如何使用自定义分隔符将多行文件名合并为一行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2764051/
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
How to join multiple lines of file names into one with custom delimiter?
提问by JavaRocky
I would like to join the result of ls -1
into one line and delimit it with whatever i want.
我想将结果加入ls -1
一行并用我想要的任何内容分隔它。
Are there any standard Linux commands I can use to achieve this?
我可以使用任何标准的 Linux 命令来实现这一点吗?
采纳答案by Artem
Similar to the very first option but omits the trailing delimiter
类似于第一个选项,但省略了尾随分隔符
ls -1 | paste -sd "," -
回答by Chris J
If you version of xargs supports the -d flag then this should work
如果您的 xargs 版本支持 -d 标志,那么这应该可以工作
ls | xargs -d, -L 1 echo
-d is the delimiter flag
-d 是分隔符标志
If you do not have -d, then you can try the following
如果你没有-d,那么你可以试试下面的
ls | xargs -I {} echo {}, | xargs echo
The first xargs allows you to specify your delimiter which is a comma in this example.
第一个 xargs 允许您指定分隔符,在此示例中为逗号。
回答by zaf
EDIT: Simply "ls -m" If you want your delimiter to be a comma
编辑:简单的“ ls -m”如果你想让你的分隔符是一个逗号
Ah, the power and simplicity !
啊,强大而简单!
ls -1 | tr '\n' ','
Change the comma "," to whatever you want. Note that this includes a "trailing comma"
将逗号“ ,”更改为您想要的任何内容。请注意,这包括“尾随逗号”
回答by codaddict
You can use:
您可以使用:
ls -1 | perl -pe 's/\n$/some_delimiter/'
回答by yabt
To avoid potential newline confusion for tr we could add the -b flag to ls:
为了避免 tr 潜在的换行混淆,我们可以将 -b 标志添加到 ls:
ls -1b | tr '\n' ';'
回答by Paused until further notice.
This replaces the last comma with a newline:
这将用换行符替换最后一个逗号:
ls -1 | tr '\n' ',' | sed 's/,$/\n/'
ls -m
includes newlines at the screen-width character (80th for example).
ls -m
包括屏幕宽度字符处的换行符(例如第 80 个)。
Mostly Bash (only ls
is external):
主要是 Bash(仅ls
是外部的):
saveIFS=$IFS; IFS=$'\n'
files=($(ls -1))
IFS=,
list=${files[*]}
IFS=$saveIFS
Using readarray
(aka mapfile
) in Bash 4:
在 Bash 4 中使用readarray
(又名mapfile
):
readarray -t files < <(ls -1)
saveIFS=$IFS
IFS=,
list=${files[*]}
IFS=$saveIFS
Thanks to gniourf_gniourf for the suggestions.
感谢 gniourf_gniourf 的建议。
回答by ghostdog74
just bash
只是猛击
mystring=$(printf "%s|" *)
echo ${mystring%|}
回答by glenn Hymanman
The combination of setting IFS
and use of "$*"
can do what you want. I'm using a subshell so I don't interfere with this shell's $IFS
设置IFS
和使用的结合"$*"
可以为所欲为。我正在使用子外壳,所以我不会干扰这个外壳的 $IFS
(set -- *; IFS=,; echo "$*")
To capture the output,
要捕获输出,
output=$(set -- *; IFS=,; echo "$*")
回答by Thor
ls
produces one column output when connected to a pipe, so the -1
is redundant.
ls
连接到管道时产生一列输出,因此-1
是多余的。
Here's another perl answer using the builtin join
function which doesn't leave a trailing delimiter:
这是使用内置join
函数的另一个 perl 答案,它不会留下尾随分隔符:
ls | perl -F'\n' -0777 -anE 'say join ",", @F'
The obscure -0777
makes perl read all the input before running the program.
晦涩-0777
使 perl 在运行程序之前读取所有输入。
sed alternative that doesn't leave a trailing delimiter
不留下尾随分隔符的 sed 替代方案
ls | sed '$!s/$/,/' | tr -d '\n'
回答by majkinetor
I think this one is awesome
我觉得这个很棒
ls -1 | awk 'ORS=","'
ORS is the "output record separator" so now your lines will be joined with a comma.
ORS 是“输出记录分隔符”,所以现在您的行将用逗号连接。