van Ness
van Ness

Reputation: 75

awk remove characters after number

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

Upvotes: 3

Views: 2538

Answers (3)

Idriss Neumann
Idriss Neumann

Reputation: 3838

Another possible solution :

awk -F "-+$" '{str=""; for(i=1; i<=NF; i++){str=str""$i} print str}' file

But, I think that sed is a better solution in this case.

Regards, Idriss

Upvotes: 1

anubhava
anubhava

Reputation: 786091

Using awk:

awk -F '-+$' '{$1=$1} 1' file

Using sed:

sed -i.bak 's/-*$//' file

Upvotes: 2

Birei
Birei

Reputation: 36282

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

Upvotes: 3

Related Questions