Visit Our UNIX and Linux User Community


Fixed length fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Fixed length fields
# 1  
Old 05-05-2011
Fixed length fields

HPUX and posix shell

Hi all.

I have a record with fixed length fields....I would like to reorder the fields and preserver the fixed lengths....

cat test
Code:
4 960025460           Dept of Music                                           
8 960025248           Dept of Music 12-08

cat newtest
Code:
960025460            4 Dept of Music                                           
960025248            8 Dept of Music 12-08

I can get it done with this awk line, but I need to preserve the field lengths....this command will not.
Code:
awk 'begin{fieldwidths = "1 51 49"}; {print $2 $1 $3}' test > newtest

Anyone have a sed line or a better awk line?

TIA!!!!
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 05-05-2011 at 05:58 PM.. Reason: code tags, please!
# 2  
Old 05-05-2011
Code:
sed 's/^\(.\{2\}\)\(.\{10\}\)/\2\1/' test >newtest


Last edited by Scott; 05-05-2011 at 06:04 PM.. Reason: Code tags...
# 3  
Old 05-05-2011
A bit verbose, but....
Code:
gawk 'BEGIN{FIELDWIDTHS="1 51 49";orderN=split("2 1 3", orderA)}; {for(i=1;i in orderA;i++)printf("%*s%c",FIELDWIDTH[i],$orderA[i],(i==orderN)?ORS:"") }' myFile

# 4  
Old 05-05-2011
Code:
awk '{print substr($0,3,20) substr($0,1,2) substr($0,23)}' infile


Previous Thread | Next Thread
Test Your Knowledge in Computers #863
Difficulty: Medium
Vue features a reactivity system that uses jQuery objects and optimized re-rendering.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Splitting the file based on two fields - Fixed length file

Hi , I am having a scenario where I need to split the file based on two field values. The file is a fixed length file. ex: AA0998703000000000000190510095350019500010005101980301 K 0998703000000000000190510095351019500020005101480 ... (4 Replies)
Discussion started by: saj
4 Replies

2. Shell Programming and Scripting

Convert variable length record to fixed length

Hi Team, I have an issue to split the file which is having special chracter(German Char) using awk command. I have a different length records in a file. I am separating the files based on the length using awk command. The command is working fine if the record is not having any... (7 Replies)
Discussion started by: Anthuvan
7 Replies

3. Shell Programming and Scripting

changing a variable length text to a fixed length

Hi, Can anyone help with a effective solution ? I need to change a variable length text field (between 1 - 18 characters) to a fixed length text of 18 characters with the unused portion, at the end, filled with spaces. The text field is actually field 10 of a .csv file however I could cut... (7 Replies)
Discussion started by: dc18
7 Replies

4. Shell Programming and Scripting

Make variable length record a fixed length

Very, very new to unix scripting and have a unique situation. I have a file of records that contain 3 records types: (H)eader Records (D)etail Records (T)railer Records The Detail records are 82 bytes in length which is perfect. The Header and Trailer records sometimes are 82 bytes in... (3 Replies)
Discussion started by: jclanc8
3 Replies

5. Shell Programming and Scripting

Need awk script to compare 2 fields in fixed length file.

Need a script that manipulates a fixed length file that will compare 2 fields in that file and if they are equal write that line to a new file. i.e. If fields 87-93 = fields 119-125, then write the entire line to a new file. Do this for every line in the file. After we get only the fields... (1 Reply)
Discussion started by: Muga801
1 Replies

6. UNIX for Dummies Questions & Answers

Convert a tab delimited/variable length file to fixed length file

Hi, all. I need to convert a file tab delimited/variable length file in AIX to a fixed lenght file delimited by spaces. This is the input file: 10200002<tab>US$ COM<tab>16/12/2008<tab>2,3775<tab>2,3783 19300978<tab>EURO<tab>16/12/2008<tab>3,28523<tab>3,28657 And this is the expected... (2 Replies)
Discussion started by: Everton_Silveir
2 Replies

7. UNIX for Dummies Questions & Answers

What the command to find out the record length of a fixed length file?

I want to find out the record length of a fixed length file? I forgot the command. Any body know? (9 Replies)
Discussion started by: tranq01
9 Replies

8. Shell Programming and Scripting

Fixed length (Fill out)

hello, I have a file that has lines with different lengts. I want this file to be filled up with a zero so that al the lines have the same length. Please advice? File 1: ----------- 2310 2009 830 1345 Result File 2: ---------- 2310 2009 0830 (3 Replies)
Discussion started by: peterk
3 Replies

9. Shell Programming and Scripting

fixed length fields in awk

I am trying to display df -h command out in proper format, how can I display each field of each record in a fixed length. (2 Replies)
Discussion started by: roopla
2 Replies

10. Shell Programming and Scripting

creating a fixed length output from a variable length input

Is there a command that sets a variable length? I have a input of a variable length field but my output for that field needs to be set to 32 char. Is there such a command? I am on a sun box running ksh Thanks (2 Replies)
Discussion started by: r1500
2 Replies

Featured Tech Videos