tr中四个空格怎么表达?
How to express four white spaces in tr?
我想用四个空格替换换行符。
tr "\n" " "
经验证tr "\n" " "
做不到。
ls -a |tr "\n" " "
.sane .shutter .ssh .subversion .svx .svy .svz .swa .swb .swc .swd .swe
这里没有四个空格。
1. \n
确实有效。
2.两个空格和四个空格没有区别
3.\n
与\r\n
效果相同
错误的工具。 tr
将单个字符映射到单个字符,而不是字符串。来自 tr(1) 手册页:
[...] the characters in string1 are translated into
the characters in string2 where the first character in string1 is
translated into the first character in string2 and so on. If string1 is
longer than string2, the last character found in string2 is duplicated
until string1 is exhausted.
您想要一个更通用的模式匹配器和替换器:
ls | perl -p -e 's/\n/ /'
1。了解 tr
命令的行为:
查看tr
命令的手册,它指出:
NAME
tr - translate or delete characters
SYNOPSIS
tr [OPTION]... SET1 [SET2]
其中:
SET
s are specified as strings of characters
所以第一个列表 (SET1
) 中的每个字符都将被第二个列表 (SET2
) 中的对应字符更改
如果我们有一个名为“file”的文件包含:
wordorwd
通过执行此命令:
tr 'word' 'abcd' < file
我们会得到abcdbcad
有关 tr
命令的更多信息,您可以查看此 link
2。了解ls
命令的输出格式:
理解命令的输出ls
。我们需要看看它在内部是如何工作的。可以通过下载包coreutils
:
获取
git clone git://git.sv.gnu.org/coreutils
或者通过这个link直接检查:ls-source code.
一般来说,它首先搜索目录,然后获取所有相关文件,排序并将它们推送到 linked 列表中,然后弹出并在输出流中传递它们附加“\n
”后。
还有一个名为od
的linux命令可以显示ls
命令的输出:
ls -a | od -c
现在 ls -a | tr "\n" " "
实际上工作正常并且正在将换行符更改为 space 这导致显示 1 只有行 包含由space 分隔的所有文件名。
对于 ls -a | tr "\r\n" " "
ls
输出中没有回车 return 只有换行符将被更改,这就是相同行为的解释。
您可以使用 tr
命令的 -d
选项来确认 通过先删除回车 return (无效)然后换行
ls -a | tr -d "\r"
ls -a | tr -d "\n"
我想用四个空格替换换行符。
tr "\n" " "
经验证tr "\n" " "
做不到。
ls -a |tr "\n" " "
.sane .shutter .ssh .subversion .svx .svy .svz .swa .swb .swc .swd .swe
这里没有四个空格。
1. \n
确实有效。
2.两个空格和四个空格没有区别
3.\n
与\r\n
效果相同
错误的工具。 tr
将单个字符映射到单个字符,而不是字符串。来自 tr(1) 手册页:
[...] the characters in string1 are translated into the characters in string2 where the first character in string1 is translated into the first character in string2 and so on. If string1 is longer than string2, the last character found in string2 is duplicated until string1 is exhausted.
您想要一个更通用的模式匹配器和替换器:
ls | perl -p -e 's/\n/ /'
1。了解 tr
命令的行为:
查看tr
命令的手册,它指出:
NAME
tr - translate or delete characters
SYNOPSIS
tr [OPTION]... SET1 [SET2]
其中:
SET
s are specified as strings of characters
所以第一个列表 (SET1
) 中的每个字符都将被第二个列表 (SET2
) 中的对应字符更改
如果我们有一个名为“file”的文件包含:
wordorwd
通过执行此命令:
tr 'word' 'abcd' < file
我们会得到abcdbcad
有关 tr
命令的更多信息,您可以查看此 link
2。了解ls
命令的输出格式:
理解命令的输出ls
。我们需要看看它在内部是如何工作的。可以通过下载包coreutils
:
git clone git://git.sv.gnu.org/coreutils
或者通过这个link直接检查:ls-source code.
一般来说,它首先搜索目录,然后获取所有相关文件,排序并将它们推送到 linked 列表中,然后弹出并在输出流中传递它们附加“\n
”后。
还有一个名为od
的linux命令可以显示ls
命令的输出:
ls -a | od -c
现在 ls -a | tr "\n" " "
实际上工作正常并且正在将换行符更改为 space 这导致显示 1 只有行 包含由space 分隔的所有文件名。
对于 ls -a | tr "\r\n" " "
ls
输出中没有回车 return 只有换行符将被更改,这就是相同行为的解释。
您可以使用 tr
命令的 -d
选项来确认 通过先删除回车 return (无效)然后换行
ls -a | tr -d "\r"
ls -a | tr -d "\n"