有没有办法转换一些“|” (不是全部)在linux中的texfile中使用逗号?

时间:2018-03-25 18:41:35

标签: linux ubuntu sed

我有一个文本文件,看起来像这样:

1|Name..........
2|Name........

我希望它看起来像这样:

1,Name.......
2,Name.......

现在问题是该名称有时也包含|字符,我不想更改它们。 有什么办法可以改变整数后出现的|个字符吗?

我尝试使用sed,但无法弄清楚如何操作。

2 个答案:

答案 0 :(得分:0)

Replace first | with ,:

sed 's/|/,/' file`

答案 1 :(得分:0)

use simple awk for it too.

awk '{sub("|",",")} 1' Input_file

Explanation of above code: Using sub utility of awk which does the substitution of regex provided to it in a line/variable. It's patter is sub(source_value,new_value,line/variable). Then providing 1 means I am making condition here TRUE for awk and NOT mentioning any action to happen so by default action will happen which is printing of the current line.

In case you want to save the changed inside Input_file itself then use following.

awk '{sub("|",",")} 1' Input_file > temp_file && mv temp_file Input_file