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
0123,1234567891012,125.32,-51.04,31-Dec-10
DEF,234,Addr1addr2zipzip4,1234512345123,6
6.24,-12.10,31-Oct-10
name,100,someaddrzipzip4,9876543212345,0,
25.50,12-Dec-12

and now 5th and 6th columns has to be
represented in 11 bytes by appending 0s to
left.
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
character.
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 •

#1
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:

#!/bin/ksh

awk ' BEGIN { FS="," }
{

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

# get the last character of field 5
a=substr($5,length($5),1)
print a
#$5=$5 + ""

# remove the decimal pt from field 5
gsub(/\./,"",$5)

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

} ' data.txt



Report •

#2
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