Aligning output with null fields in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Aligning output with null fields in shell script
# 1  
Old 09-10-2012
Aligning output with null fields in shell script

Hello Gurus !

I have what probably amounts to a few simply changes to fix; however for the life of me I cannot seem to get it ti work. I need to align the output of my script (I am writing to a logfile)... here's the lines in my code:

Code:
    if [ $header = "YES" ]
    then
        echo "NODE: $node" >> $logfile

        echo " " >> $logfile
        echo "Rule   Permission                                                                            Search" >> $logfile
        echo " ID       value    Owner     Group     Location                         File                  type" >> $logfile
        echo "------------------------------------------------------------------------------------------------------------------------------------" >> $logfile
        header="NO"
    fi
    printf "%-10s%-7s%-8s%-8s%-10s%-34s%-10s\n" "$id" "$permval" "$owner" "$group" "$loc" "$file" "$stype">> $logfile

My output looks like this:
Code:
NODE: bat
 
Rule   Permission                                                                            Search
 ID       value    Owner     Group     Location                         File                  type 
-----------------------------------------------------------------------------------------------------
1         755                    /                                                     ALL
3         755    testownertestgroup/         bat00043.bat                           FILE
10        755                    /uscripts                                            ALL

When it should look like this:
Code:
NODE: bat
 
Rule   Permission                                                                            Search
 ID       value    Owner     Group     Location                         File                  type 
-----------------------------------------------------------------------------------------------------
1         755                          /                                                      ALL
3         755      testowner testgroup /                                bat00043.bat          FILE
10        755                          /uscripts                                              ALL

Some things to note are that $owner, $group and $file may be NULL all other fields will have values. I think this is what is causing the issue. Is there wa way to tell unix (via printf, awk, sed or combination) to print a variable at a specific column regardless of it being NULL and it will not disturb other fields?

Thanks in advance to all that help out ... regards
# 2  
Old 09-10-2012
Your data is larger than the field widths you have in the printf - testowner is 9, for instance, and you have %-8s.
# 3  
Old 09-10-2012
This is "spacing" .. you have to take in consideration the previous field; do you not? But regardless; I tried it with longer lengths and it still doe not align.

Thanks
# 4  
Old 09-10-2012
Quote:
Originally Posted by gvolpini
This is "spacing" .. you have to take in consideration the previous field; do you not?
Not when the previous field isn't the problem. 8 characters is not spacing for a 9 character string - particularly when that column header is more like 11 characters (depending exactly how you want them to line up).

Code:
$ cat x.sh
#!/bin/bash

id=( "1" "3" "10" )
permval=( "755" "755" "755" )
owner[1]="testowner"
group[1]="testgroup"
loc=( "/" "/" "/uscripts" )

echo "Rule   Permission"
echo " ID       value    Owner     Group     Location"
echo "---------1---------2---------3---------4--------"
echo "1234567890123456789012345678901234567890--------"

for index in  0 1 2
do
   printf "%-10s%-7s%-11s%-11s%-10s\n" "${id[index]}" "${permval[index]}" "${own
er[index]}" "${group[index]}" "${loc[index]}"
done

$ ./x.sh
Rule   Permission
 ID       value    Owner     Group     Location
---------1---------2---------3---------4--------
1234567890123456789012345678901234567890--------
1         755                          /
3         755    testowner  testgroup  /
10        755                          /uscripts

# 5  
Old 09-10-2012
Try this format:

Code:
printf "%-10s%-9s%-10s%-10s%-33s%-22s%-7s\n" "$id" "$permval" "$owner" "$group" "$loc" "$file" "$stype" >> $logfile

# 6  
Old 09-10-2012
As mentioned above, the field width of some of the conversion specifiers in your printf format string aren't wide enough to accomdate the data. You have a decision to make: Do you want to truncate the values or not?

If you want to truncate, add precision to the specifier. %-8s creates an 8 character-wide field, but if the value is wider it will still be printed in its entirety, shifting following fields as necessary. Adding precision prevents this with truncation: %-8.8s. Note that the precision does not have to equal the field width. For example, %-8.5s represents a left-justified, 8 character-wide field whose string value will never exceed 5 characters.

If you do not want to truncate, then you have two choices. You can increase field widths until they are no longer likely to be a problem; or you can read all of the data first, determine the width of the widest value in each field, and then build the printf format string accordingly.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 7  
Old 09-10-2012
Thanks to everyone for looking at my problem; I think now I know what I will need to do.

Regards
G
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Fields shifting in file, do to null values?

The below code runs and creates an output file with three sections. The first 2 sections are ok, but the third section doesn't seem to put a . in all the fields that are blank. I don't know if this is what causes the last two fields in the current output to shift to a newline, but I can not seem... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Shell script to pull certain fields

I/m a beginner so be easy. I have text files that live on an AIX server. The files come in and I've been charged with writing a shell script to email out that pulls the first date, and the last date of the file. I need to load these 2 dates into 2 separate variables. I can figure out the variables,... (13 Replies)
Discussion started by: mattadams1983
13 Replies

3. Shell Programming and Scripting

Shell Script to Group by Based on Multiple Fields in a file

Hi, I want to know if there is any simple approach to SUM a field based on group by of different fields for e.g. file1.txt contains below data 20160622|XXX1||50.00||50.00|MONEY|Plan1| 20160622|XXX1||100.00||100.00|MONEY|Plan1| 20160623|XXX1||25.00||25.00|MONEY|Plan1|... (3 Replies)
Discussion started by: cnu_theprince
3 Replies

4. Shell Programming and Scripting

How to get fields and get output with awk or shell script.?

I have a flat file A.txt with field seperate by a pipe 2012/11/13 20:06:11 | 284:hawk pid=014268 opened Locations 12, 13, 14, 15 for /home/hawk_t112/t112/macteam/qt/NET12/full_ddr3_2X_FV_4BD_1.qt/dbFiles/t112.proto|2012/11/14 15:19:26 | still running |norway|norway 2012/11/14 12:53:51 | ... (6 Replies)
Discussion started by: sabercats
6 Replies

5. Shell Programming and Scripting

pass null value to sql script from korn shell script

There are 4 parameters that I have to pass from korn shell to sql script. 1) I have to check if $1 , $2 , $3 and $4 are null values or not . How can I do that ? 2) Once its determined that these values are null (in the sense they are empty) how can I pass null values to sql script... (11 Replies)
Discussion started by: megha2525
11 Replies

6. Shell Programming and Scripting

Shell script for validating fields in a file

Hi, I have not used Unix in a very long time and I am very rusty. I would appreciate any help I can get from the more experienced and experts in Shell script. I am reading one file at a time from a folder. The file is a flat file with no delimeters or carriage return. Col1 through col6 is... (5 Replies)
Discussion started by: asemota
5 Replies

7. Shell Programming and Scripting

Find out if few fields in a file are null

Hi, I've a pipe delimited file where I want to find out a number of lines where 1st 2nd and last field are null using awk/sed. Is it possible? Thanks (5 Replies)
Discussion started by: rudoraj
5 Replies

8. Shell Programming and Scripting

exclude records with null fields

Hi, can I do something like this to add a condition of checking if the 4th field is number or space or blank also: awk -F, '$4 /^*||*/' MYFILE >> OTHERFILE I also want the other part i.e. I need to exclude all lines whose 4th field is space or blank or number: MYFILE a,b,c,d,e a,b,c,2,r... (2 Replies)
Discussion started by: praveenK_Dudala
2 Replies

9. Shell Programming and Scripting

Replace 3 fields with null in the file

Hi, I have a file with 104 columns delimited by comma. I have to replace fields 4,5 and 19 with null values and after replacing the columns in the file , the file should be still comma delimited. I am new to shell scripting, Experts please help me out. Thank you (1 Reply)
Discussion started by: vukkusila
1 Replies

10. Shell Programming and Scripting

Find null fields in file

Hi All, I have some csv files out of which i want to find records which have empty values in either the 14th or 16th fields. The following is a sample. $cut -d',' -f14,16 SPS* | head -5 VOIP_ORIG_INFO,VOIP_DEST_INFO sip:445600709315@sip.com,sip:999@sip.com... (2 Replies)
Discussion started by: rahulrathod
2 Replies
Login or Register to Ask a Question