在 Linux 中的文本文件中用逗号替换空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1271222/
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
Replace whitespace with a comma in a text file in Linux
提问by
I need to edit a few text files (an output from sar
) and convert them into CSV files.
我需要编辑一些文本文件(来自 的输出sar
)并将它们转换为 CSV 文件。
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).
我需要使用 sed 或 awk 函数(Linux 中的简单 shell 脚本)更改每个空格(可能是输出中数字之间的选项卡)。
Can anyone help me? Every command I used didn't change the file at all; I tried gsub
.
谁能帮我?我使用的每个命令都没有更改文件;我试过了gsub
。
回答by ghostdog74
without looking at your input file, only a guess
不看你的输入文件,只是猜测
awk '{=}1' OFS=","
redirect to another file and rename as needed
重定向到另一个文件并根据需要重命名
回答by Pascal MARTIN
What about something like this :
这样的事情怎么样:
cat texte.txt | sed -e 's/\s/,/g' > texte-new.txt
(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)
(是的,有一些无用的 catting 和管道;也可以使用 < 直接从文件中读取,我想——首先使用 cat 来输出文件的内容,然后才将 sed 添加到我的命令行中)
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 :
编辑:正如@ghostdog74 在评论中指出的那样,绝对不需要猫/管道;您可以将文件名提供给 sed :
sed -e 's/\s/,/g' texte.txt > texte-new.txt
If "texte.txt" is this way :
如果“texte.txt”是这样的:
$ cat texte.txt
this is a text
in which I want to replace
spaces by commas
You'll get a "texte-new.txt" that'll look like this :
你会得到一个“texte-new.txt”,看起来像这样:
$ cat texte-new.txt
this,is,a,text
in,which,I,want,to,replace
spaces,by,commas
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")
我不会只用新文件替换旧文件(如果我没记错的话,可以使用 sed -i 来完成;正如@ghostdog74 所说,这个文件会接受即时创建备份):保留可能是明智的, 作为一种安全措施(即使这意味着必须将其重命名为“texte-backup.txt”)
回答by Dawie Strauss
This command should work:
此命令应该有效:
sed "s/\s/,/g" < infile.txt > outfile.txt
Note that you have to redirect the output to a new file. The input file is not changed in place.
请注意,您必须将输出重定向到新文件。输入文件未就地更改。
回答by ezpz
sed can do this:
sed 可以做到这一点:
sed 's/[\t ]/,/g' input.file
That will send to the console,
这将发送到控制台,
sed -i 's/[\t ]/,/g' input.file
will edit the file in-place
将就地编辑文件
回答by dave
Try something like:
尝试类似:
sed 's/[:space:]+/,/g' orig.txt > modified.txt
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.
字符类 [:space:] 将匹配所有空格(空格、制表符等)。如果您只想替换单个字符,例如。只是空间,只使用它。
EDIT: Actually [:space:] includes carriage return, so this may not do what you want. The following will replace tabs and spaces.
编辑:实际上 [:space:] 包括回车,所以这可能不会做你想要的。以下将替换制表符和空格。
sed 's/[:blank:]+/,/g' orig.txt > modified.txt
as will
随心所欲
sed 's/[\t ]+/,/g' orig.txt > modified.txt
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.
在所有这些中,您需要注意文件中由空格分隔的项目不包含您想要保留的自己的空格,例如。两个字。
回答by Alberto Zaccagni
tr ' ' ',' <input >output
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.
用逗号替换每个空格,如果需要,您可以使用 -s 标志(挤压重复)进行传递,该标志将 SET1(空格)中列出的重复字符的每个输入序列替换为该字符的一次出现特点。
Use of squeeze repeats used to after substitute tabs:
在替代标签之后使用挤压重复:
tr -s '\t' <input | tr '\t' ',' >output
回答by Chris Koknat
Here's a Perl script which will edit the files in-place:
这是一个 Perl 脚本,它将就地编辑文件:
perl -i.bak -lpe 's/\s+/,/g' files*
Consecutive whitespace is converted to a single comma.
Each input file is moved to .bak
连续的空格被转换为单个逗号。
每个输入文件都移动到 .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
-i.bak
就地编辑并制作 .bak 副本-p
循环输入文件的每一行,自动打印该行-l
在处理之前删除换行符,然后将它们添加回-e
执行perl代码
回答by H.Alzy
If you want to replace an arbitrary sequence of blank characters (tab, space) with one comma, use the following:
如果您想用一个逗号替换任意序列的空白字符(制表符、空格),请使用以下命令:
sed 's/[\t ]+/,/g' input_file > output_file
or
或者
sed -r 's/[[:blank:]]+/,/g' input_file > output_file
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:
如果您的某些输入行包含多余的不需要转换为逗号的前导空格字符,那么您首先需要删除它们,然后将剩余的空白字符转换为逗号。对于这种情况,请使用以下方法:
sed 's/ +//' input_file | sed 's/[\t ]+/,/g' > output_file