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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Awk - Working with fixed length files ambroze Shell Programming and Scripting 3 05-21-2008 06:32 PM
What the command to find out the record length of a fixed length file? tranq01 UNIX for Dummies Questions & Answers 3 10-19-2007 11:16 AM
Join two fixed length Files in Unix gharikrishnan Shell Programming and Scripting 6 03-21-2007 09:43 AM
sort on fixed length files sach_in Shell Programming and Scripting 6 10-26-2006 06:37 AM
creating a fixed length output from a variable length input r1500 Shell Programming and Scripting 2 12-03-2003 09:09 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-05-2007
Registered User
 

Join Date: Dec 2006
Posts: 29
Awk with fixed length files

Hi Unix Champs,

I want to awk on a fixed length file.
Instead if the file was a delimited file, then I could have used -F and then could have easily done manipulation on the fields.
How do i do the same in case of fixed length file?

Thanks in Advance.

Regards.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 01-05-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,573
Sorry!

Whats your actual requirement?

Could you please post sample Input and Output?
Reply With Quote
  #3 (permalink)  
Old 01-06-2007
Registered User
 

Join Date: Dec 2006
Posts: 29
Say the file format is like:

Record length: 1-30

Fields 1-10 > Emp ID
Fields 11-20 > Emp Name
Fields 21-30 > Contact No

I want 2 perform some awk operations.
Is there any option like -F is case of delimited file.
Reply With Quote
  #4 (permalink)  
Old 01-06-2007
tayyabq8's Avatar
UNIX Hobbyist
 

Join Date: Nov 2004
Location: /World/Asia/MiddleEast/Kuwait/Salmiya
Posts: 528
Still your requirement is not clear. I'll ask you to put here some sample input and output you require. But for fixed length situation you can try use substr function in awk, like substr($0,1,10) for emp ID and substr($0,11,10) for emp name and so on.
Reply With Quote
  #5 (permalink)  
Old 01-06-2007
Registered User
 

Join Date: Dec 2006
Posts: 29
There are going to be a lot of manipulations only if couple of fields' values.

Now if the file was a delimited, i could have written something like

awk '$2=something && $3=somthingelse { some action }'

But now since the file is fixed length, a lot of processing processing time will be wasted just on substring and then check wheten to perform the acton. Any solution?
Reply With Quote
  #6 (permalink)  
Old 01-06-2007
tayyabq8's Avatar
UNIX Hobbyist
 

Join Date: Nov 2004
Location: /World/Asia/MiddleEast/Kuwait/Salmiya
Posts: 528
Post sample input and sample output, no one can suggest you until you clarify your requirements.
Reply With Quote
  #7 (permalink)  
Old 01-06-2007
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,453
Quote:
Originally Posted by c2b2

But now since the file is fixed length, a lot of processing processing time will be wasted just on substring and then check wheten to perform the acton. Any solution?
"Don't use fixed length files" pops quickly to mind. "Switch to tools with support for fixed length files" is another thought. perl is probably the most widespread language with fixed field support.

If you must use awk on a fixed field file, maybe you could insert delimiters first:
Code:
$ cat input
111111111122222222223333333333
aaaaaaaaaabbbbbbbbbbcccccccccc
qqqqqqqqqqggggggggggtttttttttt
$ sed 's/\(.\{10\}\)\(.\{10\}\)\(.\{10\}\)/\1-\2-\3/' < input
1111111111-2222222222-3333333333
aaaaaaaaaa-bbbbbbbbbb-cccccccccc
qqqqqqqqqq-gggggggggg-tttttttttt
$
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:32 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0