Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

awk remove characters after number

Tags:

regex

bash

sed

awk

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

like image 358
van Ness Avatar asked Dec 10 '25 23:12

van Ness


2 Answers

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
like image 52
Birei Avatar answered Dec 12 '25 15:12

Birei


Using awk:

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

Using sed:

sed -i.bak 's/-*$//' file
like image 23
anubhava Avatar answered Dec 12 '25 16:12

anubhava