Printing Fixed Width Columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing Fixed Width Columns
# 1  
Old 03-24-2010
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 policy to build the proper output later.

Unfortunately, not all the data that I extract from the text file comes out to the same length so often times my columns are all over the place.

I've been searching left and right, but cannot find of a good simple way to do this. I am beginning to think I will have to create some sort of function to find the length of each individual column before printing the data, but I am not sure if this is possible.

I am currently using the following code:
Code:
echo -e "From: \t"$frm_Zone"\tTo:\t"$to_Zone
echo -e $pol_Id"\t"$src_Address"\t\t"$dst_Address"\t\t"$pol_Service"\t"$pol_Action"\t"$pol_Name

#finding the array with the largest index
#so we now how many times to run the for loop

lnAddy=`echo ${#srcAddyArray[*]}`
lnSrc=`echo ${#dstAddyArray[*]}`
lnSvc=`echo ${#serviceArray[*]}`

max2 $lnAddy $lnSrc
temp=$?
max2 $temp $lnSvc
longest=$?

for (( i = 0; i < longest; i++ )) do

echo -e "\t"${srcAddyArray[$i]}"\t"${dstAddyArray[$i]}"\t"${serviceArray[$i]}
done

The output unfortunately ends up looking something like this:
Code:
From:   Trust-cs        To:     Untrust-Firn
615     Any             mail.acast.foo.com             tcp-6777 (wo 85073)     Permit Nat SRC  wo 85073
        69.xxx.xxx.1    mail.gort.org tcp-6788 (wo 85073)
        75.123.44.1     my.mctabbs.net   tcp-8008 (wo 85073)

From:   Trust-cs        To:     DMZ-test
596     Any             69.xxx.xxx.31            TCP-15888       Permit
        tables.noc.wow.org     mages.icecrown.com      tcp-16888
                        TCP-17888
                        UDP-17888
                        UDP-18888

Is it possible to get the output to be spaced appropriately so it doesn't get clobbered if some lines are longer than others?

Any help would be greatly appreciated! If you need to see the rest of the script or some sample data, please let me know and I'll be happy to assist.

Thanks in advance!
# 2  
Old 03-24-2010
Hi, cixelsyd:

Welcome to the forums. I believe what you want to check out is printf. It is usually a shell built-in, but is probably also available as a standalone executable if not builtin. printf is also available as a function within AWK. You can use it to print out fixed width columns regardless of the string argument's length.

An example showing how to print out two 10 character wide, left-justified columns (separated by a space):
Code:
$ one=1 four=4444 six=666666 nine=999999999
$ echo $one $four; echo $six $nine
1 4444
666666 999999999
$ printf '%-10s %-10s\n' $one $four; printf '%-10s %-10s\n' $six $nine
1          4444      
666666     999999999

Note that if the string is wider than the column's width (in this case 10), the column will expand to accomodate the data and break the formatting. So, you'll need to choose a width that should seldom if ever be exceeded, or truncate the string to 10 prior to passing it to printf.

In bash, you should definitely have printf builtin. Perhaps something similar to the following will work for you:
Code:
printf '\t%-25s %-25s %-25s\n' "${srcAddyArray[$i]}" "${dstAddyArray[$i]}" "${serviceArray[$i]}"

Regards,
Alister

Last edited by alister; 03-24-2010 at 06:59 PM.. Reason: switch to left-justified fields
# 3  
Old 03-25-2010
Alister,

Thank you so much for your help! That did exactly the trick. I modified the script to the following:

Code:
echo -e "From: \t"$frm_Zone"\tTo:\t"$to_Zone
                        
printf '%-5s %-20s %-20s %-20s %-15s %-10s\n' $pol_Id $src_Address $dst_Address "$pol_Service" "$pol_Action" "$pol_Name"

#finding the array with the largest index
#so we now how many times to run the for loop

lnAddy=`echo ${#srcAddyArray[*]}`
lnSrc=`echo ${#dstAddyArray[*]}`
lnSvc=`echo ${#serviceArray[*]}`

max2 $lnAddy $lnSrc
temp=$?
max2 $temp $lnSvc
longest=$?

for (( i = 0; i < longest; i++ )) do

              printf '%-5s %-20s %-20s %-20s\n' "" "${srcAddyArray[$i]}" "${dstAddyArray[$i]}" "${serviceArray[$i]}"

done

I ended up having to print one blank column so it lined up properly, but now the output is exactly how I wanted:
Code:
From:   Trust-cs        To:     Untrust-Firn
615   Any                  mail.acast.xxxx.xxx  tcp-6777 (wo 85073)  Permit Nat SRC  wo 85073  
      69.xx.xxx.1          mail.xxxxxxx.edu     tcp-6788 (wo 85073) 
      75.xxx.44.1          my.xxxxxx.edu        tcp-8008 (wo 85073) 

From:   Trust-cs        To:     DMZ-acad
596   Any                  69.xx.xxx.31         TCP-15888            Permit                    
      tables.noc.xxxx.xxx  mages.icecrown.com   tcp-16888           
                                                TCP-17888           
                                                UDP-17888           
                                                UDP-18888

Thank you so much for your help!
# 4  
Old 03-25-2010
You're very welcome.

Cheers,
Alister
# 5  
Old 03-25-2010
This makes sense.

Code:
$ diff newfile oldfile
2,3c2
<
< printf '%-5s %-20s %-20s %-20s %-15s %-10s\n' $pol_Id $src_Address $dst_Address "$pol_Service" "$pol_Action" "$pol_Name"
---
> echo -e $pol_Id"\t"$src_Address"\t\t"$dst_Address"\t\t"$pol_Service"\t"$pol_Action"\t"$pol_Name
19,20c18
<               printf '%-5s %-20s %-20s %-20s\n' "" "${srcAddyArray[$i]}" "${dstAddyArray[$i]}" "${serviceArray[$i]}"
<
---
> echo -e "\t"${srcAddyArray[$i]}"\t"${dstAddyArray[$i]}"\t"${serviceArray[$i]}

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

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. UNIX for Dummies Questions & Answers

Length of a fixed width file

I have a fixed width file of length 53. when is try to get the lengh of the record of that file i get 2 different answers. awk '{print length;exit}' <File_name> The above code gives me length 50. wc -L <File_name> The above code gives me length 53. Please clarify on... (2 Replies)
Discussion started by: Amrutha24
2 Replies

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

5. Shell Programming and Scripting

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

I am trying to selectively display several columns from a db2 query, which gives me a fixed-width output (partial output listed here): --------- -------------------------- ------------ ------ 000 0000000000198012 702 29 000 0000000000198013 ... (9 Replies)
Discussion started by: ahsh79
9 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

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

8. Shell Programming and Scripting

Fixed-Width file from Oracle

Hi All, I have created a script which generates FIXED-WIDTH file by executing Oracle query. SELECT RPAD(NVL(col1,CHR(9)),20)||NVL(col2,CHR(9))||NVL(col3,CHR(9) FROM XYZ It generates the data file with proper alignment. But if same file i transfer to windows server or Mainframe... (5 Replies)
Discussion started by: Amit.Sagpariya
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. UNIX Desktop Questions & Answers

Help with Fixed width File Parsing

I am trying to parse a Fixed width file with data as below. I am trying to assign column values from each record to variables. When I parse the data, the spaces in all coumns are dropped. I would like to retain the spaces as part of the dat stored in the variables. Any help is appreciated. I... (4 Replies)
Discussion started by: sate911
4 Replies
Login or Register to Ask a Question