在 Linux 中的文本文件中用逗号替换空格 [英] Replace whitespace with a comma in a text file in Linux

查看:17
本文介绍了在 Linux 中的文本文件中用逗号替换空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编辑一些文本文件(来自 sar 的输出)并将它们转换为 CSV 文件.

I need to edit a few text files (an output from sar) and convert them into CSV files.

我需要使用 sed 或 awk 函数(Linux 中的简单 shell 脚本)更改每个空格(可能是输出中数字之间的选项卡).

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).

谁能帮帮我?我使用的每个命令都没有更改文件;我试过 gsub.

Can anyone help me? Every command I used didn't change the file at all; I tried gsub.

推荐答案

tr ' ' ',' <input >output 

用逗号替换每个空格,如果需要,您可以使用 -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:

tr -s '	' <input | tr '	' ',' >output 

这篇关于在 Linux 中的文本文件中用逗号替换空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆