Cut command in Linux

  • by

1.0 cut

Cut command in Linux

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 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

You may like these, also

  • POSIX message queues in LinuxPOSIX message queues in Linux 1.0 POSIX Message queues POSIX interprocess comunication (IPC) was introduced in the POSIX.1b standard (IEEE Std 1003.1b-1993) for real time…
  • POSIX Shared Memory in LinuxPOSIX Shared Memory in Linux 1.0 Shared Memory Shared memory is the fastest method of interprocess communication (IPC) under Linux and other Unix-like systems. The…
  • POSIX Semaphores in LinuxPOSIX Semaphores in Linux 1.0 Semaphores Semaphores are used for process and thread synchronization. Semaphores are clubbed with message queues and shared memory under…
  • fork and exec system calls in Linuxfork and exec system calls in Linux 1.0 fork and exec system calls Suppose we wish to write a "shell program" which would execute another program. Now,…
  • Connecting two computers with Ethernet LAN cableConnecting two computers with Ethernet LAN cable Quite often, we wish to connect two computers back to back using an Ethernet LAN cable. It may be because…
  • D-Bus TutorialD-Bus Tutorial 1.0 D-Bus D-Bus is a mechanism for interprocess communication under Linux and other Unix-like systems. D-Bus has a layered architecture.…
  • Socket programming using the select system callSocket programming using the select system call 1.0 Client-Server Paradigm The Client-Server paradigm divides the software architecture of a system in two parts, the server and its…
  • System V message queues in LinuxSystem V message queues in Linux 1.0 Message queues Message queues are one of the interprocess communication mechanisms available under Linux. Message queues, shared memory and…
  • POSIX Threads Synchronization in CPOSIX Threads Synchronization in C 1.0 POSIX Threads Synchronization POSIX Threads provide multiple flows of execution within a process. The threads have their own stacks…
  • System V Shared Memory in LinuxSystem V Shared Memory in Linux 1.0 Shared Memory Shared memory is one of the three interprocess communication (IPC) mechanisms available under Linux and other Unix-like…