Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
464 views
in Technique[技术] by (71.8m points)

csv - Issue with negative sign after executing the tr command in 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

After tr execution

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

gets converted to

1 abc -123
2 def 456
3 ijk 789

thereby without disturbing the column entries present.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...