Remove new line character and add space to convert into fixed width file | Unix Linux Forums | Shell Programming and Scripting

  Unix/Linux Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Remove new line character and add space to convert into fixed width file

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-08-2013
Amrutha24 Amrutha24 is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 11 November 2013, 1:54 AM EST
Location: India
Posts: 17
Thanks: 9
Thanked 0 Times in 0 Posts
Remove new line character and add space to convert into fixed width file

I have a file with different record length. The file as to be converted into fixed length by appending spaces at the end of record. The length should be calculated based on the record with maximum length in the file.

If the length is less than the max length, the spaces should be appended before the new line character of that record.


Code:
 
Input File:
123
12345
1234
 
Output file:
123  
12345
1234 
 
Here the max record length is 5

Sponsored Links
    #2  
Old Unix and Linux 01-08-2013
pravin27 pravin27 is offline Forum Advisor  
Advisor
 
Join Date: Sep 2009
Last Activity: 13 March 2015, 8:00 AM EDT
Location: ./India/Bangalore
Posts: 1,231
Thanks: 57
Thanked 283 Times in 276 Posts

Code:
awk 'NR==FNR{if(length($0) > maxlen){maxlen=length($0)}next}
{printf "|%-"maxlen"s|\n",$0}' file1.txt file1.txt

Once you are confirm with o/p then remove "|" from the above code
The Following User Says Thank You to pravin27 For This Useful Post:
Amrutha24 (01-08-2013)
Sponsored Links
    #3  
Old Unix and Linux 01-08-2013
Subbeh Subbeh is offline
Registered User
 
Join Date: May 2011
Last Activity: 27 January 2015, 8:40 AM EST
Posts: 330
Thanks: 36
Thanked 86 Times in 85 Posts
This is another way of doing it with awk which works for me:

Code:
awk 'BEGIN{while((getline f<FILENAME)>0){if(length(f)>l)l=length(f)}}{printf "%-"l"s\n",$0}' file


Last edited by Subbeh; 01-08-2013 at 07:34 AM.. Reason: Edit: Never mind, pavin27's example works fine :)
The Following User Says Thank You to Subbeh For This Useful Post:
Amrutha24 (01-08-2013)
    #4  
Old Unix and Linux 01-08-2013
Amrutha24 Amrutha24 is offline
Registered User
 
Join Date: Jan 2013
Last Activity: 11 November 2013, 1:54 AM EST
Location: India
Posts: 17
Thanks: 9
Thanked 0 Times in 0 Posts
The command is working fine but when i redirect the output to a file at the actual end of record i am able to see Control M(^M) charater.


Code:
 
awk 'NR==FNR{if(length($0) > maxlen){maxlen=length($0)}next}
{printf "%-"maxlen"s\n",$0}' file1.txt file1.txt > file2.txt
 
Output file: file2.txt
123^M<2space>
12345^M
1234^M<1space>

---------- Post updated at 06:16 PM ---------- Previous update was at 06:10 PM ----------


Code:
 
awk 'BEGIN{while((getline f<FILENAME)>0){if(length(f)>l)l=length(f)}}{printf "%-"l"s\n",$0}' file

The above code gives error as
Code:
expression for `<' redirection has null string value

Sponsored Links
    #5  
Old Unix and Linux 01-08-2013
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 1 April 2015, 12:31 PM EDT
Location: Amsterdam
Posts: 10,064
Thanks: 324
Thanked 2,631 Times in 2,352 Posts
You need to use files that are in unix-format, not DOS-format. To convert:

Code:
tr -d '\r' < infile > outfile

The Following User Says Thank You to Scrutinizer For This Useful Post:
Amrutha24 (01-08-2013)
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
convert # delimited text file to fixed width chumsky UNIX for Dummies Questions & Answers 3 07-07-2011 02:46 AM
How to remove new line character and append new line character in a file? sasikari HP-UX 8 10-27-2010 06:09 PM
Remove duplicates based on a column in fixed width file Qwerty123 UNIX for Dummies Questions & Answers 1 07-15-2010 06:37 AM
Appending string (charachters inside the line) to a fixed width file using awk or sed tamahomekarasu Shell Programming and Scripting 6 12-09-2009 02:35 AM



All times are GMT -4. The time now is 12:34 PM.