awk to print fixed length columns to right side

Hi,
I am in a situation to print the message on a column, where the each line starting position should be same.

For example code:

HOSTNAME1="1.2.3.4.5.6.7"
TARGET_DIR="/tmp"
echo "HOSTNAME1:"   "$HOSTNAME1" | awk -v var="Everyone" '{len=55-length;printf("%s%*s\n",$0,len,var)}'
echo "TARGET_DIR:"  "$TARGET_DIR" | awk -v var="Hello" '{len=55-length;printf("%s%*s\n",$0,len,var)}'

Output:

HOSTNAME1: 1.2.3.4.5.6.7                                         Everyone
TARGET_DIR: /tmp                                                          Hello
TARGET_DIR1: /tmp/tmp1/tmp2                  Thank you Unix Forum

The problem, it's fixing the end position and starts writing towards left, but i need output like this, since i don't know the length of the "TARGET_DIR"

HOSTNAME1: 1.2.3.4.5.6.7                       Everyone
TARGET_DIR: /tmp                                  Hello
TARGET_DIR1: /tmp/tmp1/tmp2                 Thank you Unix Forum

The font is not appearing what i expect here, anyway but i need "Everyone" "Hello" "Thank you unix Forum" should start at same position.

Thanks in Advance

Don't exactly understand what you want, but here is one suggestion

 ...  | awk -v var="Hello" '{printf "%- 12s %- 40s %+ 15s\n",$1,$2,var}'

$ echo "HOSTNAME1:" "/home/of/the/brave" | awk -v var="Everyone" '{printf "%- 12s %- 40s %+ 15s\n",$1,$2,var}'
HOSTNAME1:   /home/of/the/brave                              Everyone

$ echo "TARGET_DIR:" "/home/again" | awk -v var="Hello" '{printf "%- 12s %- 40s %+ 15s\n",$1,$2,var}'
TARGET_DIR:  /home/again                                        Hello

Then you can adjust the lengths (the path (second field) will adjust the last field if the path is longer then 40 characters with above example).

And if you want path to get truncated at 40:

 ...  | awk -v var="Hello" '{printf "%- 12s %- 40.40s %+ 15s\n",$1,$2,var}'

I saw you edited your post, if you want the last text to expand to the right, then you can just have "%s" as the last format

Thanks for your reply....
I don't know the length of the second field, all data is coming dynamically, is there a way i can print 3 field starting from 50th location towards right, doesn't matter what is the length of 1'st and 2'nd fields.

Read my edited post above, there I have that as a last suggestion (only "%s" as last format)
AND I also changed the format strings in that post - you were to quick :slight_smile:
And perhaps edit in you post were you quoted me, so not others will be confused...

Excellent!!!
It's working , thanks alot UNIX Forums.

Here is the modified code.

echo "HOSTNAME1:"   "$HOSTNAME1" | awk -v var="Everyone" '{printf("%- 12s %- 40.40s %s\n",$1,$2,var)}'
echo "TARGET_DIR:"  "$TARGET_DIR" | awk -v var="Hello" '{printf("%- 12s %- 40.40s %s\n",$1,$2,var)}'
echo "TARGET_DIR:"  "$TARGET_DIR1" | awk -v var="Thank you Unix Forum" '{printf("%- 12s %- 40.40s %s\n",$1,$2,var)}

'