Using printf (or other?) to create variable fixed width text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using printf (or other?) to create variable fixed width text
# 1  
Old 07-24-2013
Using printf (or other?) to create variable fixed width text

I would like to use printf (or something else?) to create a line of text that has varying column widths. This will be used to create a fixed width file (with varying column widths). For example, consider variables $1 $2 $3 are equal to a, b, c respectively and they should be printed in column positions 1, 5, 15 respectively so the output would be:
a___b_________c
(ignore the underlines)

I was thinking I could use awk for this but it isn't clear to me how I could use awk without a file input (I just want to be able to pass text or variables).

Any ideas would be greatly appreciated... maybe there's a simple perl command, a combination of multiple print commands, or something else I should be using?

Last edited by farrenthorpe; 07-24-2013 at 05:01 PM.. Reason: spaces were removed from the post
# 2  
Old 07-24-2013
Try regular shell:
Code:
printf "%-4s%-10s%s\n" "$1" "$2" "$3"

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 07-24-2013
I just figured it out just as you posted. Thanks!!!
# 4  
Old 07-24-2013
Less negative "%s%4s%10s\n" ?
# 5  
Old 07-24-2013
Sorry, but that (post #2) won't yield a FIXED width file - which was part of the request - should the contents of $3 vary. You need to specify the third column output width as well, then.

EDIT: same applies to DGPickett's proposal, should $1 vary in length.

Last edited by RudiC; 07-24-2013 at 06:14 PM.. Reason: had to quote post #2
# 6  
Old 07-24-2013
I guess it depends on whether he wants it to spill over right or left.
Code:
$ printf '%s%4s%10s\n' 1 2 3
1   2         3
$ printf '%s%4s%10s\n' 1 22 33
1  22        33
$

'%1.1s%4.4s%10.10s\n' truncates, freezes last column to pos ?
'%-4.4s%-10.10s%s\n' truncates, freezes first column to pos ?

Last edited by DGPickett; 07-24-2013 at 06:21 PM..
# 7  
Old 07-24-2013
@RudiC: OP will figure it out, it was just an example. The post was more about using shell's printf instead of an external utility. Besides what if a value is wider than the intended width?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Joining fixed width files

Hi All, I need to join fixed width files on a column which is position 1 to 3 and need to have all the records from file1 file1.txt Cu1nullL1L2 Cu2nullL1L2 Cu3nullL1L2 file2.txt Cu1B1B2 Cu3B1B2 output.txt Cu1L1B1L2B2 Cu2L1L2 Cu3L1B1L2B3 I tried but not getting the expected... (12 Replies)
Discussion started by: shash
12 Replies

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

3. UNIX for Dummies Questions & Answers

convert # delimited text file to fixed width

Hello gurus, I have a file containing 5 columns delimited by '#' as shown in the example below: HRP1000-PLVAR#HRP1000-OTYPE#HRP1000-OBJID#HRP1000-BEGDA#HRP1000-ENDDA# 99991231#AU7129#000000000#1 PROCTER & GAMBLE# 99991231#TT4283#1000013883#21111 LAUNDRY# 99991231#TT4283#1000013884#21121 DISH... (3 Replies)
Discussion started by: chumsky
3 Replies

4. Shell Programming and Scripting

Comparing two fixed width file

Hi Guys I am checking the treads to get the answer but i am not able to get the answer for my question. I have two files. First file is a pattern file and the second file is the file i want to search in it. Output will be the lines from file2. File1: P2797f12af 44751228... (10 Replies)
Discussion started by: anshul_er
10 Replies

5. Shell Programming and Scripting

Replacing variable Text between fixed strings

Hello all, This is my first post and I hope you can help me out. I searched for quite some hours now and haven't found a simple solution to my problem. It is as following: I got this file: dl.dropbox.com/u/14586156/stuff/Bookmarks.plist and want to replace the Text between... (9 Replies)
Discussion started by: pasc
9 Replies

6. Shell Programming and Scripting

How to split a fixed width text file into several ones based on a column value?

Hi, I have a fixed width text file without any header row. One of the columns contains a date in YYYYMMDD format. If the original file contains 3 dates, I want my shell script to split the file into 3 small files with data for each date. I am a newbie and need help doing this. (14 Replies)
Discussion started by: bhanja_trinanja
14 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

Comparing column of variable length anf fixed width file

Hi, I have two input files. File1: ID Name Place 1-234~name1~Newyork 1-34~name2~Boston 1-2345~name3~Hungary File1 is a variable length file where each column is seperated by delimitter "~". File2: ID Country 1-34<<11 SPACES>>USA<<7 spaces>> 1-234<<10 SPACES>>UK<<8... (5 Replies)
Discussion started by: manneni prakash
5 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. Programming

fixed-width printf() output on an XmList on Solaris

This one is a bit of bugger and I'm not sure if it's entirely relevant to this board but here goes... I'm trying to produce some fixed width output to a List widget (X11/Motif GUI) So, I use something similar to: sprintf(Buffer, "%*d%*f", 20, some_integer, 20, some_float); This gives me a... (0 Replies)
Discussion started by: trido
0 Replies
Login or Register to Ask a Question