How to add extra spaces to make all lines the same length?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to add extra spaces to make all lines the same length?
# 1  
Old 04-10-2015
How to add extra spaces to make all lines the same length?

Hello to all,

I'm trying to format a file to have all lines with the same length (the length of the longest line) adding needed extra spaces at the end.

Currently I have the awk script below that adds one space the end of each that have a lenght lower than 35, but I don't know
how to add dynamically "MaxLenght - each line lenght" spaces.

This is the code I've tried so far:
Code:
awk 'length<35{print $0 " ";next}1' file

For example:
first line has a lenght of 25 --> Then add 10 spaces at the end
first line has a lenght of 10 --> Then add 25 spaces at the end
first line has a lenght of 35 --> Then add 0 spaces at the end
first line has a lenght of 32 --> Then add 3 spaces at the end

Thanks for any help.

Best regards
# 2  
Old 04-10-2015
Code:
echo '12345' | awk -v m=10 '{printf("[%-10s]\n", $0)}'
[12345     ]

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 04-10-2015
Try, untested

Code:
awk -v x=0 '(NR == FNR) { if(length > x) x = length;next } { printf("%-"x"s\n", $0) }' file file

This User Gave Thanks to senhia83 For This Post:
# 4  
Old 04-10-2015
Hello vgersh99, it works and it seems to work without the "-v m=10" part at the beginning.

hello senhia83, Excellent! it works taken dynamically the length of the longest line!

Thank you both.

Best regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep string causes extra spaces

Hello, I have an xml file and my aim is to grab each line in keywords file and search the string in another file. When keyword is found in xml file,I expect the script to go to previous line in the xml file and grab the string/value between two strings. It's almost working with an error. tab... (6 Replies)
Discussion started by: baris35
6 Replies

2. Shell Programming and Scripting

Append spaces the rows to make it into a required fixed length file

I want to make a script to read row by row and find its length. If the length is less than my required length then i hav to append spaces to that paritucular row. Each row contains special characters, spaces, etc. For example my file contains , 12345 abcdef 234 abcde 89012 abcdefgh ... (10 Replies)
Discussion started by: Amrutha24
10 Replies

3. Shell Programming and Scripting

Flat file-make field length equal to header length

Hello Everyone, I am stuck with one issue while working on abstract flat file which i have to use as input and load data to table. Input Data- ------ ------------------------ ---- ----------------- WFI001 Xxxxxx Control Work Item A Number of Records ------ ------------------------... (5 Replies)
Discussion started by: sonali.s.more
5 Replies

4. Shell Programming and Scripting

Remove of extra spaces from the trailing

HI, I need the help from the experts like I have created one file with text like: a b c d e f g h i j k l So my question is that i have to write the script in which like in the first sentence it will take only one space after d and remove all the extra space in the end.I dont... (8 Replies)
Discussion started by: bhanudhingra
8 Replies

5. Shell Programming and Scripting

How to add trailing spaces to have file with lines of the same length?

I have textfile (source.txt) with different length of lines in it. Can anybody help to compose a script under bash which would add suitable number of trailing spaces to the end of each line so that after the processing the each line would have the same (let's say 100 char) length? Output can be... (6 Replies)
Discussion started by: sameucho
6 Replies

6. Shell Programming and Scripting

Make variable length record a fixed length

Very, very new to unix scripting and have a unique situation. I have a file of records that contain 3 records types: (H)eader Records (D)etail Records (T)railer Records The Detail records are 82 bytes in length which is perfect. The Header and Trailer records sometimes are 82 bytes in... (3 Replies)
Discussion started by: jclanc8
3 Replies

7. Shell Programming and Scripting

How to remove extra spaces from a string??

Hi, I have a string like this and i want to remove extra spaces that exists between the words. Here is the sentence. $string="The small DNA genome of hepadnaviruses is replicated by reverse transcription via an RNA intermediate. This RNA "pregenome" contains ... (2 Replies)
Discussion started by: vanitham
2 Replies

8. Shell Programming and Scripting

remove extra spaces between fields

Hi, I have a source file as mentioned below: I want to remove all the extra spaces between the fields. a b--------|sa df-------|3232---|3 sf sa------|afs sdf-----|43-----|33 a b c------|adfsa dsf---|23-32|23 *Here '-' idicates spaces Now, I want output as below: a b|sa df|3232|3... (7 Replies)
Discussion started by: srilaxmi
7 Replies

9. UNIX for Dummies Questions & Answers

how to append spaces(say 10 spaces) at the end of each line based on the length of th

Hi, I have a problem where I need to append few spaces(say 10 spaces) for each line in a file whose length is say(100 chars) and others leave as it is. I tried to find the length of each line and then if the length is say 100 chars then tried to write those lines into another file and use a sed... (17 Replies)
Discussion started by: prathima
17 Replies

10. Shell Programming and Scripting

Remove extra spaces in a line

Hi, I need a help in deleting extra spaces in a text. I have a huge file, a part of it is :- 3 09/21/08 03:32:07 started undef mino Oracle nmx004.wwdc.numonyx.com Message Text : The Oracle session with the PID 1103 has a CPU time ... (6 Replies)
Discussion started by: vikas027
6 Replies
Login or Register to Ask a Question