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

查看:3073
本文介绍了在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 

用逗号替换每个空格,if您需要可以通过-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 '\t' <input | tr '\t' ',' >output 

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

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