Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Force record size to 128 bytes 2

Status
Not open for further replies.

dickiebird

Programmer
Feb 14, 2002
758
GB
Can anyone tell me / point me to the unix command(s)
to space-fill/expand variable length records(CR/NL terminated) in my file to all be 128 bytes. eg.....
A02044842WWTV2I575257471642 01642 0
A02044843WWTV2I575257471642 01642 5752
A03237555WWTV2I632963241642 01642 6329
A03251197WWTV2I684868430001 00001 6848
A03262086WWTV2I687568700512 00512 0
to
A02044842WWTV2I575257471642 01642 0
A02044843WWTV2I575257471642 01642 5752
A03237555WWTV2I632963241642 01642 6329
A03251197WWTV2I684868430001 00001 6848
A03262086WWTV2I687568700512 00512 0
T.I.A


Dickie Bird (:)-)))
 
Dickie, man fold - we use this to expand lines. I'll post further details if need to (just off for lunch!). HTH.
 
Yes please - I've looked at the man pages and tried fold -w128 , but to no avail. Dickie Bird (:)-)))
 
Dickie,

this is an extract from a script we use to pad to 100 characters (using Solaris):

###################################
# remove previous temporary files #
###################################

rm ken.new > /dev/null
rm ken.fold > /dev/null
rm ken.del > /dev/null
rm ken.sed > /dev/null
rm ken.spac > /dev/null
rm ken.nosp > /dev/null

#########################################################
# Replace all spaces with ` character to keep structure #
#########################################################

sed s/" "/\`/g $1 > ken.spac


########################################
# pad each line to over 100 characters #
########################################

cat ken.spac |
while read line
do
echo ${line}"??????????????????????????????????????????????
?????????????" >> ken.new

done

###############################
# Fold each line to 100 chars #
###############################

fold -b -w 100 ken.new > ken.fold

#########################
# Delete spurious lines #
#########################

sed /^?/d ken.fold > ken.del

######################
# Remove all ? chars #
######################

sed s/?/" "/g ken.del > ken.sed

##########################################
# Remove all `'s and replace with spaces #
##########################################

sed s/\`/" "/g ken.sed > ken.nosp

ken.nosp then becomes the padded file and can be renamed as necessary - replace the 100 with 128 in your case. I'm not too sure about the use of temp files in this way, but if it works I'm not about to fix it! Hope this helps or at least gives you a pointer in the right direction. Cheers.
 
Clearly Predamarcel's version needs (much) less typing - must really brush up on my awk one of these days!!!
 
Predamarcel - have a star! I'd have used (g)awk if there
wasn't a unix command - but it wouldn't have been as neat as
yours ( I'd have checked lengths of $0 and added " " enough times - much slower)
Ken - thanks for your time. I'm surprised there isn't a *nix command. I looked up format, fold, pack, expand, fill etc etc - No joy
Cheers guys ! Dickie Bird (:)-)))
 
or more succinct:

(while IFS=&quot;\n&quot; read LINE; do printf &quot;%-128s\n&quot; $LINE; done) < /tmp/in.txt > /tmp/out.txt vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top