The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
To convert multi format file to a readable ascii format gaur.deepti UNIX for Dummies Questions & Answers 5 03-25-2008 03:03 PM
change the format of a giving file ( a bit challenge) Thank you netbanker Shell Programming and Scripting 5 12-04-2007 02:49 PM
Giving input to a c++ file tonyaim83 Shell Programming and Scripting 2 10-23-2007 02:10 AM
Convert UTF8 Format file to ANSI format rajreddy UNIX for Dummies Questions & Answers 9 05-25-2007 09:26 AM
Convert UTF8 Format file to ANSI format rajreddy UNIX for Advanced & Expert Users 1 05-24-2007 07:40 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-15-2005
DebianJ DebianJ is offline
Registered User
  
 

Join Date: May 2005
Posts: 52
Giving format to a file

hi all,

i have a big problem and i don't know what to do. This is the thing: i have a flat file with 26 fields, which are separated by ';'
by example, i have this:

Peter;Smith;2005;200508; .........

if the lengths of the fields are:
field 1: Alphanumeric[10] - field 2: Alphanumeric[10] - field 3: numeric[8] - field 4: numeric [10] - etc ....
i want this:

Peter ;Smith ;00002005;0000200508;

I need giving format to each field, to get the length of each field, which is established. if the field is numeric, i must fill with zeroes to the left of the number, until the length of the field is completed. if the field is alphanumeric, i must field whit blank spaces to the rigth of the value.

an important thing, is what i must keep the field separator (.

how can i do that¿?
please, help me
thanks for your help
  #2 (permalink)  
Old 11-15-2005
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
given a sample file debian.txt

Code:
Peter;Smith;2005;200508

and a script debian.awk

Code:
BEGIN {
  FS=OFS=";"
  PAT_percent="%"
      # 'LENGTH of the fields'
  split("10 10 8 10", arrL, " ")
      # 'TYPE of the fields"
  split("s s d d", 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)
 }
}

running the script as:
nawk -f debian.awk debian.txt

produces:

Code:
Peter     ;Smith     ;00002005;0000200508


Last edited by vgersh99; 11-15-2005 at 03:00 PM..
  #3 (permalink)  
Old 11-17-2005
DebianJ DebianJ is offline
Registered User
  
 

Join Date: May 2005
Posts: 52
thanks

thanks vgersh99 - it works ok
  #4 (permalink)  
Old 03-06-2008
Sreejith_VK
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
Hi,

I tried vgersh's code.but why is that i getting everything aligned right?
I am new to awk.
How can align everything left.
  #5 (permalink)  
Old 03-06-2008
Sreejith_VK
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
At last I found the answer to my question.
{The change is in bold}
Code:
Quote:
BEGIN {
FS=OFS=";"
PAT_percent="%"
# 'LENGTH of the fields'
split("-10 -10 -8 -10", arrL, " ")
# 'TYPE of the fields"
split("s s d d", 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)
}
}
This will align your output to left.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:25 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0