去除转义字符的 ASCII 文本(ls 输出重定向)

发布于 2024-12-22 07:17:28 字数 1184 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

柒夜笙歌凉 2024-12-29 07:17:28

您的 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 leave ls unaliased and instead alias something like l to 'ls --color -F'.

江城子 2024-12-29 07:17:28

在这种情况下,我认为来自 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.

假扮的天使 2024-12-29 07:17:28

您可以执行以下操作,而不是更改别名:

\ls *.pbs

Rather than changing your alias, you can just do:

\ls *.pbs
淡写薰衣草的香 2024-12-29 07:17:28

它并不明显,但您可以使用 echo 换行,echo 将删除所有 ANSI 转义字符。
(参见屏幕截图)

FS 结构

daniel@synapse:/tmp/test$ tree
.
├── archive.tar.bz2
├── archive.tar.gz
├── dir1
│   └── file.txt
├── dir2
│   └── file.pdf
└── image.png

2 directories, 5 files

列表项 - 仅项目

daniel@synapse:/tmp/test$ for ITEM in `echo *`; do echo ${ITEM}; done
archive.tar.bz2
archive.tar.gz
dir1
dir2
image.png

列表项 - 带相对路径

daniel@synapse:/tmp/test$ for ITEM in `echo ./*`; do echo ${ITEM}; done
./archive.tar.bz2
./archive.tar.gz
./dir1
./dir2
./image.png

列表项 - 带绝对路径路径

daniel@synapse:/tmp/test$ for ITEM in `echo $(pwd)/*`; do echo ${ITEM}; done
/tmp/test/archive.tar.bz2
/tmp/test/archive.tar.gz
/tmp/test/dir1
/tmp/test/dir2
/tmp/test/image.png

编辑

抱歉,我提供了不好的示例,我会尝试通过屏幕截图改进它们:
ls: echo 删除 ANSI 转义字符

grep: echo 删除了 ANSI 转义字符

Its not obvious, but you can wrap with echo, echo will remove all ANSI escape chars.
(see screen shots)

FS structure:

daniel@synapse:/tmp/test$ tree
.
├── archive.tar.bz2
├── archive.tar.gz
├── dir1
│   └── file.txt
├── dir2
│   └── file.pdf
└── image.png

2 directories, 5 files

List items - just items:

daniel@synapse:/tmp/test$ for ITEM in `echo *`; do echo ${ITEM}; done
archive.tar.bz2
archive.tar.gz
dir1
dir2
image.png

List items - with relative path:

daniel@synapse:/tmp/test$ for ITEM in `echo ./*`; do echo ${ITEM}; done
./archive.tar.bz2
./archive.tar.gz
./dir1
./dir2
./image.png

List items - with absolute path:

daniel@synapse:/tmp/test$ for ITEM in `echo $(pwd)/*`; do echo ${ITEM}; done
/tmp/test/archive.tar.bz2
/tmp/test/archive.tar.gz
/tmp/test/dir1
/tmp/test/dir2
/tmp/test/image.png

EDIT

Sorry, I provided bad examples, I will try to improve them by screen shot:
ls: echo removed ANSI escape chars

grep: echo removed ANSI escape chars

娇俏 2024-12-29 07:17:28

看起来 ls 命令认为它是在终端中执行的,即使您正在管道输出。您可以尝试 --color=never 选项:

ls --color=never *.pbs > myfiles.txt

Looks like ls command thinks it's executed in terminal even if you're piping output. You can try --color=never option:

ls --color=never *.pbs > myfiles.txt
哑剧 2024-12-29 07:17:28

运行时您会看到什么

$ alias

我看到以下内容:

alias ls='ls --color=auto'

系统通常足够智能,在重定向时不会着色,但您可以尝试

$ unalias ls

然后再试一次以确保不会获得颜色。

What do you see when you run

$ alias

I see the following:

alias ls='ls --color=auto'

The system is usually smart enough to not color when redirecting but you might try

$ unalias ls

and then try again to ensure you don't get colors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文