Conversion problem with date field and formatting.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Conversion problem with date field and formatting.
# 1  
Old 05-27-2009
Conversion problem with date field and formatting.

Hi,

My input file contains the record(s) as below with space as FS.

01-01024180 35000 MV010 02/03/09 0306 03060226 03
02-00410330 470000 MV010 02/03/09 0301 03010276 03

1. I need to convert the field 02/03/09 (dd/mm/yy) to yyyymmdd yet retain the Field separator.

Using the modified code below(previous solution by Vgersh99 to Debian):

BEGIN {
FS=OFS=" "
PAT_percent="%"
# 'LENGTH of the fields'
split("-15 17 5 8 5 15 2", arrL, " ")
# 'TYPE of the fields"
split("s d s d s d s", arrT, " ")
}

{
for(i=1; i <= NF; i++) {
format=sprintf("%c%c%d%s%s", PAT_percent, (arrT[i] == "s") ? "-" : "0", arrL
[i], arrT[i], (i<NF) ? OFS : "\n")
printf(format, $i)
}
}

I get the output as:
01-01024180 00000000000035000 MV010 00000002 0306 000000003060226 03
02-00410330 00000000000470000 MV010 00000002 0301 000000003010276 03

Yet my intention is to get an output like:
01-01024180000000000000000035000MV010200903020030600000000306022603
02-00410330000000000000000470000MV010200903020030100000000301027603

Each record appearing as one single continuous line without field separators.
Would someone kindly guide.Smilie
Much Regards!

Pawee.
# 2  
Old 05-27-2009
Code:
# t.awk
awk 'BEGIN {OFS="" }{ split($4,arr,"/"); 
                      $4=sprintf("%d%s%s",arr[2]+2000,arr[1],arr[0])
                      print $0} ' filename

output:
Code:
/home/jmcnama> t.awk  
01-0102418035000MV01020030203060306022603
02-00410330470000MV01020030203010301027603

/home/jmcnama> cat filename
01-01024180 35000 MV010 02/03/09 0306 03060226 03
02-00410330 470000 MV010 02/03/09 0301 03010276 03

# 3  
Old 05-28-2009
Hi,

Kindly assist by analyzing my code below and suggest the changes to achieve the required output.

The input file:
01-010241800000 35000 MV010 02/03/09 0306 03060226 03
02-004103300000 470000 MV010 02/03/09 0301 03010276 03

Then I pass it on to:

awk 'BEGIN {OFS=" " }

{ split($4,arr,"/");
$4=sprintf("%d%s%s",arr[2]+2000,arr[1],arr[0])
print $0} '

The output comes as:
01-010241800000 35000 MV010 200302 0306 03060226 03
02-004103300000 470000 MV010 200302 0301 03010276 03

sprintf, truncates the 2009 and instead returns a value of 20 for arr[2]. I guess arr[2] should be of length 4 as opposed to it's original length of 2. Would someone kindly help decipher why I am losing the "09" on arr[2]?


Much Regards

Pawee.

Last edited by talk2pawee; 06-02-2009 at 07:39 AM.. Reason: Make the issue much clearer for support analysis.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Update a field using awk and keep the formatting.

Look at this simple example. echo " 2 4 6" | awk '{$2+=3;$3-=1}1' 2 7 5 Is there a simple way to update a field and at the same time keep the formatting? I would like to get it like this 2 7 5 I have tested both sub and gsub, it reformat too. (2 Replies)
Discussion started by: Jotne
2 Replies

2. Linux

How do I format a Date field of a .CSV file with multiple commas in a string field?

I have a .CSV file (file.csv) whose data are all enclosed in double quotes. Sample format of the file is as below: column1,column2,column3,column4,column5,column6, column7, Column8, Column9, Column10 "12","B000QRIGJ4","4432","string with quotes, and with a comma, and colon: in... (3 Replies)
Discussion started by: dhruuv369
3 Replies

3. Shell Programming and Scripting

Adding a field to a file using a conversion table

Hello everyone, Here is what i am trying to accomplish. I have a transaction log that I want to to add a field. The fields in the transaction log are tab delimited FYI. My goal is to add a column specifying the category/type to each item purchased. I have created a two column "conversion table"... (2 Replies)
Discussion started by: SpencerClark
2 Replies

4. Shell Programming and Scripting

Julian date to Calendar date conversion

Hi all, I require to convert julian date to normal calander date in unix for eg julian date=122 now i want corresponding calander date ---------------------------------------- gr8 if give very small command/script and please explain the steps as well(imp) Thanks ... (3 Replies)
Discussion started by: RahulJoshi
3 Replies

5. Shell Programming and Scripting

How to check field formatting of input file?

Hi, I had input file with below data, abcdefghij;20100903040607;1234567891;GLOBAL; Having values of fields with seperated by semi-colon (;) and ended with line feed (\n). Through shell script, how can I check the field formatting? Thanks in advance. (18 Replies)
Discussion started by: Poonamol
18 Replies

6. Shell Programming and Scripting

Delimted to padded conversion with unknown field length

I’m looking for an elegant way to convert a delimited file (comma delimited in this case) to padded columns (for printing in non-proportional font) but the length of each column is not known ahead of time. It needs to be calculated for each column from the longest entry in that column in a given... (3 Replies)
Discussion started by: Michael Stora
3 Replies

7. Shell Programming and Scripting

Conversion of date to Julian date

Hi Gurus, Need help in Conversion of date(2007-11-30) to Julian date(YYDDD)... '+%J' 2007-11-30 to 'YYDDD' Thanks (4 Replies)
Discussion started by: SeenuGuddu
4 Replies

8. Shell Programming and Scripting

CSV formatting with prefixing, appending and padding field

Hi I have a very large csv file with some hundreds of thousands of rows of data. The data is in the following format: Up to four alpha numeric characters for the first word. This is either set as 2 characters followed by 2 spaces, or as a single 4character word. This is then followed by an 8... (7 Replies)
Discussion started by: meself
7 Replies

9. Shell Programming and Scripting

Date Conversion

Hi, Does anyone know (in KSH, CSH, SED or AWK), how to convert date text in a file from: EX: May232008 to: 05232008 Thanks, (3 Replies)
Discussion started by: jgrant746
3 Replies

10. Shell Programming and Scripting

date conversion

Hi everybody: Could anybody tell me how I convert from a julian date, with shell comands, to gregorian. Thanks in advance. (2 Replies)
Discussion started by: tonet
2 Replies
Login or Register to Ask a Question