在 Linux 中将文本文件中的空格替换为逗号

发布于 2024-08-01 23:19:17 字数 189 浏览 7 评论 0原文

我需要编辑一些文本文件(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 技术交流群。

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

发布评论

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

评论(9

慵挽 2024-08-08 23:19:18

sed 可以做到这一点:

sed 's/[\t ]/,/g' input.file

这将发送到控制台,

sed -i 's/[\t ]/,/g' input.file

将就地编辑文件

sed can do this:

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

温柔戏命师 2024-08-08 23:19:18

这是一个 Perl 脚本,它将就地编辑文件:

perl -i.bak -lpe 's/\s+/,/g' files*

连续的空格将转换为单个逗号。
每个输入文件都移动到 .bak

使用以下命令行选项:

  • -i.bak 就地编辑并制作 .bak 副本

  • -p 循环输入文件的每一行,自动打印该行

  • -l 在处理之前删除换行符,然后将它们添加回来

  • -e 执行 perl 代码

Here's a Perl script which will edit the files in-place:

perl -i.bak -lpe 's/\s+/,/g' files*

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

云裳 2024-08-08 23:19:18

如果您想用一个逗号替换任意序列的空白字符(制表符、空格),请使用以下命令:

sed 's/[\t ]+/,/g' input_file > output_file

或者

sed -r 's/[[:blank:]]+/,/g' input_file > output_file

如果某些输入行包含多余的前导空格字符,不需要将其转换为逗号,则首先,您需要删除它们,然后将剩余的空白字符转换为逗号。 对于这种情况,请使用以下命令:

sed 's/ +//' input_file | sed 's/[\t ]+/,/g' > output_file

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
夜访吸血鬼 2024-08-08 23:19:18

这对我有用。

sed -e 's/\s\+/,/g' input.txt >> output.csv

This worked for me.

sed -e 's/\s\+/,/g' input.txt >> output.csv
相守太难 2024-08-08 23:19:18
tr ' ' ',' <input >output 

用逗号替换每个空格,如果需要,可以使用 -s 标志(挤压重复)进行传递,这会将 SET1(空格)中列出的重复字符的每个输入序列替换为该字符的单次出现特点。

在替换选项卡后使用挤压重复:

tr -s '\t' <input | tr '\t' ',' >output 
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.

Use of squeeze repeats used to after substitute tabs:

tr -s '\t' <input | tr '\t' ',' >output 
倾城泪 2024-08-08 23:19:18

尝试如下:

sed 's/[:space:]+/,/g' orig.txt > modified.txt

字符类 [:space:] 将匹配所有空白(空格、制表符等)。 如果您只想替换单个字符,例如。 只是空间,仅使用它。

编辑:实际上 [:space:] 包含回车符,所以这可能不会达到您想要的效果。 以下内容将替换制表符和空格。

sed 's/[:blank:]+/,/g' orig.txt > modified.txt

sed 's/[\t ]+/,/g' orig.txt > modified.txt

在所有这些中,您需要小心文件中由空格分隔的项目不包含您想要保留的自己的空格,例如 两个字。

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.

EDIT: Actually [:space:] includes carriage return, so this may not do what you want. The following will replace tabs and spaces.

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.

左岸枫 2024-08-08 23:19:18

不查看您的输入文件,仅猜测

awk '{$1=$1}1' OFS=","

重定向到另一个文件并根据需要重命名

without looking at your input file, only a guess

awk '{$1=$1}1' OFS=","

redirect to another file and rename as needed

意中人 2024-08-08 23:19:18

像这样的东西怎么样:

cat texte.txt | sed -e 's/\s/,/g' > texte-new.txt

(是的,有一些无用的 catting 和管道;我想也可以使用 < 直接从文件中读取 - 首先使用 cat 来输出文件的内容,并且只有在之后,我将 sed 添加到我的命令行中)

编辑: 正如 @ghostdog74 在评论中指出的那样,绝对不需要 cat/pipe ; 您可以将文件名提供给 sed :

sed -e 's/\s/,/g' texte.txt > texte-new.txt

如果“texte.txt”是这样的:

$ cat texte.txt
this is a text
in which I want to replace
spaces by commas

您将得到一个“texte-new.txt”,如下所示:

$ cat texte-new.txt
this,is,a,text
in,which,I,want,to,replace
spaces,by,commas

我不会只替换旧文件通过新的(如果我没记错的话,可以使用 sed -i 来完成;正如 @ghostdog74 所说,这个会接受动态创建备份):保留可能是明智的,因为安全措施(即使这意味着必须将其重命名为“texte-backup.txt”之类的名称)

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)

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 :

sed -e 's/\s/,/g' texte.txt > texte-new.txt

If "texte.txt" is this way :

$ 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 :

$ 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")

仲春光 2024-08-08 23:19:18

此命令应该有效:

sed "s/\s/,/g" < infile.txt > outfile.txt

请注意,您必须将输出重定向到新文件。 输入文件未就地更改。

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.

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