Delimted to padded conversion with unknown field length


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delimted to padded conversion with unknown field length
# 1  
Old 07-30-2010
Delimted to padded conversion with unknown field length

I’m looking for an elegant way to convert a delimited file (comma delimited in this case) to padded columns (for printing in non-proportional font) but the length of each column is not known ahead of time. It needs to be calculated for each column from the longest entry in that column in a given file.

I can live with a known number of columns, but I’d rather have something that does not rely on that.

I’d rather not brute force this (with a pre-read and column length arrays) if I’m missing some elegant use of an existing command.

My shell is BASH. This is for a Cygwin in a Windows environment (no choice).

Mike
# 2  
Old 07-30-2010
Hi,

for a file like:
Code:
Year,Make,Model,Extras
1997,Ford,E350,variables
2000,Mercury,Cougar,examples

This command:
Code:
awk -F, 'NR==FNR{for (i=1;i<=NF;i++)
    {t=length($i)+2;(t>a[i])?a[i]=t:a[i]=a[i]}}
    NR!=FNR{for (i=1;i<=NF;i++)
    {printf "%-"a[i]"s", $i};printf "\n"}' file file

gives you:

Code:
Year  Make     Model   Extras     
1997  Ford     E350    variables  
2000  Mercury  Cougar  examples

HTH Chris
This User Gave Thanks to Christoph Spohr For This Post:
# 3  
Old 07-30-2010
Thank you. Awk syntax looks a lot like C.


I think I understand what you are doing. You are using one program when it is the first run and another when it is the second. I'm trying to modify the script to ignore the length when there is only a single column on the line but I don’t understand the Awk language enough.

Code:
This is a long table header that is not data.
Year,Make,Model,Extras
1997,Ford,E350,variables
2000,Mercury,Cougar,examples



And I want:

Code:
This is a long table header that is not data.
Year  Make     Model   Extras     
1997  Ford     E350    variables  
2000  Mercury  Cougar  examples   



Instead of:

Code:
This is a long table header that is not data.
Year                                           Make     Model   Extras     
1997                                           Ford     E350    variables  
2000                                           Mercury  Cougar  examples   


Mike
# 4  
Old 07-31-2010
Not pretty, but it works. It uses NF to check if the number
of fields is greater than 1.

Code:
awk -F, 'NF > 1 && NR==FNR{for (i=1;i<=NF;i++)
    {t=length($i)+2;(t>a[i])?a[i]=t:a[i]=a[i]}}
    NR!=FNR{if (NF>1) {for (i=1;i<=NF;i++)
    {printf "%-"a[i]"s", $i};printf "\n"}
    else print $0}' file file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

C: inputting string of unknown length

I realize this general issue (inputting strings of variable length in C) has been addressed in myriad locations before, but I'm interested in knowing why my specific approach is not working. (BTW I'm intentionally keeping the size increments small so that I can more easily follow what's going on.... (5 Replies)
Discussion started by: DevuanFan
5 Replies

2. Shell Programming and Scripting

Fixed length to delimited file conversion

Hi All, I need to convert a fixed length file to a delimited file with , (comma). But not all columns, some of the columns in the fixed files are used as fillers and I do not need that in the output file. test_fixed_len.txt I 0515 MR 394 I 0618 MR & MRS 942 I 0618 MR & MRS... (7 Replies)
Discussion started by: member2014
7 Replies

3. Shell Programming and Scripting

Replace a field with a character as per the field length

Hi all, I have a requirement to replace a field with a character as per the length of the field. Suppose i have a file where second field is of 20 character length. I want to replace second field with 20 stars (*). like ******************** As the field is not a fixed one, i want to do the... (2 Replies)
Discussion started by: gani_85
2 Replies

4. Shell Programming and Scripting

Flat file-make field length equal to header length

Hello Everyone, I am stuck with one issue while working on abstract flat file which i have to use as input and load data to table. Input Data- ------ ------------------------ ---- ----------------- WFI001 Xxxxxx Control Work Item A Number of Records ------ ------------------------... (5 Replies)
Discussion started by: sonali.s.more
5 Replies

5. Shell Programming and Scripting

Removing characters from end of line (length unknown)

Hi I have a file which contains wrong XML, There are some garbage characters at the end of line that I want to get rid of. Example: <request type="product" ><attributes><pair><name>q</name><value><!]></value></pair><pair><name>start</name><value>1</value></pair></attributes></request>�J ... (7 Replies)
Discussion started by: dirtyd0ggy
7 Replies

6. Shell Programming and Scripting

Adding a field to a file using a conversion table

Hello everyone, Here is what i am trying to accomplish. I have a transaction log that I want to to add a field. The fields in the transaction log are tab delimited FYI. My goal is to add a column specifying the category/type to each item purchased. I have created a two column "conversion table"... (2 Replies)
Discussion started by: SpencerClark
2 Replies

7. Shell Programming and Scripting

timestamp field was padded with blanks warning

There are two columns on the data base table, create and update timestamps of datatype timestamp. I dont have these fields on the csv file. So I am doing the below. awk -F , -v d="$(date "+ %Y-%m-%d-%H.%M.%S")" '{ OFS=FS; print $1, $2, $3, $4, d, $5, $6, d }' temp.csv > temp1.csv and then... (3 Replies)
Discussion started by: mitr
3 Replies

8. UNIX for Dummies Questions & Answers

Conversion problem with date field and formatting.

Hi, My input file contains the record(s) as below with space as FS. 01-01024180 35000 MV010 02/03/09 0306 03060226 03 02-00410330 470000 MV010 02/03/09 0301 03010276 03 1. I need to convert the field 02/03/09 (dd/mm/yy) to yyyymmdd yet retain the Field separator. Using the modified... (2 Replies)
Discussion started by: talk2pawee
2 Replies

9. UNIX for Dummies Questions & Answers

Extract substring of unknown length from string

I have a string: hgLogOutput=" +0000 files: forum/web/hook-test.txt /forum/web/hook-test-2.txt description: test" and I want to extract the file names from it, they will always appear between the files: and the description:. I have worked out that I can do this: "$hgLogOutput" | awk '{... (2 Replies)
Discussion started by: klogger
2 Replies

10. Shell Programming and Scripting

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... (6 Replies)
Discussion started by: dsravan
6 Replies
Login or Register to Ask a Question