Formatting File having big single line into 95 Char Per Line

Hi All,

I have 4 big files which contains one big line containing formatted character records, I need to format each file in such way that each File will have 95 Characters per line. Last line of each file will have newline character at end.

Before:-
File Name:- File1.dat

102 121340560 1210001341210300400A094101ABCDEFG ABCDEFGH SEDABCD ABCDIFARION RFTPERSR        1234ABCDCARPABCDEF                   0366130207ASDFGYHUIO11011212110130610710034042404246222910750809020101142       0000026950329075001      ABCRFTYU DERTF           0071003402508271820000000100291075080000000000000000000269500366

After:-

102 121340560 1210001341210300400A094101ABCDEFG ABCDEFGH SEDABCD ABCDIFARION RFTPERSR        
1234ABCDCARPABCDEF                   0366130207ASDFGYHUIO110112121101306107100340424042462229
10750809020101142       0000026950329075001      ABCRFTYU DERTF           0071003402508271820
000000100291075080000000000000000000269500366                                                

Similar remaining file will be formatted in same fashion.
Thanks in advance guys.
-LanceSunny

Try:

 dd if=<infilename> of=<outfilename> cbs=95 conv=unblock 
1 Like

vbe Thanks for you help regarding this.
I ma sorry but when I execute this command I get either file not found or 0+0 records in & o bytes copied.

dd if=<File1.dat> of=<File1Final.dat> cbs=95 conv=unblock

Here File1 is the file name
File1Final.dat trying to copy finalize contents to new file.
Thanks,
Lance

Remove the "<>" around the file names

dd if=File1.dat of=File1Final.dat cbs=95 conv=unblock
1 Like
fold -w95 file
2 Likes

Thanks vbe, scottaazz & Scrutinizer. All your solution worked.
Can you guys please tell me what I need to do in order to add new line after only last line in the file?

Here are the command which worked:-

fold -w95 fileName 

OR

dd if=FileName.dat of=NewFile.dat cbs=95 conv=unblock

Thanks,
Lance

dd's unblock conversion yields variable length records/lines. If, as you say, the result must consist of 95 character lines, you cannot use that dd approach, because it will strip trailing space characters from a 95 character block and leave something shorter.

Regards,
Alister

1 Like

Do you mean like this?

{ fold -w95 infile; echo; } > outfile
1 Like

Thanks Scrutinizer It worked.

---------- Post updated at 11:14 AM ---------- Previous update was at 10:38 AM ----------

Yes I saw this.
DD nevers keeps length 95 per line for some lines.

Even

{ fold -w95 infile; echo; } > outfile

works till second last line. Last line do not have 95 characters.
How we can make sure last line will also have 95 Characters.

You could try:

oldIFS=$IFS
IFS="
"
printf "%-95s\n" $(fold -w95 infile1824)
IFS=$oldIFS
1 Like

If fold is used, and if the input text may contain tabs (or backspace characters), if those bytes should not be treated specially (count as 1 byte instead of modifying fold's width counter), the -b option should be used.

Here's a defensive pipeline:

od -An -tu1 < file |
tr -cs 0-9 '[\n*]' |
awk 'length {printf "%c%s", $0+0, (++i%w ? "" : "\n")} END {if (i%w) {while (i++%w) printf " "; printf "\n"}}' w=95

Regards,
Alister

1 Like