|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
Code:
awk 'NR==FNR{if(length($0) > maxlen){maxlen=length($0)}next}
{printf "|%-"maxlen"s|\n",$0}' file1.txt file1.txtOnce 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
|
|||
|
|||
|
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}' fileLast 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
|
|||
|
|||
|
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}' fileThe above code gives error as Code:
expression for `<' redirection has null string value |
| Sponsored Links | |
|
|
#5
|
||||
|
||||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|