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

  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 Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 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 01-08-2013
pravin27 pravin27 is offline Forum Advisor  
Advisor
 
Join Date: Sep 2009
Last Activity: 16 April 2014, 8:22 AM EDT
Location: ./India/Mumbai
Posts: 1,201
Thanks: 54
Thanked 266 Times in 259 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 01-08-2013
Subbeh Subbeh is offline
Registered User
 
Join Date: May 2011
Last Activity: 16 April 2014, 9:30 AM EDT
Posts: 313
Thanks: 32
Thanked 81 Times in 80 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 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 01-08-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 17 April 2014, 6:04 AM EDT
Location: Amsterdam
Posts: 8,874
Thanks: 230
Thanked 2,140 Times in 1,921 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 Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 06:05 AM.