Operating System - OpenVMS
1825803 Members
2620 Online
109687 Solutions
New Discussion

Re: Can I truncate text file line to 100 characters

 
SOLVED
Go to solution
Renda Skandier
Frequent Advisor

Can I truncate text file line to 100 characters

I have a text file with variable length lines. I'd like each line to be a maximum of 100 characters.
Can this be done with cat, awk or sed?
thanks
4 REPLIES 4
Jan van den Ende
Honored Contributor

Re: Can I truncate text file line to 100 characters

Renda,

CONVERT is the tool that does this the easiest.

Make an FDL file specifying record length 100, and

$ CONVERT/FDL=/TRUNCATE/PAD

and you are home.
For /PAD use either " " (one quoted space) of %X0 (binary zero)

Alternatively, if you want to retain the shorter records as-is, make a small DCL procedure:

$ open /read ifi
$ open /write ofi
$loop:
$ read ifi rec/end=done
$ write ofi f$extract(0,100,rec)
$ goto loop
$done:
$ close ifi
$ close ofi

hth

Prosst.

Have one on me

jpe
Don't rust yours pelled jacker to fine doll missed aches.
Hein van den Heuvel
Honored Contributor
Solution

Re: Can I truncate text file line to 100 characters

Hello Renda,

Welcome to the OpenVMS Forum.
Howerer, i think upi meant to post to a Unix forum.

Yes it can be done with awk or sed.

awk '{print substr($0,1,100)}' old > new

The most basic (fastes) tool to do this job on Unix is cut:

cut -c 1-100 old > new

Check the man pages for detail!

On OpenVMS I would use perl or convert

$CONV/TRUN/FDL="RECORD; SIZE 100" old new


Cheers,
Hein.
Craig A Berry
Honored Contributor

Re: Can I truncate text file line to 100 characters

Of course Perl can do anything awk or sed can do and then some. This would be one way:

$ perl -ne "print substr($_,0,100) . qq/\n/;" < foo.old > foo.new

Another would be to use the -i edit-in-place switch:

$ perl -n -i -e "print substr($_,0,100) . qq/\n/;" foo.dat

which will create a new version of foo.dat with all the lines truncated at 100.

Both of these methods will give you some extra newlines if some of the lines in the original file were less than 100 characters long. You can avoid that by trimming newlines first:

$ perl -n -i -e "$_ =~ s/\n\z//; print substr($_,0,100) . qq/\n/;" foo.tmp
Joseph Huber_1
Honored Contributor

Re: Can I truncate text file line to 100 characters

To really truncate, the methods shown are the queckest.
If You want to keep the text beond the clolumn 100, my TRIMCARD utility might be of use:

TRIMCARD


Syntax: TRIMCARD /COLUMN:n /WRAP /WORD file [default n=80]

TRIMCARD reads a (cardimage-) input-file and writes a
new output file with the same content, but
o all text beyond column /COLUMN:n [80] removed, and
o all trailing blanks removed.

Qualifier /WRAP: wrap line at column, do not cut.
Qualifier /WORD : wrap, but at word boundaries.

See http://wwwvms.mppmu.mpg.de/~huber/util/main/trimcard.html
http://www.mpp.mpg.de/~huber