File cutting 

File cutting summary 

line oriented 

  • to keep the first 10 lines of "file":

    head -10 file
  • to skip the first 10 lines of "file":

    tail +11 file
  • to skip the last 10 lines of "file":

    head -n -10 file
  • to keep the last 10 lines of "file":

    tail -10 file
  • to print lines 11-20 of a file:

    sed -e 1,10d -e 20q file
  • to cut lines by criteria, use 'grep', 'grep -v' or 'sed'.
  • to cut a file into pieces, use split

byte oriented 

  • Use head to keep the first 50m

    head -c 50m
  • to skip the first several bytes using dd

    $ seq 5 | dd ibs=1 skip=6
    4
    5
    4+0 records in
    0+1 records out
  • to skip the last 50m

    head -c -50m

column oriented 

use cut.

documented on: 2007.04.11

cmd:head 

head
head -c 50m

Help 

-n, --lines=[-]N         print the first N lines instead of the first 10;
                           with the leading `-', print all but the last
                           N lines of each file
-c, --bytes=[-]N         print the first N bytes of each file;
                           with the leading `-', print all but the last
                           N bytes of each file
SIZE  may have a multiplier suffix: b for 512, k for 1K, m for 1 Meg.

head or tail 

> > Is there any ready-made tool to print lines from a file *after* a given
> > line number?

Well, tail does that just fine; to skip the first 10 lines of "file":

tail +11 file

Another option is sed:

sed 1,10d file

The sed approach generizes better; to print lines 11-20 of a file:

sed -e 1,10d -e 20q file

-Ken Pizzini

head or tail 

All lines after line 10:

sed -n '11,$ p' <infile

Ken

documented on: 07-19-99

cmd:cut 

Usage 

ff -l . | cut -c1-12,29-
ls -l | cut -c30-42,56-
head /usr/X11R6/lib/X11/rgb.txt | cut -f 3
# X ls -l | cut -d ' ' -f 1,9 # not working!

Help 

-c list The list following -c  specifies  character  posi-
         tions  (for  instance, -c1-72 would pass the first
         72 characters of each line).
[Note]

Starting from 1.

-f, --fields field-list
      Print only the fields listed in field-list.  Fields are  sepa-
      rated by a TAB by default.
-d, --delimiter delim
      For  -f,  fields are separated by the first character in delim
      instead of by TAB.
[Note]

-d 'x' would normally always follows -f,

choosing the fields 

cut -d':' -f 2
$ cut -d: -f1,5 /etc/passwd | head
root:Super-User
daemon:
adm:Admin
lp:Line Printer Admin
smtp:Mail Daemon User
uucp:uucp Admin
[Note]

-d ' ' is not good.

It can't imitate the awk field selection: Better use -c to pick out the range if you can.

How to eliminate 1 column 

Newsgroups: comp.unix.shell
> > I need to eliminate the second column of a certain file.
>
> Sounds like a job for cut.

Only if the columns are delimited by *exactly one* space. If they look like:

Margolin    Barry    Other stuff
Doherty     John     More other stuff

then cut by itself is useless. You could, however, use sed to collapse all the spaces into a single space and then pipe that to cut.

Barry Margolin