Format output into columns, variables with multiple entries


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Format output into columns, variables with multiple entries
# 1  
Old 05-10-2013
<SOLVED>Format output into columns, variables with multiple entries

Hello all, I've got a script that collects data on file systems and prints out specific data about each. I've formatted headers w/ printf like so.
Code:
printf "\033[4mVolume Name\tDisk\t\tBlock Device\t\t\t\tMount Point\t\tSize\tOwnership\033[0m\n"

and I had the content of the varibles printed out beneath those columns like so:

Code:
printf "${volume}\t${diskvol}\t${blockdevice}\t\t${mntpnt}\t\t${fssize}\t${owner}\n"

The problem I've run into is that should one of those variables contain multiple entries if ruins the formatting. I've experimented with printf and to attempt to overcome it but I'm not successful thus far.

The code I have at the moment is below.
Code:
printf "%-5s%-10s%-15s%-10s%-5s" ${volume} ${diskvol} ${blockdevice} ${mntpnt} ${fssize} ${owner}

actual output of the script attached.

Essentially the headers are fine. Its the data that follows that has no spacing and isnt respecting the "spaces" i defined.

Help appreciated
Format output into columns, variables with multiple entries-bashoutputpng

Last edited by awreneau; 05-16-2013 at 09:43 AM.. Reason: tagging as solved
# 2  
Old 05-10-2013
Add a "\n" to the end of the printf statement, and use double quotes around each variable.
# 3  
Old 05-13-2013
Is this what youre expecting? I have something very similar in my first post but I edited it per your specs.

Code:
printf "${volume}\t${diskvol}\t${blockdevice}\t\t${mntpnt}\t\t${fssize}\t${owner}\n"

to this?
Code:
printf "${volume}"\t "${diskvol}"\t"${blockdevice}"\t\t"${mntpnt}"\t\t"${fssize}"\t"${owner}""\n"

If I have my code matching what you expected to see then it clobbers the formatting again. See attachment.
Format output into columns, variables with multiple entries-codechangeresultpng
# 4  
Old 05-13-2013
Expanding shell variables directly within a printf format string is a bad idea; it sh it can lead to unpredictable behavior (in C, it's a serious security hole) if something in those variables resembles a format specifier (%s, %d, %%, etc).

No offense, but I cannot be bothered to analyze your output and reverse engineer the values of your variables.

You should show us the code you are using in its entirety or at the very least print out the value of each variable for each line of output. Then the actual output for those values followed by the desired output.

Regardless of any of the above, your format will be wrecked if any string exceeds its alloted width unless the format specifier truncates it by specifying a precision. In case it's illuminating compare this ...
Code:
$ printf '%4s %4s\n' col1 col2 ---- ---- 1234567890 1234567890
col1 col2
---- ----
1234567890 1234567890

against this ...
Code:
$ printf '%4.4s %4.4s\n' col1 col2 ---- ---- 1234567890 1234567890
col1 col2
---- ----
1234 1234

If your output format must accommodate fields of unpredictable widths, and if you are unwilling to truncate them, then you must resort to inspecting all of the output, storing it in the meantime, and then dynamically generating a format string which can accommodate the widest value in each column.

Regards,
Alister

Last edited by alister; 05-13-2013 at 02:28 PM..
# 5  
Old 05-13-2013
OK, lets make it simpler. I've changed the code somewhat to amplify the specific problem.

I have two columns of data:
Think of the periods as actual spaces (the forum didnt allow a space to format the desired result)

volume1 disk1
............disk2
............disk3
volume2 disk4
............disk5
............disk6
volume3 disk3
............disk2
volume4 disk4
............disk8

Thats how I want them displayed. In reality they are displayed as such:

volume1 disk1
disk2
disk3
volume2 disk4
disk5
disk6
volume3 disk3
disk2
volume4 disk4
disk8


the code I'm using to format the output is below:
Code:
 
header="\n%-18s %-15s\n"
format="\n%-18s %-15s"
printf "${header}" "Volume" "Disk"
printf "${format}" "${volume}" "${diskvol}"

the data in ${volume} and ${diskvol} is being collected elsewhere and for this thread I'm only interested in how to make the 2nd, 3rd, 4th and so on values in the variable ${diskvol} respect the same formatting as the 1st value in said variable.

The actual output of the script is attached. it works as expected with the exception of the formatting. I'm stuck at how to force the printing of the 2nd and following values into the required column.

Help is much appreciated.
Format output into columns, variables with multiple entries-script_outputpng
# 6  
Old 05-16-2013
I never got the exact formatting I'd wanted but opted for a slightly different solution. Code follows:

Code:
#set column headers
header="\n\033[4m\033[1m%-18s %-35s %-25s %-8s %-10s\033[0m\033[0m"
format="\n%-18s %-35s %-25s %-8s %-10s"
printf "${header}" "Volume/Disk" "Block Device" "Mount Point" "Size" "Ownership"
for mntpnt in ${mountpoints}
do
 #displays BLOCKDEVICE - FS SIZE - MOUNT POINT - OWNER
 volume=$(/bin/df -hP $mntpnt | /bin/grep -v Filesystem | /bin/awk '{print $1}' | awk -F/ ' {print $6 }')
 diskvol=$(/sbin/vxprint -qtrg ${diskgroup} ${volume} | /bin/grep ^sd | /bin/grep -v "LOG" | /bin/awk '{ print $8 }' | /bin/sort -u |  sed -s 's/\s\+/\n/g' )
 blockdevice=$(/bin/df -hP $mntpnt | /bin/grep -v Filesystem | /bin/awk '{print $1 }')
 mntpnt=$(/bin/df -hP $mntpnt | /bin/grep -v Filesystem | /bin/awk '{print $6 }')
 fssize=$(/bin/df -hP $mntpnt | /bin/grep -v Filesystem | /bin/awk '{print $4 }')
 owner=$(/bin/ls -Ald ${mntpnt} | awk '{ print $3 ":" $4 }')
 #print the results
  printf "${format}" "${volume}" "${blockdevice}" "${mntpnt}" "${fssize}" "${owner}"
  for disk2vol  in ${diskvol};
  do
   printf "${format}" "   ${disk2vol}"
  done
done

Format output into columns, variables with multiple entries-script_outputpng
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Read in Multiple log files and output selected variables and values to cvs file

I have several problems with my problems: I hope you can help me. 1) the If else statement I am getting an error message. My syntax must be incorrect because the entire statement is throwing an error. For example in filew.log if these items don't exist Memsize, SASFoundation and also if... (0 Replies)
Discussion started by: dellanicholson
0 Replies

2. Shell Programming and Scripting

Join two files combining multiple columns and produce mix and match output

I would like to join two files when two columns in each file matches with each other and then produce an output when taking multiple columns. Like I have file A 1234,ABCD,23,JOHN,NJ,USA 2345,ABCD,24,SAM,NY,USA 5678,GHIJ,24,TOM,NY,USA 5678,WXYZ,27,MAT,NJ,USA and file B ... (2 Replies)
Discussion started by: mady135
2 Replies

3. Shell Programming and Scripting

Sum of columns and format the output

Input file: 011100020100 0.00 1 20000 30000 20000 011110000025 0.00 1 000 240000 10000 011100020100 0.00 1 200000 2324000 403500 032200030025 0.00 1 2077500 3077500 250000 032200030025 0.00 1 2565000 25536400 320000 022220000005 0.00 1 10000 300000 300000 022220000005 0.00 1 200050... (7 Replies)
Discussion started by: vinus
7 Replies

4. Shell Programming and Scripting

Reading multiple values from multiple lines and columns and setting them to unique variables.

Hello, I would like to ask for help with csh script. An example of an input in .txt file is below, the number of lines varies from file to file and I have 2 or 3 columns with values. I would like to read all the values (probably one by one) and set them to independent unique variables that... (7 Replies)
Discussion started by: FMMOLA
7 Replies

5. Shell Programming and Scripting

How to format output in columns by appending multi lines one by one?

Hi, I need to display output in below format Customer : Apr 24 16:31 Customer_Name_111121.txt |---Space---|Apr 24 16:32 Customer_Name _111121. txt |---Space---|Apr 24 16:34 Customer_Name_111112. txt |---Space---|Apr 24 16:35 Customer_Name _222223. txt |---Space---|Apr 24 16:37... (8 Replies)
Discussion started by: ketanraut
8 Replies

6. Shell Programming and Scripting

Multiple records need to convert UNIXtime to human readable datatime and all output in one format

Hello Experts, Below is the record i have: sample data attached I want this record of each row to be in single line and there are multiple rowise unixtime mentioned e.g 11996327 , This needs to be converted to Human readdable data and time from multiple rows Can you help me , it will be... (10 Replies)
Discussion started by: manishK
10 Replies

7. Shell Programming and Scripting

Combining columns from multiple files into one single output file

Hi, I have 3 files with one column value as shown File: a.txt ------------ Data_a1 Data_a2 File2: b.txt ------------ Data_b1 Data_b2 Data_b3 Data_b4 File3: c.txt ------------ Data_c1 Data_c2 Data_c3 Data_c4 Data_c5 (6 Replies)
Discussion started by: vfrg
6 Replies

8. Shell Programming and Scripting

awk output to multiple variables

Hi I need to assign the ouput of a awk statement to two variables; below is a example of the txt file i have which I use awk against sample file testval,USA,loc2,testing02 testval1,GB,loc4,testing01 awk statement awk -F , '{print $2,$3}' USA loc2 GB loc4 I need a method where... (6 Replies)
Discussion started by: duckeggs01
6 Replies

9. Shell Programming and Scripting

Format output from command from variables

Hi , I have below command to that outputs from variables.. command: echo $INSTANCE $DATAB $status $TSLastBackup| awk '{printf("%-8s %-8s \t \n",$1,$2,$3,$4)}' | tee $LOGF the ouput is now: INSTANCE DATABSE BACKUP_STATUS BACKUPTIMESTAMP ------- -------- -------- ... (1 Reply)
Discussion started by: db2_usd
1 Replies

10. Shell Programming and Scripting

sql output from multiple rows and columns as variables in a script

This is for an Oracle journal import. I was using a pl/sql package and oracle API's. Oracle added invoker rights to their API's and now my package won't run. I didn't want to use their API's anyway. The only reason i was using pl/sql and the API's (just a package) was to utilize a cursor. How... (2 Replies)
Discussion started by: lmu
2 Replies
Login or Register to Ask a Question