Questions tagged [cut]

A Unix shell command that breaks input into fields, which can be selected for output, based on a delimiter.

cut is often the easiest way to split input lines or to extract only parts from them. If the rule how to split a line into fields can be expressed with a single character, cut should be used insead of the more powerful tools sed or awk.

cut can select fixed ranges from the input line, be they bytes or characters.

The perfect match for cut is, of course, .

For Prolog's cut, see

In scientific software for statistical computing and graphics, the function cut partitions elements of a numeric vector into bins.

1687 questions
20
votes
5 answers

Subset a file by row and column numbers

We want to subset a text file on rows and columns, where rows and columns numbers are read from a file. Excluding header (row 1) and rownames (col 1). inputFile.txt Tab delimited text file header 62 9 3 54 6 1 25 1 2 3 4 5 …
zx8754
  • 52,746
  • 12
  • 114
  • 209
19
votes
10 answers

Can I delete a field in awk?

This is test.txt: 0x01,0xDF,0x93,0x65,0xF8 0x01,0xB0,0x01,0x03,0x02,0x00,0x64,0x06,0x01,0xB0 0x01,0xB2,0x00,0x76 If I run awk -F, 'BEGIN{OFS=","}{$2="";print $0}' test.txt the result…
Edward
  • 333
  • 2
  • 12
19
votes
1 answer

tail -f into grep into cut not working properly

i'm trying to build a shell script to monitor some log files. I'm using a command like this: tail -f /var/somelog | grep --line-buffered " some test and p l a c e h o l d e r" | cut -f 3,4,14 -d " " the log file is like: some test and p l a c e h o…
Andrea Tullis
  • 191
  • 1
  • 1
  • 6
18
votes
3 answers

prevent newline in cut command

Is it possible to cut a string without a line break? printf 'test.test' prints the test.test without a newline. But if I cut the output with printf 'test.test' | cut -d. -f1 there's a newline behind test.
miu
  • 1,234
  • 2
  • 18
  • 34
18
votes
2 answers

Code for extracting from the right side with cut command?

There is a sample of using the cut command to extract parts of a string starting from the left. An example is given below. $ echo "abc-def-ghi-jkl" | cut -d- -f-2 abc-def How can the same code be adapted to extracting from the right side? I thought…
vfclists
  • 19,193
  • 21
  • 73
  • 92
17
votes
7 answers

How to get the release value?

I've a file with the below name formats: rzp-QAQ_SA2-5.12.0.38-quality.zip rzp-TEST-5.12.0.38-quality.zip rzp-ASQ_TFC-5.12.0.38-quality.zip I want the value as: 5.12.0.38-quality.zip from the above file names. I'm trying as below, but not getting…
User123
  • 1,498
  • 2
  • 12
  • 26
16
votes
3 answers

How to 'cut' on null?

Unix 'file' command has a -0 option to output a null character after a filename. This is supposedly good for using with 'cut'. From man file: -0, --print0 Output a null character ‘\0’ after the end of the filename. Nice to cut(1)…
philcolbourn
  • 4,042
  • 3
  • 28
  • 33
16
votes
8 answers

Does CUT support multiple spaces as the delimiter?

I have text like this: word1 word2 word3 word4 There may be more than one space between a pair of words and I want to get some columns of words from each line . When I use cat file | cut -d ' ' -f1,2,4 it seems that some fields are space which…
notbad
  • 2,797
  • 3
  • 23
  • 34
16
votes
5 answers

How to get the count of fields in a delimited string?

Given the following data: field1;field2;field3;field4; How to get the number of fields in this string? For example, the command should give back 4 for this case. Actually, I don't have the same number of contents in each line, but I need to…
DoesJohnDo
  • 183
  • 1
  • 1
  • 5
16
votes
8 answers

Unix method to remove last 4 columns from csv file

Is there a way to use bash to remove the last four columns for some input CSV file? The last four columns can have fields that vary in length from line to line so it is not sufficient to just delete a certain number of characters from the end of…
user788171
  • 16,753
  • 40
  • 98
  • 125
16
votes
1 answer

Cut and labels/breaks length conflict

I am working with the cut function to prep data for a barplot histogram but keep running into a seeming inconsistency between my labels and breaks: Error in cut.default(sample(1:1e+05, 500, T), breaks = sq, labels = sprintf("$%.0f", : …
ako
  • 3,569
  • 4
  • 27
  • 38
15
votes
4 answers

Remove Styles from Text when Copying / Cutting using CSS or Javascript

Yo, Alright been noodling on this one for a while: How copy/cut styled text without bringing along any style baggage (background-color, color, etc)? Couple of routes of attacks that have been foiled: Style the text differently using ::select?…
Mojowen
  • 437
  • 1
  • 4
  • 17
15
votes
4 answers

Python Cut Example

I'm looking for a way in python to achieve similar functionality to the unix cut utility. I know I can make a system call and process my data that way but I'd like to make it a bit more "pythonic" and do it with python libraries. Example…
user735304
  • 447
  • 2
  • 5
  • 9
14
votes
8 answers

Unix - Need to cut a file which has multiple blanks as delimiter - awk or cut?

I need to get the records from a text file in Unix. The delimiter is multiple blanks. For example: 2U2133 1239 1290fsdsf 3234 From this, I need to extract 1239 3234 The delimiter for all records will be always 3 blanks. I need to do…
visakh
  • 2,503
  • 8
  • 29
  • 55
13
votes
4 answers

How can I remove trailing characters from a string using shell script?

How can I remove the last n characters from a particular string using shell script? This is my input: ssl01:49188,,, ssl01:49188, ssl01:49188,,,,, ssl01:49188,ssl999999:49188,,,,, ssl01:49188,abcf999:49188,,,,, The output should be in the following…
anish
  • 6,884
  • 13
  • 74
  • 140