I'm writing a AWK script to clean up a data stream so it's usable for analysis. Right now, I have the following issue.
I have a data stream that looks like this:
56, 2
64, 3
72, 0
80, -1-
88, -3--
96, 1
04, -2-
12, -7----
20, -1-
28, 7
36, 1
44, -3--
52, 3
60, 0
68, 0
76, -3--
84, -5---
92, 1
00, 4
08, 3
16, -2-
24, -3--
32, 1
40, 3
And I want to remove any dash that occurs after a number character, keep the minus in front of the numbers, so it would look like this:
56, 2
64, 3
72, 0
80, -1
88, -3
96, 1
04, -2
12, -7
20, -1
28, 7
36, 1
44, -3
52, 3
60, 0
68, 0
76, -3
84, -5
92, 1
00, 4
08, 3
16, -2
24, -3
32, 1
40, 3
I know how to do this with sed (sed 's/-*$//'), but how could this be done with only awk so i can use it in my script? Cheers
One way, simply using sub():
awk '{ sub(/-+$/, "", $NF); print }' infile
It yields:
56, 2
64, 3
72, 0
80, -1
88, -3
96, 1
04, -2
12, -7
20, -1
28, 7
36, 1
44, -3
52, 3
60, 0
68, 0
76, -3
84, -5
92, 1
00, 4
08, 3
16, -2
24, -3
32, 1
40, 3
Using awk:
awk -F '-+$' '{$1=$1} 1' file
Using sed:
sed -i.bak 's/-*$//' file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With