Visit Our UNIX and Linux User Community


Fixed Width Join & Pad Sed/Awk Help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Fixed Width Join & Pad Sed/Awk Help
# 1  
Old 09-03-2009
Question Fixed Width Join & Pad Sed/Awk Help

I was wondering someone might be able to push me in the right direction, I am writing a script to modify fixed-width spool files, As you can see below the original spool file broke a single line into two for printability sake.

I have had been able do the joins using sed, the thing I am stumbling on is how to properly pad the description field (ex. PODIATRY) to the maximum allowable position so that visually everything still adds up.

I would love to see a sed or awk solution or suggestion, my programming skills aren't as strong as most but I would love to see an efficient solution that I could sink my teeth into and figure out why it does what it does. Thanks in advance!

Input File:

Code:
 18 PODIATRY
              539    579.00      54588.73      23284.43         40.21       698    727.00      70887.59      28976.69         39.86
 19 O/P SURGICAL FACILITY
              388    352.00     864494.44     326688.71        928.09       389    385.00    1084639.25     318630.46        827.61
 38 THERAPY
             3111   3642.00     261919.67      61583.00         16.91      3150   3531.00     241123.29      70527.76         19.97
 39 DURABLE MEDICAL EQUIPMENT

Output:
Code:
 18 PODIATRY                               539    579.00      54588.73      23284.43         40.21       698    727.00      70887.59      28976.69         39.86
 19 O/P SURGICAL FACILITY                  388    352.00     864494.44     326688.71        928.09       389    385.00    1084639.25     318630.46        827.61
 38 THERAPY                               3111   3642.00     261919.67      61583.00         16.91      3150   3531.00     241123.29      70527.76         19.97
 39 DURABLE MEDICAL EQUIPMENT              143    967.00      31939.99      12069.13         12.48       172    976.00      47575.54      20028.77         20.52

# 2  
Old 09-03-2009
kinda 'ugly' ('cause you have the embedded spaces in the SECOND 'field'), but.....
Code:
nawk '
  BEGIN {max=25}
  FNR%2{gsub(" ","_");$0=sprintf("%-*s",max,$0);gsub("_", " ");printf;next}
  1' myFile

# 3  
Old 09-03-2009
Quote:
Originally Posted by vgersh99
kinda 'ugly' ('cause you have the embedded spaces in the SECOND 'field'), but.....
Code:
nawk '
  BEGIN {max=25}
  FNR%2{gsub(" ","_");$0=sprintf("%-*s",max,$0);gsub("_", " ");printf;next}
  1' myFile

Thanks for the reply, I seem to be encountering the following error:
nawk: line 3: no arguments in call to printf
# 4  
Old 09-03-2009
your nawk seems finicky :
Code:
nawk '
  BEGIN {max=25}
  FNR%2{gsub(" ","_");$0=sprintf("%-*s",max,$0);gsub("_", " ");printf $0;next}
  1' myFile

# 5  
Old 09-03-2009
Bug

Quote:
Originally Posted by vgersh99
your nawk seems finicky :
Code:
nawk '
  BEGIN {max=25}
  FNR%2{gsub(" ","_");$0=sprintf("%-*s",max,$0);gsub("_", " ");printf $0;next}
  1' myFile

Thanks! That looks much better, your code should give me the insight I need to massage it further. Thanks again!
# 6  
Old 09-03-2009
Another one:

Code:
awk 'NR%2{s=$0;getline;printf("%-25s%s\n", s, $0)}' file

# 7  
Old 09-03-2009
along the same lines as Franklin52:
Code:
nawk '
  FNR%2{s=sprintf("%-*s",max,$0);next}
  {printf("%-*s%s\n", max,s,$0)}' max=25 myFile


Previous Thread | Next Thread
Test Your Knowledge in Computers #188
Difficulty: Easy
Python is an interpreted, high-level, general-purpose programming language, created by Linus Torvalds and first released in 1991
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk issue splitting a fixed-width file containing line feed in data

Hi Forum. I have the following script that splits a large fixed-width file into smaller multiple fixed-width files based on input segment type. The main command in the script is: awk -v search_col_pos=$search_col_pos -v search_str_len=$search_str_len -v segment_type="$segment_type"... (8 Replies)
Discussion started by: pchang
8 Replies

2. Shell Programming and Scripting

Replace using awk on fixed width file.

All, I used to use following command to replace specific location in a fixed width file. Recently looks like my command stopped working as intended. We are on AIX unix. awk 'function repl(s,f,t,v) { return substr(s,1,f-1) sprintf("%-*s", t-f+1, v) substr(s,t+1) } NR<=10 {... (3 Replies)
Discussion started by: pinnacle
3 Replies

3. Shell Programming and Scripting

Print column details from fixed width file using awk command

hi, i have a fixed width file with multiple columns and need to print data using awk command. i use: awk -F "|" '($5 == BH) {print $1,$2,$3}' <non_AIM target>.txt for a delimiter file. but now i have a fixed width file like below: 7518 8269511BH 20141224951050N8262 11148 8269511BH... (5 Replies)
Discussion started by: kcdg859
5 Replies

4. Shell Programming and Scripting

Alter Fixed Width File

Thank u so much .Its working fine as expected. ---------- Post updated at 03:41 PM ---------- Previous update was at 01:46 PM ---------- I need one more help. I have another file(fixed length) that will get negative value (ex:-00000000003000) in postion (98 - 112) then i have to... (6 Replies)
Discussion started by: vinus
6 Replies

5. Shell Programming and Scripting

Left pad spaces using awk or sed

Hi,I've a unix pipe delimited file as below f1|f2|f3|f4|f5|f6 My requirement is to pad spaces on the left to fields f2, f3 and f5. Field Lengths according to file layout f2 - 4 char f3 - 5 char f5 - 3 char If my record is as below 1|43|bc|h0|34|a Output record should be as below 1| 43| bc|h0|... (4 Replies)
Discussion started by: Soujanya_K
4 Replies

6. Shell Programming and Scripting

variable fixed-width fields

Hi there, CTL Port IO Rate(IOPS) Read Rate(IOPS) Write Rate(IOPS) Read Hit(%) Write Hit(%) Trans. Rate(MB/S) Read Trans. Rate(MB/S) Write Trans. Rate(MB/S) 09:36:48 0 A 136 0 135 97 100 ... (6 Replies)
Discussion started by: gray380
6 Replies

7. Shell Programming and Scripting

awk: creating a fixed-width single file from 2 different files

I have to create a single file from three files, Please see below for samples: day.txt 20090101 20090102 item.txt 123456789101 12345678910209 1234567891 str.txt 1 12 123 output.txt 20090101123456789101 1 0 2009010112345678910209 12 ... (2 Replies)
Discussion started by: tamahomekarasu
2 Replies

8. Shell Programming and Scripting

Appending string (charachters inside the line) to a fixed width file using awk or sed

Source File: abcdefghijklmnop01qrstuvwxyz abcdefghijklmnop02qrstuvwxyz abcdefghijklmnop03qrstuvwxyz abcdefghijklmnop04qrstuvwxyz abcdefghijklmnop05qrstuvwxyz Whatever characters are in 17-18 on each line of the file, it should be concatenated to the same line at the character number... (6 Replies)
Discussion started by: tamahomekarasu
6 Replies

9. Shell Programming and Scripting

edit entire column from a fixed-width file using awk or sed

Col1 Col2 Col3 Col4 12 Completed 08 0830 12 In Progress 09 0829 11 For F U 07 0828 Considering the file above, how could i replace the third column the most efficient way? The actual file size is almost 1G. I am... (10 Replies)
Discussion started by: tamahomekarasu
10 Replies

10. UNIX for Dummies Questions & Answers

Fixed Width file using AWK

I am using the following command at the Unix prompt to make my 'infile' into a fixed width file of 100 characters. awk '{printf "%-100s\n",$0}' infile > outfile However, there are some records with a special character "" These records are using 3 characters in place of one and my record... (2 Replies)
Discussion started by: alok.benjwal
2 Replies

Featured Tech Videos