Ana Sayfa / Linux / Bash Script / The Cut Command

The Cut Command

-f, –fieldsField-based selection
-d, –delimiterDelimiter for field-based selection
-c, –charactersCharacter-based selection, delimiter ignored or error
-s, –only-delimitedSuppress lines with no delimiter characters (printed as-is otherwise)
–complementInverted selection (extract all except specified fields/characters
–output-delimiterSpecify when it has to be different from the input delimiter

The cut command is a fast way to extract parts of lines of text files. It belongs to the oldest Unix commands. Its most popular implementations are the GNU version found on Linux and the FreeBSD version found on MacOS, but each flavor of Unix has its own. See below for differences. The input lines are read either from stdin or from files listed as arguments on the command line.

Only one delimiter character

You cannot have more than one delimiter: if you specify something like -d “,;:”, some implementations will use only the first character as a delimiter (in this case, the comma.) Other implementations (e.g. GNU cut) will give you an error message.

$ cut -d ",;:" -f2 <<<"J.Smith,1 Main Road,cell:1234567890;land:4081234567"
cut: the delimiter must be a single character
Try `cut --help' for more information.

Repeated delimiters are interpreted as empty fields

$ cut -d, -f1,3 <<<"a,,b,c,d,e"

is rather obvious, but with space-delimited strings it might be less obvious to some

$ cut -d ' ' -f1,3 <<<"a b c d e"
a b

cut cannot be used to parse arguments as the shell and other programs do.

No quoting

There is no way to protect the delimiter. Spreadsheets and similar CSV-handling software usually can recognize a text-quoting character which makes it possible to define strings containing a delimiter. With cut you cannot.

$ cut -d, -f3 <<<'John,Smith,"1, Main Street"'

Extracting, not manipulating

You can only extract portions of lines, not reorder or repeat fields.

$ cut -d, -f2,1 <<<'John,Smith,USA' ## Just like -f1,2
$ cut -d, -f2,2 <<<'John,Smith,USA' ## Just like -f2

Bunada Göz Atın

PHP Types

Type Comparison There are two types of comparison: loose comparison with == and strict comparison …

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir