The cut command cuts sections of each line of input files and writes it on its standard output. It is mostly used for taking out a few columns from the input files. The command syntax is,
cut option... [FILE]...
You can cut out bytes, characters or fields from files.
2.0 Cut fields
The most common use is to cut fields. The default field delimiter is the tab character. The field number starts with 1. For example, to get a list of second names in the names file, we can give the command,
$ cat names
Erika Mustermann
Jane Doe
Joe Bloggs
John Doe
John Roe
Max Mustermann
Richard Roe
Tommy Atkins
$ cut -f2 -d' ' names
Mustermann
Doe
Bloggs
Doe
Roe
Mustermann
Roe
Atkins
The -f option specifies the field to be cut. The -d option specifies the field delimiter.
3.0 Specifying field numbers
We can cut multiple fields. For example, if we want a list of user ids along with the default shell, we can cut out the first and the seventh fields from /etc/passwd, using
:as the delimiter.
$ cut -f1,7 -d':' /etc/passwd
root:/bin/bash
daemon:/usr/sbin/nologin
bin:/usr/sbin/nologin
sys:/usr/sbin/nologin
sync:/bin/sync
games:/usr/sbin/nologin
...
While specifying field numbers, we can give ranges like, m-n. A range -n means fields 1 through n. Similarly, a range m-means fields m through till the last field. For example,
$ cut -f-3,5,6- -d':' /etc/passwd | head
root:x:0:root:/root:/bin/bash
daemon:x:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:bin:/bin:/usr/sbin/nologin
sys:x:3:sys:/dev:/usr/sbin/nologin
sync:x:4:sync:/bin:/bin/sync
games:x:5:games:/usr/games:/usr/sbin/nologin
man:x:6:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:news:/var/spool/news:/usr/sbin/nologin
4.0 Complement of selected fields
The cut command selects fields identified with the -f option. We can find the complement of these fields using the --complement option. For example,
$ cut -f-3 -d':' /etc/passwd | head
root:x:0
daemon:x:1
bin:x:2
sys:x:3
sync:x:4
games:x:5
man:x:6
lp:x:7
mail:x:8
news:x:9
$
$ cut --complement -f-3 -d':' /etc/passwd | head
0:root:/root:/bin/bash
1:daemon:/usr/sbin:/usr/sbin/nologin
2:bin:/bin:/usr/sbin/nologin
3:sys:/dev:/usr/sbin/nologin
65534:sync:/bin:/bin/sync
60:games:/usr/games:/usr/sbin/nologin
12:man:/var/cache/man:/usr/sbin/nologin
7:lp:/var/spool/lpd:/usr/sbin/nologin
8:mail:/var/mail:/usr/sbin/nologin
9:news:/var/spool/news:/usr/sbin/nologin
5.0 Suppress lines not containing the delimiter
By default, if a line did not contain the delimiter, it is printed in the output. We can suppress printing of lines not containing the delimiter using the -s option. For example, if the password file had a few comments at the top,
$ cut -f-3 -d':' passwd | head
# The system password file.
# Please ensure that this file is accessible to administrators only.
root:x:0
daemon:x:1
bin:x:2
sys:x:3
sync:x:4
games:x:5
man:x:6
$
$ cut -f-3 -d':' -s passwd | head
root:x:0
daemon:x:1
bin:x:2
sys:x:3
sync:x:4
games:x:5
man:x:6
...
6.0 Specifying output delimiter
The output need not contain the abstruse delimiter of the input file. We can set the output delimiter string using the --output-delimiter option. For example,
$ cut -f1,7 -d':' --output-delimiter ' ' /etc/passwd
root /bin/bash
daemon /usr/sbin/nologin
bin /usr/sbin/nologin
sys /usr/sbin/nologin
sync /bin/sync
games /usr/sbin/nologin
...
7.0 Cutting out bytes, characters
The above discussion focused on cutting fields from files. We can cut bytes and characters from files by framing similar commands, replacing -f with the -b option for bytes and -c option for characters, respectively. In case of bytes and characters, the input delimiter is not meaningful and can not be specified as a part of the command. The byte and character numbers start from one for each line.
0 comments:
Post a Comment