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:
The output unfortunately ends up looking something like this:
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.
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):
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:
Regards,
Alister
Last edited by alister; 03-24-2010 at 06:59 PM..
Reason: switch to left-justified fields
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)
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)
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)
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)
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)
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)
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)
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)
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)