在UNIX中执行tr命令后带有负号的问题 [英] Issue with negative sign after executing the tr command in UNIX
问题描述
我的要求是将管道分隔文件转换为正常excel。
因此,我在UNIX中使用以下tr命令来执行此操作。
tr'|'','< filename.csv> filename_Final.csv
当我执行上述命令时,它将字段的负号带到字段末尾。
所以我试图带负号前面的字段我dint找到正确匹配的UNIX脚本。
如果任何人遇到类似的情况,请帮助。
输入:管道文件
My requirement is to convert pipe separated file into normal excel. So I used the below tr command in UNIX to perform this operation. tr '|' ',' < filename.csv > filename_Final.csv when I executed the above command it brings the negative sign of the field to field end. So I tried to bring the negative sign to front of the field I dint find correct matching UNIX script. If anyone come across a similar instance, kindly help. Input: Pipe file
1|abc|-123
2|def|456
3|ijk|789
执行后
tr '|' ',' < filename.csv > filename_Final.csv
输出:
管道文件分叉到正常列
Output: Pipe file is bifurcated into normal columns
1 abc 123-
2 def 456
3 ijk 789
我的要求是将负号带到字段前面。
My requirement is to bring the negative sign to front of the field.
推荐答案
而不是 tr
,你可以使用 awk
覆盖 Output-Field-Separator
其中 |
Instead of tr
, you can just use awk
to over-write the Output-Field-Separator
which in your case is |
awk '{$1=$1}1' FS="|" OFS=" " filename.csv > filename_Final.csv
上述命令覆盖了 OFS
到单个白色空格,因此您的输入文件
The above command over-writes the OFS
to a single white-space, hence your input file
1|abc|-123
2|def|456
3|ijk|789
转换为
1 abc -123
2 def 456
3 ijk 789
从而不干扰列条目的存在。
thereby without disturbing the column entries present.
这篇关于在UNIX中执行tr命令后带有负号的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!