a technical how to

David Carter-Hitchin david at carter-hitchin.clara.co.uk
Thu Dec 11 14:45:09 PST 2003


Hiya,

no-one has mentioned 'head' yet:

head -100 file > newfile

to save the first 100 lines of file into newfile.

You can also use a combination of head and tail to take a portion of the
file, e.g:

head -100 file | tail -3 > newfile

to save off lines 98,99 and 100 of file into newfile.  I've known this to
be useful when trying to extract certain lines from mammoth files.

If, by "a certain point" you meant, for example, up to some general regex
then you could employ some perl:

cat file | perl -e 'while (<>) { exit if /REGEX/; print }' > newfile

I know you can do similar things in sed and awk, but I don't know the
syntax off the top of my head, and don't have my notes to hand.

David



On Tue, 9 Dec 2003, Charles Swiger wrote:

> On Dec 8, 2003, at 8:51 PM, homeyra g wrote:
> > Here is the question: How to truncate a file from the
> > begining to a certain point in the file?
>
> The question is whether this file is ASCII text so line-based tools
> (such as tail) work, or whether you are truncating a binary file, in
> which case "split -b" is probably a better bet.
>
> If you've got a logfile named /var/log/messages, and you want to
> truncate that to the last 100 lines:
>
> mv /var/log/messages /var/log/messages.$$
> tail -100 < /var/log/messages.$$ > /var/log/messages
> rm -f /var/log/messages.$$
>
> Use "wc -l" and "grep -n" to identify where to truncate the file if
> it's not a fixed size that you want...
>
> --
> -Chuck
>
> _______________________________________________
> freebsd-questions at freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "freebsd-questions-unsubscribe at freebsd.org"
>
>


More information about the freebsd-questions mailing list