Modifying input text file using awk and sed

April 26, 2010 at 08:21:42
Specs: Windows Vista
I have a requirement to modify the input file
fields data with the preconditions..

Below is the problem description:

I have an input text file with 10 columns and it
has decimal values in it. My requirement is to
modify those decimal values by appending 0s
and changing the last digit depending on the
given calculation table.
My input text file look like:

ABC,145,PO BOX 10 00625

and now 5th and 6th columns has to be
represented in 11 bytes by appending 0s to
Also the last digit and sign change the 11th
digit in new file.

Eg: 125.32 should be 0000001253B
-51.04 should be 0000000510M
66.24 should be 0000000662D
-12.10 should be 0000000121}
i.e. the positive number and last digit will give
one character.
negative number and last digit will give another
like positive and 2 in last digit will be
represented as B
negative and 4 in last digit will be represented
as M.

Can I use the awk and sed to achieve this?
Much appreciate any help in this regard.
Thanks in advance for any suggestions.

See More: Modifying input text file using awk and sed

Report •

April 27, 2010 at 06:28:49
You are asking for a lot of data parsing which can be done in awk. I do not have time to do the entire project for you, but I have done some work with field 5 to get you started:


awk ' BEGIN { FS="," }

# get the absolute value of field 5
if($5 < 0)
   $5=(-1) * $5

# get the last character of field 5
print a
#$5=$5 + ""

# remove the decimal pt from field 5

# print field 5 with a width of 10 characters
printf("%010d\n", $5)

} ' data.txt

Report •

April 27, 2010 at 10:29:20
Thanks for the response nails. I will try to implement your answer
and update here with my response soon.

Report •
Related Solutions

Ask Question