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
What the command to find out the record length of a fixed length file? tranq01 UNIX for Dummies Questions & Answers 9 12-04-2008 04:04 PM
Finding unique reocrds at a particular field dsravan Shell Programming and Scripting 7 12-11-2007 04:03 PM
creating a fixed length output from a variable length input r1500 Shell Programming and Scripting 2 12-03-2003 01:09 PM
Validating fixed length field... giannicello UNIX for Dummies Questions & Answers 12 05-22-2003 12:19 PM
Finding out the length of a string held within a variable dbrundrett Shell Programming and Scripting 5 02-19-2002 08:02 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 12-13-2007
dsravan dsravan is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 190
Help with finding length of a field

I have a pipe delimited file. I need to check that the first and second fields are 5 characters long and if not i need to append 0 in front of them to make them 5 characters long. can some body let mwe know how i can find the length of the two fields and then make them 5 characters long if they are not.

e.g:

Quote:
2|251|25G|NY|NY
it needs to be

Quote:
00002|00251|25G|NY|NY
  #2 (permalink)  
Old 12-13-2007
Klashxx's Avatar
Klashxx Klashxx is offline Forum Advisor  
HP-UX/Linux/Oracle
  
 

Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 393
One of the ways:

Code:
echo "2|251|25G|NY|NY"|awk '{printf("%0.5d|%0.5d|",$1,$2);print $3,$4,$5}' FS='|' OFS='|'

  #3 (permalink)  
Old 12-13-2007
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,348
Or this one:


Code:
awk 'BEGIN {FS="|"}{printf("%05d|%05d|%s|%s|%s\n",$1,$2,$3,$4,$5)}' file

Regards
  #4 (permalink)  
Old 12-13-2007
dsravan dsravan is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 190
can somebody let me know first how i can print the records that doesnt have 5 characters in the 1st and 2nd fields please?
  #5 (permalink)  
Old 12-13-2007
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131

Code:
nawk ' length($1) != 5 && length($2) != 5' myFile

  #6 (permalink)  
Old 12-13-2007
dsravan dsravan is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 190
vgreshh,

its printing everything even when the first and second fields are 5 characters long. something wrong?
  #7 (permalink)  
Old 12-13-2007
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
sorry - forgot the field seprator

Code:
nawk -F'|' length($1) != 5 || length($2) != 5'

Also yuu probably want to print a records if EITHER one of the fields is not 5 chars long?
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:57 AM.


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