awk to print fixed length columns to right side


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to print fixed length columns to right side
# 1  
Old 07-20-2012
Question 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:
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:
Code:
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"

Code:
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

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by radoulov; 07-20-2012 at 04:16 PM.. Reason: font is incorrect
# 2  
Old 07-20-2012
Don't exactly understand what you want, but here is one suggestion
Code:
 ...  | 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:
Code:
 ...  | 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

Last edited by 244an; 07-20-2012 at 04:02 PM..
# 3  
Old 07-20-2012
Quote:
Originally Posted by 244an
Don't exactly understand what you want, but here is one suggestion
Code:
 ...  | awk -v var="Hello" '{printf "%- 8s %- 40s %+ 15s\n",$1,$2,var}'

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).
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.
# 4  
Old 07-20-2012
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 Smilie
And perhaps edit in you post were you quoted me, so not others will be confused...
# 5  
Old 07-20-2012
Quote:
Originally Posted by 244an
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 Smilie
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.
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)}

'

Last edited by Franklin52; 07-20-2012 at 04:50 PM.. Reason: Please use code tags for data and code samples, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting fixed length file using awk

Hi, I need to split a fixed length file of 160 characters based on value of a column. Example: ABC 456780001 DGDG SDFSF BCD 444440002 SSSS TTTTT ABC 777750003 HHHH UUUUU THH 888880001 FFFF LLLLLL HHH 999990002 GGGG OOOOO I need to split this file on basis of column from... (7 Replies)
Discussion started by: Neelkanth
7 Replies

2. UNIX for Dummies Questions & Answers

Fixed length file extracting values in columns

How do I extract values in a few columns in a row of a fixed length file? If there are 8 columns and I need to extract values of 2nd,4th and 6 th columns, how do i do that? I used cut command, this I used only for one column. How do I do it more than one column? The below command will give... (1 Reply)
Discussion started by: princetd001
1 Replies

3. Shell Programming and Scripting

Unix sort for fixed length columns and records

I was trying to use the AIX 6.1 sort command to sort fixed-length data records, sorting by specific columns only. It took some time to figure out how to get it to work, so I wanted to share the solution. The sort man page wasn't much help, because it talks about field delimeters (default space... (1 Reply)
Discussion started by: CheeseHead1
1 Replies

4. Shell Programming and Scripting

Script to place selected columns from a group of files side by side in a new file

Hi Everyone, I need a shell/perl script to bring selected columns from all the files located in a directory and place them in a new file side by side. File1: a b c d 2 3 4 5 f g h i .......... File2: I II III IV w x y z .............. and so on many files are there...... (8 Replies)
Discussion started by: ks_reddy
8 Replies

5. Shell Programming and Scripting

How to print string on screen according the fixed length?

Problem: entry_name="joke:hello:yellow:blue:default" print("%d %-12s\t%-10s\t%-5s\n", $i, $entry_name....); I just want to print the output like this index entry value .... 1 joke:hello:y 0 123 567 ellow:blue:d ... (1 Reply)
Discussion started by: a2156z
1 Replies

6. Shell Programming and Scripting

print a file with one column having fixed character length

Hi guys, I have tried to find a solution for this problem but couln't. If anyone of you have an Idea do help me. INPUT_FILE with three columns shown to be separated by - sign A5BNK723NVI - 1 - 294 A7QZM0VIT - 251 - 537 A7NU3411V - 245 - 527 I want an output file in which First column... (2 Replies)
Discussion started by: smriti_shridhar
2 Replies

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

8. Shell Programming and Scripting

Awk - Working with fixed length files

OK I am somewhat new to UNIX programming please see what you can do to help. I have a flat file that is a fixed length file containing different records based on the 1st character of each line. The 1st number at the beginning of the line is the record number, in this case it's record #1. I... (3 Replies)
Discussion started by: ambroze
3 Replies

9. Shell Programming and Scripting

Awk with fixed length files

Hi Unix Champs, I want to awk on a fixed length file. Instead if the file was a delimited file, then I could have used -F and then could have easily done manipulation on the fields. How do i do the same in case of fixed length file? Thanks in Advance. Regards. (7 Replies)
Discussion started by: c2b2
7 Replies

10. Shell Programming and Scripting

fixed length fields in awk

I am trying to display df -h command out in proper format, how can I display each field of each record in a fixed length. (2 Replies)
Discussion started by: roopla
2 Replies
Login or Register to Ask a Question