去除转义字符的 ASCII 文本(ls 输出重定向)
我正在使用 Mac ssh 连接到无头 RedHat 盒子,并且需要创建一个文件列表以供下游程序进行操作。
当我重定向 ls 的输出时,创建的文件包含一堆转义字符,并且我的下游脚本无法正确读取文件名。当我创建并 cat
文件时,一切看起来都很好:
sdt5z@franklin:~/cufflinks$ ls *.pbs
01_SL8426.pbs 03_SL8428.pbs 09_SL8891.pbs
02_SL8427.pbs 04_SL8429.pbs
sdt5z@franklin:~/cufflinks$ ls *.pbs > myfiles.txt
sdt5z@franklin:~/cufflinks$ cat myfiles.txt
01_SL8426.pbs
02_SL8427.pbs
03_SL8428.pbs
04_SL8429.pbs
09_SL8891.pbs
但是如果我在 vim 中打开文件,这就是它的样子(所有的小 ^[[
和 < code>^[ 字符在我的终端模拟器中为蓝色):
1 ^[[0m^[[0m01_SL8426.pbs^[[0m
2 ^[[0m02_SL8427.pbs^[[0m
3 ^[[0m03_SL8428.pbs^[[0m
4 ^[[0m04_SL8429.pbs^[[0m
5 ^[[0m09_SL8891.pbs^[[0m
6 ^[[m
~
~
~
~
运行 file 命令显示该文件具有转义序列。
sdt5z@franklin:~/cufflinks$ file myfiles.txt
myfiles.txt: ASCII text, with escape sequences
我的问题是,如何才能非常简单地 ls 文件,将它们重定向到文件,而不需要任何转义字符?首先是什么导致了这种情况?
谢谢。 myfiles.txt:ASCII 文本,带有转义序列
I'm using a Mac ssh'ing into a headless RedHat box, and I need to create a list of files for a downstream program to operate on.
When I redirect the output of ls
, the file that's created has a bunch of escape characters, and my downstream scripts can't read in the filenames properly. When I create and cat
the file, everything looks fine:
sdt5z@franklin:~/cufflinks$ ls *.pbs
01_SL8426.pbs 03_SL8428.pbs 09_SL8891.pbs
02_SL8427.pbs 04_SL8429.pbs
sdt5z@franklin:~/cufflinks$ ls *.pbs > myfiles.txt
sdt5z@franklin:~/cufflinks$ cat myfiles.txt
01_SL8426.pbs
02_SL8427.pbs
03_SL8428.pbs
04_SL8429.pbs
09_SL8891.pbs
But if I open the file in vim, this is what it looks like (all the little ^[[
and ^[
characters are colored blue in my terminal emulator):
1 ^[[0m^[[0m01_SL8426.pbs^[[0m
2 ^[[0m02_SL8427.pbs^[[0m
3 ^[[0m03_SL8428.pbs^[[0m
4 ^[[0m04_SL8429.pbs^[[0m
5 ^[[0m09_SL8891.pbs^[[0m
6 ^[[m
~
~
~
~
Running the file command shows that this file has escape sequences.
sdt5z@franklin:~/cufflinks$ file myfiles.txt
myfiles.txt: ASCII text, with escape sequences
My question is, how can I very simply ls the files, redirect those to a file, without any escape characters? What causes this in the first place?
Thanks.
myfiles.txt: ASCII text, with escape sequences
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您的
ls
看起来像是“ls --color
”之类的别名。如果你取消别名,你将不会得到任何颜色,这就是转义字符的来源。别名 | grep ls
会告诉你。我将ls
保留为非别名,而是将l
等别名设置为“ls --color -F
”。Your
ls
looks like it's aliased to something like 'ls --color
'. If you unalias it, you will not get any colouring, which is where the escape characters are coming from.alias | grep ls
will tell you. I leavels
unaliased and instead alias something likel
to 'ls --color -F
'.在这种情况下,我认为来自 ls 的附加信息被打印到文件中。 cat 命令使用信息为终端中的输出着色,而 vim 显示文件中的颜色字符。
要修复此问题,请使用以下命令
/bin/ls *.pbs
而不是ls *.pbs
这样您就可以保留别名,同时从 ls 获得所需的行为。
In this case, I think the additional information from ls is being printed to the file.
cat
command is using the information to color the output in the terminal while vim is displaying the color characters as they are in the file.To fix, instead of
ls *.pbs
use the following command/bin/ls *.pbs
This way you can leave your alias alone, while getting the desired behavior from ls.
您可以执行以下操作,而不是更改别名:
Rather than changing your alias, you can just do:
它并不明显,但您可以使用
echo
换行,echo
将删除所有 ANSI 转义字符。(参见屏幕截图)
FS 结构:
列表项 - 仅项目:
列表项 - 带相对路径:
列表项 - 带绝对路径路径:
编辑
抱歉,我提供了不好的示例,我会尝试通过屏幕截图改进它们:
Its not obvious, but you can wrap with
echo
,echo
will remove all ANSI escape chars.(see screen shots)
FS structure:
List items - just items:
List items - with relative path:
List items - with absolute path:
EDIT
Sorry, I provided bad examples, I will try to improve them by screen shot:
看起来 ls 命令认为它是在终端中执行的,即使您正在管道输出。您可以尝试
--color=never
选项:Looks like
ls
command thinks it's executed in terminal even if you're piping output. You can try--color=never
option:运行时您会看到什么
我看到以下内容:
系统通常足够智能,在重定向时不会着色,但您可以尝试
然后再试一次以确保不会获得颜色。
What do you see when you run
I see the following:
The system is usually smart enough to not color when redirecting but you might try
and then try again to ensure you don't get colors.