How to parse fixed-width columns which may include empty fields?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to parse fixed-width columns which may include empty fields?
# 8  
Old 08-02-2012
I prefer no space between the two ;; I had just used that to highlight the empty field.
# 9  
Old 08-02-2012
I think we've had a problem communicating because the example input doesn't match the sample results you said that you want to see. When you said the input was truncated, I think most of us thought you were giving us complete lines (but just giving us a few lines; not that you were also shortening fields in the few lines you gave us). And then showing a <space> between semicolons in sample output when you didn't want the space there caused more confusion.

I think you'll get what you want if you take the code supplied by binlib modified to use the field start and end points specified in your first message, and modified to use ";" instead of "," as the output field separator:
Code:
awk '
BEGIN {
  n = split("1-9,11-48,60-68,112-142,143-159", a, "[^0-9]") - 1
  for (i = 1; i <= n; i += 2)
    l[i] = a[i+1] - (p[i] = a[i]) + 1
}
{
  for (i = 1; i <= n; i += 2) {
    x = substr($0, p[i], l[i])
    sub(/ +$/, "", x)
    printf("%s%s", x, (i < n)? ";" : "\n")
  }
}'

If you're having trouble following the initial set up in the BEGIN clause or the for loop in the main body of the script, another way to do it would be:
Code:
awk '{
	out=sprintf("%s;%s;%s;%s;%s", substr($0,1,8), substr($0,11,36),
		substr($0,60,7), substr($0,112,29), substr($0,143))
	gsub(" ","",out)
	print out
}'

This User Gave Thanks to Don Cragun For This Post:
# 10  
Old 08-03-2012
My apologies for posting different sample output. I agree, that was confusing.

And that works!!! Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Request: How to Parse dynamic SQL query to pad extra columns to match the fixed number of columns

Hello All, I have a requirement in which i will be given a sql query as input in a file with dynamic number of columns. For example some times i will get 5 columns, some times 8 columns etc up to 20 columns. So my requirement is to generate a output query which will have 20 columns all the... (7 Replies)
Discussion started by: vikas_trl
7 Replies

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

3. Shell Programming and Scripting

Removing duplicates in fixed width file which has multiple key columns

Hi All , I have a requirement where I need to remove duplicates from a fixed width file which has multiple key columns .Also , need to capture the duplicate records into another file . File has 8 columns. Key columns are col1 and col2. Col1 has the length of 8 col 2 has the length of 3. ... (5 Replies)
Discussion started by: saj
5 Replies

4. UNIX for Dummies Questions & Answers

Filling the empty columns in a fixed column file

Hi, I have a file with fixed number of columns (total 58 columns) delimeted by pipe (|). Due to a bug in the application the export file does not come with fixed number of columns. The missing data columns are being replaced by blank in the output file. In one line I can have 25 columns (33... (1 Reply)
Discussion started by: yale_work
1 Replies

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

6. Shell Programming and Scripting

Printing Fixed Width Columns

Hi everyone, I have been working on a pretty laborious shellscript (with bash) the last couple weeks that parses my firewall policies (from a Juniper) for me and creates a nifty little columned output. It does so using awk on a line by line basis to pull out the appropriate pieces of each... (4 Replies)
Discussion started by: cixelsyd
4 Replies

7. Shell Programming and Scripting

Removing \n within a fixed width record

I am trying to remove a line feed (\n) within a fixed width record. I tried the tr -d ‘\n' command, but it also removes the record delimiter. Is there a way to remove the line feed without removing the record delimiter? (10 Replies)
Discussion started by: CKT_newbie88
10 Replies

8. Shell Programming and Scripting

summing up the fields in fixed width file

Hi, I have a fixed width file with some records as given below: " 1000Nalsdjflj243324jljlj" "-0300Njfowjljl309933fsf" " 0010Njsfsjklj342344fsl" I want to sum-up first field values(i.e from 2nd character to 6th character)of each record. so for the above file i want to add (1000 - 300+... (2 Replies)
Discussion started by: srilaxmi
2 Replies

9. Shell Programming and Scripting

Combining Two fixed width columns to a variable length file

Hi, I have two files. File1: File1 contains two fixed width columns ID of 15 characters length and Name is of 100 characters length. ID Name 1-43<<11 spaces>>Swapna<<94 spaces>> 1-234<<10 spaces>>Mani<<96 spaces>> 1-3456<<9 spaces>>Kapil<<95 spaces>> File2: ... (4 Replies)
Discussion started by: manneni prakash
4 Replies

10. Shell Programming and Scripting

Extracting records with unique fields from a fixed width txt file

Greetings, I would like to extract records from a fixed width text file that have unique field elements. Data is structured like this: John A Smith NY Mary C Jones WA Adam J Clark PA Mary Jones WA Fieldname / start-end position Firstname 1-10... (8 Replies)
Discussion started by: sitney
8 Replies
Login or Register to Ask a Question