Unix cut file by lines




















Basically the cut command slices a line and extracts the text. The cut command can be used to print characters in a line by specifying the position of the characters.

To print the characters in a line, use the -c option in cut command cut -c4 file. You can print more than one character at a time by specifying the character positions in a comma separated list as shown in the below example cut -c4,6 file.

You can print a range of characters in a line by specifying the start and end position of the characters. To print the first six characters in a line, omit the start position and specify only the end position. You can use the cut command just as awk command to extract the fields in a file using a delimiter.

The -d option in cut command can be used to specify the delimiter and -f option is used to specify the field position. You can print more than one field by specifying the position of the fields in a comma delimited list.

Note: If the delimiter you specified is not exists in the line, then the cut command prints the entire line. To suppress these lines use the -s option in cut command. You can print a range of fields by specifying the start and end position. 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:.

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:. 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. Remember that the "space" 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 results 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:. 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. These fields contain the following information, in the following order, separated by a colon character:.

The username is the first field on the line, so to display each username on the system, use the command:. Let us consider two files having name state.

Options and their Description with examples:. 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. 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 uses a special form for selecting characters from beginning upto the end of the line:. Skip to content.



0コメント

  • 1000 / 1000