在 Linux 中将文本文件中的空格替换为逗号
我需要编辑一些文本文件(sar
的输出)并将它们转换为 CSV 文件。
我需要使用 sed 或 awk 函数(Linux 中的简单 shell 脚本)更改每个空格(可能是输出中数字之间的制表符)。
谁能帮我? 我使用的每个命令根本没有改变文件; 我尝试了gsub
。
I need to edit a few text files (an output from sar
) and convert them into CSV files.
I need to change every whitespace (maybe it's a tab between the numbers in the output) using sed or awk functions (an easy shell script in Linux).
Can anyone help me? Every command I used didn't change the file at all; I tried gsub
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
sed 可以做到这一点:
这将发送到控制台,
将就地编辑文件
sed can do this:
That will send to the console,
will edit the file in-place
这是一个 Perl 脚本,它将就地编辑文件:
连续的空格将转换为单个逗号。
每个输入文件都移动到 .bak
使用以下命令行选项:
-i.bak
就地编辑并制作 .bak 副本-p
循环输入文件的每一行,自动打印该行-l
在处理之前删除换行符,然后将它们添加回来-e
执行 perl 代码Here's a Perl script which will edit the files in-place:
Consecutive whitespace is converted to a single comma.
Each input file is moved to .bak
These command-line options are used:
-i.bak
edit in-place and make .bak copies-p
loop around every line of the input file, automatically print the line-l
removes newlines before processing, and adds them back in afterwards-e
execute the perl code如果您想用一个逗号替换任意序列的空白字符(制表符、空格),请使用以下命令:
或者
如果某些输入行包含多余的前导空格字符,不需要将其转换为逗号,则首先,您需要删除它们,然后将剩余的空白字符转换为逗号。 对于这种情况,请使用以下命令:
If you want to replace an arbitrary sequence of blank characters (tab, space) with one comma, use the following:
or
If some of your input lines include leading space characters which are redundant and don't need to be converted to commas, then first you need to get rid of them, and then convert the remaining blank characters to commas. For such case, use the following:
这对我有用。
This worked for me.
用逗号替换每个空格,如果需要,可以使用 -s 标志(挤压重复)进行传递,这会将 SET1(空格)中列出的重复字符的每个输入序列替换为该字符的单次出现特点。
在替换选项卡后使用挤压重复:
Substitutes each space with a comma, if you need you can make a pass with the -s flag (squeeze repeats), that replaces each input sequence of a repeated character that is listed in SET1 (the blank space) with a single occurrence of that character.
Use of squeeze repeats used to after substitute tabs:
尝试如下:
字符类 [:space:] 将匹配所有空白(空格、制表符等)。 如果您只想替换单个字符,例如。 只是空间,仅使用它。
编辑:实际上 [:space:] 包含回车符,所以这可能不会达到您想要的效果。 以下内容将替换制表符和空格。
。
在所有这些中,您需要小心文件中由空格分隔的项目不包含您想要保留的自己的空格,例如 两个字。
Try something like:
The character class [:space:] will match all whitespace (spaces, tabs, etc.). If you just want to replace a single character, eg. just space, use that only.
EDIT: Actually [:space:] includes carriage return, so this may not do what you want. The following will replace tabs and spaces.
as will
In all of this, you need to be careful that the items in your file that are separated by whitespace don't contain their own whitespace that you want to keep, eg. two words.
不查看您的输入文件,仅猜测
重定向到另一个文件并根据需要重命名
without looking at your input file, only a guess
redirect to another file and rename as needed
像这样的东西怎么样:
(是的,有一些无用的 catting 和管道;我想也可以使用 < 直接从文件中读取 - 首先使用 cat 来输出文件的内容,并且只有在之后,我将 sed 添加到我的命令行中)
编辑: 正如 @ghostdog74 在评论中指出的那样,绝对不需要 cat/pipe ; 您可以将文件名提供给 sed :
如果“texte.txt”是这样的:
您将得到一个“texte-new.txt”,如下所示:
我不会只替换旧文件通过新的(如果我没记错的话,可以使用 sed -i 来完成;正如 @ghostdog74 所说,这个会接受动态创建备份):保留可能是明智的,因为安全措施(即使这意味着必须将其重命名为“texte-backup.txt”之类的名称)
What about something like this :
(Yes, with some useless catting and piping ; could also use < to read from the file directly, I suppose -- used cat first to output the content of the file, and only after, I added sed to my command-line)
EDIT : as @ghostdog74 pointed out in a comment, there's definitly no need for thet cat/pipe ; you can give the name of the file to sed :
If "texte.txt" is this way :
You'll get a "texte-new.txt" that'll look like this :
I wouldn't go just replacing the old file by the new one (could be done with sed -i, if I remember correctly ; and as @ghostdog74 said, this one would accept creating the backup on the fly) : keeping might be wise, as a security measure (even if it means having to rename it to something like "texte-backup.txt")
此命令应该有效:
请注意,您必须将输出重定向到新文件。 输入文件未就地更改。
This command should work:
Note that you have to redirect the output to a new file. The input file is not changed in place.