Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I am trying to modify a script so that it will truncate the output that goes to another file to a maximum (for example: 1000 lines).

I have read about split, but what I understand is that split by default does 1000 by default and splits that file into smaller files.

But, there may be a time when the output isn't going to be 1000 lines. It may only be 100.

I just want to cap the output at no more than X amount.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

If you want to split the output into multiple files, each limited to 1000 lines, then use split.

If you just want to "truncate the output that goes to another file to a maximum (for example: 1000 lines)," then use head:

cmd | head -n1000 >output_file

The -n option tells head to limit the number of lines of output. Alternatively, to limit the output by number of bytes, the -c option would be used. For details, see man head.

The companion utility to head is tail. One uses tail when one wants the end of a file, rather the the beginning. Thus, tail -n1000 would deliver the last 1,000 lines of a file.

share|improve this answer
    
Thank you very much! –  user2419571 Jan 29 at 18:29
    
Thank you for the explanation too! –  user2419571 Jan 29 at 18:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.