Cut linux примеры. Изучаем команды Linux: cut

В примерах мы будем использовать такой файл:

$ cat test.txt cat command for file oriented operations. cp command for copy files or directories. ls command to list out files and directories with its attributes.

1. Выборка колонок по символам

Для получения определённых колонок используется опция -c . В следующем примере мы получим 2-ой символ (2 колонка) в каждой строке:

$ cut -c2 test.txt a p s

2. Выборка колонок по символам с диапазоном значений

Диапазон символов можно получить, указав стартовую и конечную позицию символом « - «.

В следующем примере мы выделим символы с первого по третий в каждой строке:

$ cut -c1-3 test.txt cat cp ls

3. Выборка колонок с использованием стартовой или конечной позиции

Начальная или последняя позиция могут задавать с помощью опции -c .

В следующем примере мы задаём только стартовую колонку, с которой начинать выборку, перед символом « - «. Мы получим все символы, начиная с 3-го:

$ cut -c3- test.txt t command for file oriented operations. command for copy files or directories. command to list out files and directories with its attributes.

А в следующем примере — мы получим только первые 8 символов:

$ cut -c-8 test.txt cat comm cp comma ls comma

4. Выборка указанных полей

Вместо указания количества символов, можно задать выборку по целым полям, используя опции -d и -f . Опция -f указывает поля, которые необходимо получить, а опция -d — разделитель, по которым необходимо осуществлять разделение полей.

В следующем примере мы отобразим только первое поле файла /etc/passwd , используя разделитель « : » (двоеточие):

$ cut -d":" -f1 /etc/passwd root daemon bin sys sync games bala

5. Выборка различных полей

Вы так же можете осуществить выборку нескольких полей. В примере ниже будут выведены имена пользователей и домашние директории пользователей, у которых shell задан как /bin/bash:

$ grep "/bin/bash" /etc/passwd | cut -d":" -f1,6 root:/root bala:/home/bala

Что бы отобразить диапазон полей — укажите начальное и последнее поле, как в примере ниже. Ниже мы осуществим выборку полей с 1 по 4, 6 и 7:

$ grep "/bin/bash" /etc/passwd | cut -d":" -f1-4,6,7 root:x:0:0:/root:/bin/bash bala:x:1000:1000:/home/bala:/bin/bash

6. Выборка полей, если строка содержит разделитель

В нашем примере с /etc/passwd , если вы укажете разделитель отличный от « : » — вы получите целую строку.

В следующем примере мы укажем разделителем пайп (« | «), а утилита cut отобразит всю строку целиком, даже если такой разделитель не будет найден:

$ grep "/bin/bash" /etc/passwd | cut -d"|" -f1 root:x:0:0:root:/root:/bin/bash bala:x:1000:1000:bala,:/home/bala:/bin/bash

Однако, возможно отфильтровать строки, содержащие только разделитель, с использованием опции -s .

В следующем примере не будет показано ничего, так как -s отфильтрует строки, не содержащие разделитель « | «:

$ grep "/bin/bash" /etc/passwd | cut -d"|" -s -f1

7. Выборка всех полей, кроме заданных

Что бы использовать выборку по полям, кроме заданных — используйте опцию --complement .

В следующем примере вы выберем все поля, кроме поля 7:

$ grep "/bin/bash" /etc/passwd | cut -d":" --complement -s -f7 root:x:0:0:root:/root bala:x:1000:1000:bala,:/home/bala

8. Изменение разделителя в отображении результата

По умолчанию — в результатах разделитель будет тот же, который использовался при выборке.

Что бы изменить разделитель — используйте опцию --output-delimiter .

В примере ниже — мы заменим разделитель « : » на « # «:

$ grep "/bin/bash" /etc/passwd | cut -d":" -s -f1,6,7 --output-delimiter="#" root#/root#/bin/bash bala#/home/bala#/bin/bash

9. Заменить разделитель на символ новой строки

В следующем примере каждое поле в результатах вывода cut будет выведено в новой строке. Мы так же используем опцию --output-delimiter , но укажем ей параметр « n «:

$ grep bala /etc/passwd | cut -d":" -f1,6,7 --output-delimiter=$"n" bala /home/bala /bin/bash

10. Комбинирование утилиты Cut с другими утилитами Unix

Все возможности утилиты cut могут проявится при её использовании с другими утилитами для обработки stdout .

В следующем примере мы отобразим только самую нужную информацию из вывода утилиты ps:

$ ps axu | grep python | sed "s/s+/ /g" | cut -d" " -f2,11- 2231 /usr/bin/python /usr/lib/unity-lens-video/unity-lens-video 2311 /usr/bin/python /usr/lib/unity-scope-video-remote/unity-scope-video-remote 2414 /usr/bin/python /usr/lib/ubuntuone-client/ubuntuone-syncdaemon 2463 /usr/bin/python /usr/lib/system-service/system-service-d 3274 grep --color=auto python


Автор: Rares Aioanei
Дата публикации: 5 января 2012 года
Перевод: А. Кривошей
Дата публикации перевода: 2 ноября 2012 г.

1. Введение

Если вы думаете, что можете администрировать Linux без использования команды cut, то вы совершенно правы. Однако владение этой очень простой утилитой командной строки даст вам определенные преимущества, так как позволит повысить эффективность своей работы. Проще говоря, cut - это одна из многих утилит для фильтрации текста, предлагаемых операционной системой Linux. Она фильтрует STDIN из другой команды или из файла и отправляет фильтрованный вывод в STDOUT.

2. Часто используемые опции

Давайте без долгих предисловий перейдем сразу к делу и изучим наиболее часто используемые опции командной строки.

-b, --bytes=LIST
выбрать из файла только заданные байты согласно списку
-c, --characters=LIST
выбрать из файла заданные символы согласно списку
-f, --fields=LIST
выбирает только поля, перечисленные в списке. Разделителем по умолчанию служит TAB. Значение по умолчанию может быть переопределено с помощью опции -d.
-d, --delimiter=DELIMITER
Позволяет задать разделитель полей. Как уже говорилось выше, значением по умолчанию является TAB, но эта опция позволяет переопределить его.

3. Использование списков

Список (list) в данном случае может состоять из одного или набора байтов, символов или полей. Например, для вывода только второго байта список будет включать единственное число 2.
Поэтому:

2 - будет выведен только второй байт, символ или поле, считая с первого.
2-5 - будут выведены все байты, символы и поля со второго по пятый.
-3 - будут выведены все байты, символы и поля до четвертого.
5- - будут выведены все байты, символы или поля, начиная с пятого.
1,3,6 - будут выведены только первый, третий и шестой байты, символы или поля.
1,3- - будут выведены первый и все байты, символы или поля, начиная с третьего.

4. Выборка по символам

Приведенные ниже примеры говорят сами за себя. Мы использовали опцию -c, чтобы выводить только заданный диапазон символов из файла cut.txt.

Echo cut-command > cut.txt $ cut -c 2 cut.txt u $ cut -c -3 cut.txt cut $ cut -c 2-5 cut.txt ut-c $ cut -c 5- cut.txt command

5. Выборка по байтам

Приницип работы опции -b (побайтно) похож на описанный выше для работы с символами. Мы знаем, что один символ имеет размер 1 байт, поэтому результат выполнения команды с опцией -b будет тот же самый.

$ cut -b 2 cut.txt u $ cut -b -3 cut.txt cut $ cut -b 2-5 cut.txt ut-c $ cut -b 5- cut.txt command

cut.txt - это простой текстовый файл в кодировке ASCII. Различия будут заметны только при использовании многобайтных кодировок, таких как UTF-8 Unicode. Например:

$ echo Lubos > cut.txt $ file cut.txt cut.txt: UTF-8 Unicode text $ cut -b 1-3 cut.txt Lu $ cut -c 1-3 cut.txt Lub

6. Выборка по полям

Как уже говорилось ранее, поле, используемое командной cut по умолчанию - это TAB. Например, создадим файл, где общим разделителем является TAB.
Подсказка: если вы не знаете, как вставить TAB в командной строке, используйте комбинацию клавиш CTRL + V, перед тем, как нажать TAB.

$ echo "1 2 3" > cut.txt $ echo "4 5 6" >> cut.txt $ cat cut.txt 1 2 3 4 5 6 $ cut -f2- cut.txt 2 3 5 6

В примере выше были выведены только второй и третий столбцы, потому что TAB является общим разделителем и в то же время используется командной cut как поле по умолчанию. Убедиться в том, что вы использовали TAB вместо пробела можно с помощью команды od:

$ echo "1 2" > tab.txt $ echo "1 2" > space.txt $ od -a tab.txt 0000000 1 ht 2 nl 0000004 $ od -a space.txt 0000000 1 sp sp sp sp sp sp sp sp 2 nl 0000013

Если необходимо переопределить поведение команды, чтобы использовать другой разделитель, имеется опция -d.

$ echo 1-2-3-4 > cut.txt $ echo 5-6-7-8 >> cut.txt $ cat cut.txt 1-2-3-4 5-6-7-8 $ cut -d - -f-2,4 cut.txt 1-2-4 5-6-8

Классический пример, где нам необходимо использовать опцию -d, чтобы извлечь список пользователей текущей системы из файла /etc/passwd.

$ cut -d: -f 1 /etc/passwd root daemon bin sys sync games man lp mail news uucp proxy www-data ...

Необходимо отметить, что для получения единообразного вывода общий разделитель должен быть унифицирован по каждой строке ввода. Например, в следующем примере было бы сложно использовать пробел в качестве общего разделителя.

$ cat cut.txt cut command w command awk command wc command $ cut -d " " -f2 cut.txt command command

В этом случае было бы намного проще воспользоваться командой awk или sed, чтобы сначала заменить множественные пробелы на единый разделитель, например запятую.

$ sed "s/\s\+/,/" cut.txt | cut -d , -f2 command command command command $ awk "{ print $2; }" cut.txt command command command command

7. Исключение данных с помощью complement

Команда cut позволяет вам выборочно включать требуемые данные в вывод. В случае, если вам необходимо выбрать данные для их исключения из вывода, очень полезна опция complement.
Пример:

$ echo 12345678 > cut.txt $ cat cut.txt 12345678 $ cut --complement -c -2,4,6- cut.txt 35

8. Примеры

Синтаксис команды Описание

The cut command in UNIX is a command for cutting out the sections from each line of files and writing the result to standard output. It can be used to cut parts of a line by byte position, character and field . Basically the cut command slices a line and extracts the text. It is necessary to specify option with command otherwise it gives error. If more than one file name is provided then data from each file is not precedes by its file name.

cut OPTION... ...

Let us consider two files having name state.txt and capital.txt contains 5 names of the Indian states and capitals respectively.

$ cat state.txt

Without any option specified it displays error.

$ cut state.txt cut: you must specify a list of bytes, characters, or fields Try "cut --help" for more information.

Options and their Description with examples:

1. -b(byte): To extract the specific bytes, you need to follow -b option with the list of byte numbers separated by comma. Range of bytes can also be specified using the hyphen(-). It is necessary to specify list of byte numbers otherwise it gives error. Tabs and backspaces are treated like as a character of 1 byte.

List without ranges $ cut -b 1,2,3 state.txt And Aru Ass Bih Chh List with ranges $ cut -b 1-3,5-7 state.txt Andra Aruach Assm Bihr Chhtti

It uses a special form for selecting bytes from beginning upto the end of the line:

In this, 1- indicate from 1st byte to end byte of a line $ cut -b 1- state.txt Andhra Pradesh Arunachal Pradesh Assam Bihar Chhattisgarh In this, -3 indicate from 1st byte to 3rd byte of a line $ cut -b -3 state.txt And Aru Ass Bih Chh

2. -c (column): To cut by character use the -c option. This selects the characters given to the -c option. This can be a list of numbers separated comma or a range of numbers separated by hyphen(-). Tabs and backspaces are treated as a character. It is necessary to specify list of character numbers otherwise it gives error with this option.

$cut -c [(k)-(n)/(k),(n)/(n)] filename

Here,k denotes the starting position of the character and n denotes the ending position of the character in each line, if k and n are separated by “-” otherwise they are only the position of character in each line from the file taken as an input.

$ cut -c 2,5,7 state.txt nr rah sm ir hti

Above cut command prints second, fifth and seventh character from each line of the file.

$ cut -c 1-7 state.txt Andhra Arunach Assam Bihar Chhatti

Above cut command prints first seven characters of each line from the file.

Cut uses a special form for selecting characters from beginning upto the end of the line:

$ cut -c 1- state.txt Andhra Pradesh Arunachal Pradesh Assam Bihar Chhattisgarh Above command prints starting from first character to end. Here in command only starting position is specified and the ending position is omitted. $ cut -c -5 state.txt Andhr Aruna Assam Bihar Chhat Above command prints starting position to the fifth character. Here the starting position is omitted and the ending position is specified.

3. -f (field): -c option is useful for fixed-length lines. Most unix files doesn’t have fixed-length lines. To extract the useful information you need to cut by fields rather than columns. List of the fields number specified must be separated by comma. Ranges are not described with -f option . cut uses tab as a default field delimiter but can also work with other delimiter by using -d option.
Note: Space is not considered as delimiter in UNIX.

$cut -d "delimiter" -f (field number) file.txt

Like in the file state.txt fields are separated by space if -d option is not used then it prints whole line:

$ cut -f 1 state.txt Andhra Pradesh Arunachal Pradesh Assam Bihar Chhattisgarh

If -d option is used then it considered space as a field separator or delimiter:

$ cut -d " " -f 1 state.txt Andhra Arunachal Assam Bihar Chhattisgarh Command prints field from first to fourth of each line from the file. Command: $ cut -d " " -f 1-4 state.txt Output: Andhra Pradesh Arunachal Pradesh Assam Bihar Chhattisgarh

4. –complement: As the name suggests it complement the output. This option can be used in the combination with other options either with -f or with -c .

$ cut --complement -d " " -f 1 state.txt Pradesh Pradesh Assam Bihar Chhattisgarh $ cut --complement -c 5 state.txt Andha Pradesh Arunchal Pradesh Assa Biha Chhatisgarh

5. –output-delimiter: By default the output delimiter is same as input delimiter that we specify in the cut with -d option. To change the output delimiter use the option –output-delimiter=”delimiter” .

$ cut -d " " -f 1,2 state.txt --output-delimiter="%" Andhra%Pradesh Arunachal%Pradesh Assam Bihar Chhattisgarh

Here cut command changes delimiter(%) in the standard output between the fields which is specified by using -f option .

6. –version: This option is used to display the version of cut which is currently running on your system.

$ cut --version cut (GNU coreutils) 8.26 Packaged by Cygwin (8.26-2) Copyright (C) 2016 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later . This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by David M. Ihnat, David MacKenzie, and Jim Meyering.

Applications of cut Command

1. How to use tail with pipes(|): The cut command can be piped with many other commands of the unix. In the following example output of the cat command is given as input to the cut command with -f option to sort the state names coming from file state.txt in the reverse order.

$ cat state.txt | cut -d " " -f 1 | sort -r Chhattisgarh Bihar Assam Arunachal Andhra

It can also be piped with one or more filters for additional processing. Like in the following example, we are using cat, head and cut command and whose output is stored in the file name list.txt using directive(>).

$ cat state.txt | head -n 3 | cut -d " " -f 1 > list.txt $ cat list.txt Andhra Arunachal Assam

Thanks Saloni Gupta for providing more examples.


Команда cut позволяет выбрать из каждой строки файла нужную часть (по единому правилу для всех строк) и показать выборку на экране дисплея.

Команда имеет три основные опции:

  • -b - когда объектом выбора являются байты;
  • - когда объектом выбора являются символы;
  • -f - когда объектом выбора являются элементы форматирования текста (колонки, столбцы и прочее).

Начнем с наиболее очевидного - символов (опция -с)

Опция -с

Например, возьмем файл /etc/shells, в целом виде он выглядит так: /bin/bash /bin/tcsh /bin/csh /bin/ash /bin/ksh /bin/zsh

Теперь применим команду:

$ cut -c 1,5 /etc/shells // // // // // //

В данном примере опция -c означает, что объектом выбора будут символы, 1,5 означает, что мы выбираем символы 1 и 5 (счет идет с 1), а все остальное содержимое файла игнорируется.

Можно выбирать символы не по порядковым номерам, а диапазонами:

$ cut -c 1-5 /etc/shells /bin/ /bin/ /bin/ /bin/ /bin/ /bin/

В этом случае мы выбрали символы в диапазоне с 1 по 5 (включительно).

Можно указать несколько диапазонов через запятую:

~$ cut -c 1-5,8-9 /etc/shells /bin/sh /bin/sh /bin/h /bin/h /bin/h /bin/h

Первые три примера имеют чисто демонстрационный характер и не имеют практического смысла. Следующий пример может претендовать на некий смысл:

$ cut -c 6- /etc/shells bash tcsh csh ash ksh zsh

Тут мы получили список доступных в системе шеллов. Для этого мы не стали указывать правую границу диапазона, что заставляет команду выдавать все содержимое строк от указанного символа до конца строки. Можно оставить прочерк с левой стороны, тогда выборка пойдет от начала строки до указанного символа.

Интересно, что все эти примеры не всегда работают с русскими символами (в кодировке UTF-8, по крайней мере) - выдают какую-то странную ступенчатую распечатку:

$ cut -c 1- rus.txt йцукенгшщзхъ фывапролджэё ячсмитьбю.ёё

Это полный текст файла rus.txt. Программа сработала правильно.

$ cut -c 1-5 rus.txt йц? фы? яч? $

А вот тут начались странности. Забавно, что даже следующее приглашение командной строки вышло по "лесенке". А вот в "настоящей" консоли, вызываемой нажатием Ctrl+Alt+F2, вывод той же команды выглядел более или менее прилично:

$ cut -c 1-5 rus.txt йц фы яч

Как выяснилось в многочисленных опытах, символы кириллицы в кодировке UTF-8 состоят из двух байтов, поэтому нужно вводить удвоенные числовые значения. Скажем, если заменить в предыдущей команде 5 на 6 (или любое другое четное число), то и в эмуляторе консоли все будет как надо:

$ cut -c 1-6 rus.txt йцу фыв ячс

Для чистоты эксперимента я запустил Knoppix5.1, у которого по умолчанию кодировка koi-8, так там никакой разницы с английским текстом не наблюдается - каждая буква представлена одним байтом:

$ cut -c 1-5 rus.txt йцуке фывап ячсми

Так что с национальными кодировками ухо надо держать востро!

Надеюсь, что с опцией -c ясность полная, можно переходить к следующей.

Опция -b

Весьма похожа на предыдущую опцию, как по способу задания выборки байт, так и по результатам, ведь в большинстве случаев один байт определяет один символ. $ cut -b 1-6 /etc/shells /bin/b /bin/t /bin/c /bin/a /bin/k /bin/z

Мы выбрали первые 6 байт из каждой строки файла /etc/shells.

Точно так же как и с опцией -с, можно использовать способы выборки n,m; -n; и n-. Остается лишь добавить, что символы табуляции и backspace (возврат назад на один символ с удалением его) трактуются как любой другой символ - они и занимают один байт. (Не поручусь за национальные кодировки).

Опция -f

Объектом выбора данной опции являются те самые загадочные "поля", о которых я говорил в предисловии. Оказалось все просто. Выбираем мы колонки текста, или столбцы, или что еще там, разделенные знаком табуляции. Так как я таких файлов никогда не встречал, то пришлось создать специально для опытов. Вот содержимое файла tab.txt:

Qwer tyui op asdf ghjk llll zxcv bnm, ....

А вот команда:

$ cut -f 1,2 tab.txt qwer tyui asdf ghjk zxcv bnm,

Все как ожидалось, выбраны две первые колонки. Еще пример:

$ cut -f 2- tab.txt tyui op ghjk llll bnm, ....

В общем понятно, схема та же, только с колонками. Вопрос в другом - где взять такие файлы с колонками, разделенными знаком табуляции, чтобы их обрабатывать?

К счастью, есть опция -d, предназначенная для работы в паре с опцией -f и понимающая другие разделители текста, кроме знаков табуляции. В директории /etc полно файлов, содержащих столбцы данных, разделенных всякими разделителями; беда только, что они довольно длинные для примеров. Я возьму первые десять строчек из файла /etc/group и создам файл group10.txt.

$ cut -f 1- -d: group10.txt root::0:root bin::1:root,bin,daemon daemon::2:root,bin,daemon sys::3:root,bin,adm adm::4:root,adm,daemon tty::5: disk:!:6:root,adm,haldaemon,ya,alex lp::7:lp,ya,alex mem::8: kmem::9:

В этом примере, опция -f приказывает отбирать все столбцы, с первого до последнего, а опция -d указывает на символ, являющийся разделителем -d: разделителем служит двоеточие. Выше на экране файл group10.txt полностью.

$ cut -f 1,3 -d: group10.txt root:0 bin:1 daemon:2 sys:3 adm:4 tty:5 disk:6 lp:7 mem:8 kmem:9

А сейчас мы отобрали первый и третий столбцы, то есть узнали идентификационный номер каждой группы. В остальном все те же правила выбора объекта, что и для прочих опций. Символы, разделяющие столбцы, разумеется, могут быть иными, нежели двоеточие. Их нужно задавать при помощи опции -d <символ>. Есть и более наглядный способ употребления опции -d. Вместо только что описанной конструкции (-d <символ>) пишем --delim=<символ>, это кажется более надежным, чем при помощи пробела.

Остались две опции: -s и -n

Опция -s употребляется с опцией -f и приказывает не выводить на экран строки, не содержащие символов-разделителей. Ясно даже мне.

Оставшиеся неясности

Опция -n осталась неопознанной мною. Она велит ничего не делать с многобайтными символами, в частности не разбивать их. Что это за символы я не знаю, как не знаю и того, что команда cut делает с ними по умолчанию. Есть и еще одна загадка. Как обозначить разделитель, если таковым является пробел? Или два-три пробела? Все мои эксперименты ни к чему не привели. Может кто-нибудь знает? Напишите мне, мой адрес в начале статьи.

Внимание: Опции -c, -b, и -f могут употребляться только поодиночке.

Опции --help и --version общеизвестны, и задерживаться на них я не буду.

Резюме программы cut

Программа применима и эффективно работает с регулярными файлами, те есть с файлами, где из строчки в строчку повторяется некая структура информации (всякого рода списки, перечни, распечатки и прочее). Трудно представить себе, как ее можно использовать с обычным текстом, не разбитым на строки. Наличие трех подходов (байты, символы и столбцы) к определению выборки позволяют добиться успеха почти в любом случае, не одним способом, так другим. Особенно полезной может быть команда в качестве фильтра в составе программных каналов (pipes).

Updated: 10/17/2017 by Computer Hope

About cut

cut syntax

cut OPTION ... [FILE ]...

Options

-b , --bytes= LIST Select only the bytes LIST . LIST specifies a byte, a set of bytes, or a range of bytes; see below.
-c , --characters= LIST Select only the characters from each line as specified in LIST . LIST specifies a character, a set of characters, or a range of characters; see below.
-d , --delimiter= DELIM use character DELIM instead of a tab for the field delimiter .
-f , --fields= LIST select only these fields on each line; also print any line that contains no delimiter character, unless the -s option is specified. LIST specifies a field, a set of fields, or a range of fields; see below.
-n This option is ignored, but is included for compatibility reasons.
--complement complement the set of selected bytes, characters or fields.
-s , --only-delimited do not print lines not containing delimiters.
--output-delimiter= STRING use STRING as the output delimiter string . The default is to use the input delimiter.
--help Display a help message and exit.
--version output version information and exit.

Usage Notes

When invoking cut , use the -b , -c , or -f option, but only one of them.

If no FILE is specified, cut reads from the standard input .

Specifying LIST

Each LIST is made up of an integer , a range of integers, or multiple integer ranges separated by commas. Selected input is written in the same order that it is read, and is written to output exactly once. A range consists of:

N the N th byte, character, or field, counted from 1 .
N- from the N th byte, character, or field, to the end of the line.
N - M from the N th to the M th byte, character, or field (inclusive).
- M from the first to the M th byte, character, or field.

For example, let"s say you have a file named data.txt which contains the following text:

One two three four five alpha beta gamma delta epsilon

In this example, each of these words is separated by a tab character, not spaces. The tab character is the default delimiter of cut , so it will by default consider a field to be anything delimited by a tab.

To "cut" only the third field of each line, use the command:

Cut -f 3 data.txt

Three gamma

If instead you want to "cut" only the second-through-fourth field of each line, use the command:

Cut -f 2-4 data.txt

Which will output the following:

Two three four beta gamma delta

If you want to "cut" only the first-through-second and fourth-through-fifth field of each line (omitting the third field), use the command:

Cut -f 1-2,4-5 data.txt

Which will output the following:

One two four five alpha beta delta epsilon

Or, let"s say you want the third field and every field after it, omitting the first two fields. In this case, you could use the command:

Cut -f 3- data.txt

Which will output the following:

Three four five gamma delta epsilon

Specifying a range with LIST also applies to cut ting characters (-c ) or bytes (-b ) from a line. For example, to output only the third-through-twelfth character of every line of data.txt , use the command:

Cut -c 3-12 data.txt

Which will output the following:

E two thre pha beta g

Remember that the "space" in between each word is actually a single tab character, so both lines of output are displaying ten characters: eight alphanumeric characters and two tab characters. In other words, cut is omitting the first two characters of each line, counting tabs as one character each; outputting characters three through twelve, counting tabs as one character each; and omitting any characters after the twelfth.

Counting bytes instead of characters will result in the same output in this case, because in an ASCII -encoded text file, each character is represented by a single byte (eight bits) of data. So the command:

Cut -b 3-12 data.txt

Will, for our file data.txt , produce exactly the same output:

E two thre pha beta g

Specifying A Delimiter Other Than Tab

The tab character is the default delimiter that cut uses to determine what constitutes a field. So, if your file"s fields are already delimited by tabs, you don"t need to specify a different delimiter character.

You can specify any character as the delimiter, however. For instance, the file /etc/passwd contains information about each user on the system, one user per line, and each information field is delimited by a colon (": "). For example, the line of /etc/passwd for the root user may look like this:

Root:x:0:0:root:/root:/bin/bash

These fields contain the following information, in the following order, separated by a colon character:

  1. Username
  2. Password (shown as x if encrypted)
  3. User ID number (UID)
  4. Group ID number (GID)
  5. Comment field (used by the finger command)

The username is the first field on the line, so to display each username on the system, use the command:

Cut -f 1 -d ":" /etc/passwd

Which will output, for example:

Root daemon bin sys chope

(There are many more user accounts on a typical system, including many accounts specific to system services, but for this example we will pretend there are only five users.)

The third field of each line in the /etc/passwd file is the UID (user ID number), so to display each username and user ID number, use the command:

Cut -f 1,3 -d ":" /etc/passwd

Which will output the following, for example:

Root:0 daemon:1 bin:2 sys:3 chope:1000

As you can see, the output will be delimited, by default, using the same delimiter character specified for the input. In this case, that"s the colon character (": "). You can specify a different delimiter for the input and output, however. So, if you wanted to run the previous command, but have the output delimited by a space, you could use the command:

Cut -f 1,3 -d ":" --output-delimiter=" " /etc/passwd root 0 daemon 1 bin 2 sys 3 chope 1000

But what if you want the output to be delimited by a tab? Specifying a tab character on the command line is a bit more complicated, because it is an unprintable character. To specify it on the command line, you must "protect" it from the shell. This is done differently depending on which shell you"re using, but in the Linux default shell (bash), you can specify the tab character with $"\t" . So the command:

Cut -f 1,3 -d ":" --output-delimiter=$"\t" /etc/passwd

Will output the following, for example:

Root 0 daemon 1 bin 2 sys 3 chope 1000

cut Examples

cut -c 3 file.txt

Output the third character of every line of the file file.txt , omitting the others.

Cut -c 1-3 file.txt

Output the first three characters of every line of the file file.txt , omitting the rest.

Cut -c -3 file.txt

Same as the above command. Output the first three characters of every line of file.txt .

Cut -c 3- file.txt

Output the third through the last characters of each line of the file file.txt , omitting the first two characters.

Cut -d ":" -f 1 /etc/passwd

Output the first field of the file /etc/passwd , where fields are delimited by a colon (": "). The first field of /etc/passwd is the username , so this command will output every username in the passwd file.

Grep "/bin/bash" /etc/passwd | cut -d ":" -f 1,6

Output the first and sixth fields, delimited by a colon, of any entry in the /etc/passwd file which specifies /bin/bash as the login shell. This command will output the username and home directory of any user whose login shell is /bin/bash .

Related commands

grep — Filter text which matches a regular expression.
paste — Merge corresponding lines of files.



Понравилась статья? Поделитесь с друзьями!