bash 为什么从 grep 输出创建文件时会得到一些额外的、奇怪的字符?

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

Why am I getting some extra, weird characters when making a file from grep output?

bashunixgrep

提问by jake9115

I am doing a very basic command that never gave me trouble in the past, but is inexplicably returning undesired characters now.

我正在执行一个非常基本的命令,过去从未给我带来麻烦,但现在却莫名其妙地返回了不需要的字符。

I am in BASH on linux, and simply want to search through a directory and make a file containing filenames that match a pattern:

我在 linux 上的 BASH 中,只想搜索一个目录并创建一个包含与模式匹配的文件名的文件:

ls | grep "*.file_ID" > my_list.txt

...This works fine, and if I cat the data:

...这工作正常,如果我发现数据:

   cat my_list.txt
       seriesA.file_ID
       seriesB.file_ID
       seriesC.file_ID

However, when I try to feed this file into downstream processes, I keep getting a weird errors, as if the file isn't properly formatted as a list of file names. When I open the file in vim to reveal any unnecessary characters, I find the file actually looks like this:

但是,当我尝试将此文件提供给下游进程时,我不断收到奇怪的错误,好像该文件没有正确格式化为文件名列表。当我在 vim 中打开文件以显示任何不必要的字符时,我发现文件实际上是这样的:

vi my_list.txt

^[[00mseriesA.file_ID^[[00m
^[[00mseriesB.file_ID^[[00m
^[[00mseriesC.file_ID^[[00m

For some reason, every line is started and ended with the characters ^[[00m. If I delete these characters, all of the downstream processes work fine. However, I need to have my scripts automatically make such a file list, so I can't keep going in and manually deleting these chars.

出于某种原因,每一行都以字符 开始和结束^[[00m。如果我删除这些字符,所有下游进程都可以正常工作。但是,我需要让我的脚本自动制作这样一个文件列表,所以我不能继续手动删除这些字符。

Does anyone know what is producing the ^[[00mcharacters? I don't have any idea where they are coming from, and need a to be able to generate files without them.

有谁知道是什么产生了^[[00m字符?我不知道它们来自哪里,需要一个能够在没有它们的情况下生成文件。

Thanks!

谢谢!

采纳答案by glenn Hymanman

Don't use ls:

不要使用 ls

printf "%s\n" *.file_ID > my_list.txt

回答by user123444555621

Probably your GREP_OPTIONSenvironment variable contains --color=always, which causes the output to be stuffed with control characters, even when piped to a file.

可能您的GREP_OPTIONS环境变量包含--color=always,这会导致输出中塞满控制字符,即使通过管道传输到文件也是如此。

Use --color=autoinstead.

使用--color=auto来代替。

http://www.gnu.org/software/grep/manual/html_node/Environment-Variables.html

http://www.gnu.org/software/grep/manual/html_node/Environment-Variables.html

Even better, don't use grep:

更好的是,不要使用grep

ls *.file_ID > my_list.txt

回答by das.cyklone

This should take care of it (assuming GNU find and no directory traversing):

这应该解决它(假设 GNU find 并且没有目录遍历):

find . -maxdepth 1 -type f -name "*.file_ID" -printf "%f\n" > my_list.txt

Example:

例子:

~> ls *file_ID*
a.file_ID  b.file_ID  c.file_ID

~> find . -maxdepth 1 -type f -name "*.file_ID" -printf "%f\n" > my_list.txt

~> cat my_list.txt
a.file_ID
b.file_ID
c.file_ID

As far as the "^[[00m" characters, check your ls options:

至于“^[[00m”字符,请检查您的 ls 选项:

~> alias -p | grep "ls="

You may get something like:

你可能会得到类似的东西:

alias ls='/bin/ls $LS_OPTIONS'

If so, check env for this:

如果是这样,请检查 env :

~> env | grep LS_OP
LS_OPTIONS=-N --color=tty -T 0

The character string you're referencing is used to turn off colors, so your shell likely has been set to show colors. Removing and/or changing the ls alias should resolve it.

您引用的字符串用于关闭颜色,因此您的外壳可能已设置为显示颜色。删除和/或更改 ls 别名应该可以解决它。

回答by euccas

The weird characters such as ^[[00mare escape characters for colorizing the output. Color output for lsis most likely enabled through an alias in your environment.

奇怪的字符,例如^[[00m用于为输出着色的转义字符。ls最有可能通过环境中的别名启用颜色输出。

To avoid getting these color characters, you can try disable the lsalias temporarily with a backslash:

为避免获得这些颜色字符,您可以尝试ls使用反斜杠暂时禁用别名:

\ls *.txt

Or you can use printfcommand instead.

或者您可以改用printf命令。

printf "%s\n" *.txt

回答by Sophie Chloe

  • usually it is automatically => grep --color=auto

  • if it pipes into a csv file that looks like this ^[[00...^[[00m

  • you would have to type this in the terminal:
    grep --color=auto "your regex" > example.csv

  • if you want it to be a permanent situation where you do not have to type "--color=auto" every time, type this in the terminal:
    export GREP_OPTIONS='--color=auto'

  • 通常它是自动的 => grep --color=auto

  • 如果它通过管道传输到一个看起来像这样的 csv 文件 ^[[00...^[[00m

  • 你必须在终端中输入:
    grep --color=auto "your regex" > example.csv

  • 如果您希望它成为您不必--color=auto每次都键入“ ”的永久情况,请在终端中键入:
    export GREP_OPTIONS='--color=auto'

more info: https://linuxcommando.blogspot.com/2007/10/grep-with-color-output.html

更多信息:https: //linuxcommando.blogspot.com/2007/10/grep-with-color-output.html