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
510
votes
14 answers

How to find the last field using 'cut'

Without using sed or awk, only cut, how do I get the last field when the number of fields are unknown or change with every line?
noobcoder
  • 6,089
  • 2
  • 18
  • 34
412
votes
8 answers

Use space as a delimiter with cut command

I want to use space as a delimiter with the cut command. What syntax can I use for this?
Jaelebi
  • 5,879
  • 8
  • 32
  • 34
389
votes
17 answers

How to split a string in shell and get the last field

Suppose I have the string 1:2:3:4:5 and I want to get its last field (5 in this case). How do I do that using Bash? I tried cut, but I don't know how to specify the last field with -f.
cd1
  • 15,908
  • 12
  • 46
  • 47
336
votes
6 answers

How to make the 'cut' command treat same sequental delimiters as one?

I'm trying to extract a certain (the fourth) field from the column-based, 'space'-adjusted text stream. I'm trying to use the cut command in the following manner: cat text.txt | cut -d " " -f 4 Unfortunately, cut doesn't treat several spaces as one…
mbaitoff
  • 8,831
  • 4
  • 24
  • 32
331
votes
19 answers

Copy and paste content from one file to another file in vi

I am working with two files, and I need to copy a few lines from one file and paste into another file. I know how to copy (yy) and paste (p) in the same file. But that doesn't work for different files. How is this done? Also, is there a way to…
macha
  • 7,337
  • 19
  • 62
  • 84
268
votes
16 answers

How can I remove the extension of a filename in a shell script?

What's wrong with the following code? name='$filename | cut -f1 -d'.'' As is, I get the literal string $filename | cut -f1 -d'.', but if I remove the quotes I don't get anything. Meanwhile, typing "test.exe" | cut -f1 -d'.' in a shell gives me the…
mimicocotopus
  • 5,280
  • 4
  • 22
  • 24
226
votes
12 answers

How to specify more spaces for the delimiter using cut?

Is there any way to specify a field delimiter for more spaces with the cut command? (like " "+) ? For example: In the following string, I like to reach value '3744', what field delimiter I should say? $ps axu | grep jboss jboss 2574 0.0 0.0 …
leslie
  • 11,858
  • 7
  • 23
  • 22
179
votes
10 answers

Loop through a comma-separated shell variable

Suppose I have a Unix shell variable as below variable=abc,def,ghij I want to extract all the values (abc, def and ghij) using a for loop and pass each value into a procedure. The script should allow extracting arbitrary number of comma-separated…
Ramanathan K
  • 1,829
  • 3
  • 13
  • 8
169
votes
9 answers

Rearrange columns using cut

I am having a file in the following format Column1 Column2 str1 1 str2 2 str3 3 I want the columns to be rearranged. I tried below command cut -f2,1 file.txt The command doesn't reorder the columns. Any idea why its not…
Boolean
  • 14,266
  • 30
  • 88
  • 129
96
votes
4 answers

Using cut command to remove multiple columns

given input echo 1,2,3,4,5,6,7,8,9,...100 If I want to cut columns 5 I can do cut -d, -f-4,6- what if I want to cut multiple non consecutive columns like 5, 7,etc is there a one liner?
user121196
  • 30,032
  • 57
  • 148
  • 198
81
votes
6 answers

Using ffmpeg to cut audio from/to position

I need to cut parts from an audio file from position to position. When I tried this command ffmpeg -ss 132 -t 139 -i original.mp3 new.mp3 it started at the second 132, and added the next 139 seconds to the new file. What I need is from second 132…
Mike
  • 2,051
  • 4
  • 28
  • 46
76
votes
9 answers

how to remove the first two columns in a file using shell (awk, sed, whatever)

I have a file with many lines in each line there are many columns(fields) separated by blank " " the numbers of columns in each line are different I want to remove the first two columns how to?
wenzi
  • 1,235
  • 2
  • 11
  • 11
70
votes
3 answers

How to get first n characters of each line in unix data file

I am trying to get the first 22 characters from a unix data file.Here is my data looks as below. First 12 characters is column 1 and next 10 characters is 2nd column. 000000000001199998000180000 DUMMY RAG # MFR NOT ST 1999980 …
Teja
  • 13,214
  • 36
  • 93
  • 155
63
votes
7 answers

How can I find the minimum cut on a graph using a maximum flow algorithm?

I need to find the minimum cut on a graph. I've been reading about flow networks, but all I can find are maximum flow algorithms such as Ford-Fulkerson, push-relabel, etc. Given the max flow-min cut theorem, is it possible to use one of those…
cesarbs
  • 894
  • 1
  • 8
  • 9
57
votes
8 answers

How to cut off string after the first line in the paragraph

I have the string as show below: XXX:Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur cursus lacus sed justo faucibus id pellentesque nunc porttitor. Sed venenatis tempor dui, nec mattis dolor ultrices at. Duis suscipit, dolor sed…
Zhen
  • 12,361
  • 38
  • 122
  • 199
1
2 3
99 100