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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to add trailing spaces to have file with lines of the same length?
# 1  
Old 05-21-2010
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 redirected to dest.txt file.
Thank a lot
# 2  
Old 05-21-2010
Code:
awk '{printf("%100s\n", $0) }' oldfile > newfile

This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 05-21-2010
I tried this, but script produce output file which contains leading spaces (it aligns lines from source file to right). My need is to add spaces to end of each line (from source file).

---------- Post updated at 02:07 PM ---------- Previous update was at 02:04 PM ----------

Only a small mistake was there. This accomplish my needs:

Code:
awk '{printf("%-100s\n", $0) }' oldfile > newfile

Thanks a lot!

---------- Post updated at 02:27 PM ---------- Previous update was at 02:07 PM ----------

Can you still help to know how to concatenate these two commands? The first one finds out the length of longest line in the file. The second one adds trailing spaces to each line to have fixed line length equal 100 chars. I want to use the result of first script to specify length of the each line in the output file of second script (not to have it always 100 chars long):



1)
Code:
awk ' { if ( length > x ) { x = length } }END{ print x }' oldfile

2)
Code:
awk '{printf("%-100s\n", $0) }' oldfile > newfile


Last edited by Scott; 05-21-2010 at 09:39 AM.. Reason: Code tags, PLEASE!
# 4  
Old 05-21-2010
Code:
awk -v x=0 '(NR == FNR) { if(length > x) x = length;next } { printf("%-"x"s\n", $0) }' oldfile oldfile > newfile


Last edited by steadyonabix; 05-21-2010 at 12:22 PM.. Reason: Thanks Alister - oops
# 5  
Old 05-21-2010
Simple! - for those who know Smilie
Thank you very much!
# 6  
Old 05-21-2010
Quote:
Originally Posted by sameucho
Can you still help to know how to concatenate these two commands? The first one finds out the length of longest line in the file. The second one adds trailing spaces to each line to have fixed line length equal 100 chars. I want to use the result of first script to specify length of the each line in the output file of second script (not to have it always 100 chars long):



1)
Code:
awk ' { if ( length > x ) { x = length } }END{ print x }' oldfile

2)
Code:
awk '{printf("%-100s\n", $0) }' oldfile > newfile


Quote:
Originally Posted by steadyonabix
Code:
awk '(NR == FNR) {if(length >  x);x = length;next}{printf("%-100s\n", $0) }' oldfile oldfile >  newfile

Quote:
Originally Posted by sameucho
Simple! - for those who know Smilie
Thank you very much!

I don't see how that accomplishes what you sought. The max length of a line is computed but not used for anything. printf is still using a hardcoded 100 character field width. Try the following:
Code:
awk '{l=length; if (l>x) x=l; a[NR]=$0} END {fstr="%-"x"s\n"; for(i=1;i<=NR;i++) printf(fstr, a[i])}' oldfile > newfile

Regards,
Alister
# 7  
Old 05-26-2010
Yes, you are right. My conclusion was prematured (I judged before I even tried it).
Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove leading and trailing spaces from a file

Hi, I am trying to remove leading and trailing spaces from a file using awk but somehow I have not been able to do it. Here is the data that I want to trim. 07/12/2017 15:55:00 |entinfdev |AD ping Time ms | .474| 1.41| .581|green |flat... (9 Replies)
Discussion started by: svajhala
9 Replies

2. UNIX for Beginners Questions & Answers

Trim trailing spaces from file

I have a file like this. hari,corporationbank,2234356,syndicate ravi,indian bank,4567900000000,indianbank,accese raju,statebank of hyderabad,565866666666666,pause Here each record has different record length and there are blank spaces... (8 Replies)
Discussion started by: kshari8888
8 Replies

3. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: Ophiuchus
3 Replies

4. Shell Programming and Scripting

Help to find length of string avoiding trailing spaces

Hi, I have a record of length 200 bytes and values filled is only 100 bytes and remaining 100 spaces is occupied by spaces. In script wen i try to find the length of the entire record it should get as 200 not 100. i tried using length and wc -c but it doesnt work can anyone have any idea on... (3 Replies)
Discussion started by: Pranaveen
3 Replies

5. UNIX for Dummies Questions & Answers

Want to add trailing spaces to the variable

I want to keep string/varible length to 10 even its actual length is less than 10(may be no value). so, i want to add trailing spaces to my string. :wall: "typeset -L10 myvarible" is not working, its saying invalid typset -L option. Can you please advise. (4 Replies)
Discussion started by: djaks111
4 Replies

6. Shell Programming and Scripting

Remove trailing spaces from file

I'm currently writing my sql results to a file and they have trailing spaces after each field. I want to get rid of these spaces and I'm using this code: TVXTEMP=$(echo $TVXTEMP|sed -e 's/\ //g') It doesn't work though. I'm not familiar with sedscript, and the other codes I've found online... (6 Replies)
Discussion started by: avillanueva
6 Replies

7. Shell Programming and Scripting

Trim trailing spaces from each line in a file

Hello folks, Is there a simple way to trim trailing spaces from each line a file. Please let me know. Regards, Tipsy. (5 Replies)
Discussion started by: tipsy
5 Replies

8. Shell Programming and Scripting

Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help. echo $SH_VAR | command_line Syntax. The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)
Discussion started by: jerardfjay
6 Replies

9. UNIX for Dummies Questions & Answers

Adding Trailing Spaces to a file

I have a text file which is not fixed width. I want to put trailing spaces to each line and make it a 100 byte fixed width file. Can someone please help me as soon as possible? Thanks, Denis (1 Reply)
Discussion started by: 222001459
1 Replies

10. UNIX for Dummies Questions & Answers

removing trailing spaces of a particular column in a file

Hi, I am currently confused. Suppose I have a file something like the one below. 4299|raj Telecommunications|12||||| 4302|anjali International Ltd.|86|ritchie||dong|(000)2890 9993 |(222)4881 3689 4305|フィデュシアリ・ト-スト・インター...ショ...ル投資顧問株式会社 |112||||01-9211-1931 |08-3677-1985 Now... (2 Replies)
Discussion started by: rooh
2 Replies
Login or Register to Ask a Question