简体   繁体   中英

Unix Shell Scripting removing - from file

I have a file containing following info

job, cost_code, laborcost,materialcost       
202-12-21, 23-94-23, **110.00-**, 120.04    
204-12-21, 23-93-23, 520.00, **120.04-**    
204-12-12, 24-93-23, 155.00, **120.04-**

There are few problem records specifically ones which have a -sign at the end of the line (marked with ** which isn't really there in the file).

I am trying to remove the - sign with regex but i am having trouble matching only the amount ones which have a problem.

Try this, this command removes dashes at end of line

sed s'/-$//'
echo "204-12-21, 23-93-23, 520.00, 120.04-" | sed s'/-$//'

You can use sed :

sed -i~ 's/\([0-9]\)-,/\1,/g;s/-$//' input.csv

The first expression finds a digit followed by -, and replaces it by the digit and comma; the second expression removes dashes at ends of lines.

This might work for you (GNU sed):

sed -r 's/-(,|$)/\1/g' file

Remove a hyphen before a comma or a hyphen from the last field of a line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM