Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manipulate awk output?

Tags:

linux

bash

awk

If I type

echo '"";"";" 01.06.2011";"7";"01.06";"-21,00";"-6.097,73";' | awk -F';' '{print $3 " " $7}'

then I get

" 01.06.2011" "-6.097,73"

but what I would like is

" 01.06.2011" "-6097"

How should that be done?

like image 505
Sandra Schlichting Avatar asked Feb 12 '12 22:02

Sandra Schlichting


1 Answers

AWK supports the printf function, and, from memory, parses as much of a string as an integer as it can. So, from your example:

awk -F';' '{printf("%s %i", $3, $7*1000);}'

would work. This will work for any size number, too…

(above doesn't work due to double quotes in the data, see below…)

EDIT: If you're using gawk, you can achieve this stably using a combination of printf and strtonum.

awk -F';' '{ gsub(/"/, "", $7); printf("%s \"%i\"\n", $3, strtonum($7)*1000); }'

The gsub() call strips off the quotes so that strtonum doesn't choke on them, and then the printf call puts them back as part of the format string…

like image 57
andrewdotnich Avatar answered Sep 20 '22 17:09

andrewdotnich