Questions tagged [cut]

For questions pertaining to cut, a filter program which extracts fields or columns of each line of input. Use this tag for questions about cut itself or questions about issues arising from using the cut utility.

The tag should be used for questions that are about the utility itself or about issues arising from the usage of the utility.

Relevant questions

How to define 'tab' delimiter with 'cut' in BASH?

Split string by delimiter and get N-th element

External reference

cut specification (The Open Group Base Specifications Issue 7, 2018 edition)

436 questions
355
votes
6 answers

How to define 'tab' delimiter with 'cut' in Bash?

Here is an example of using cut to break input into fields using a space delimiter, and obtaining the second field: cut -f2 -d' ' How can the delimiter be defined as a tab, instead of a space?
Muhammad Hasan Khan
  • 3,653
  • 2
  • 14
  • 6
176
votes
10 answers

How do I use cut to separate by multiple whitespace?

I have this input, which is displayed in columns. I would like to get the second last column with the numbers of this sample: [ 3] 1.0- 2.0 sec 1.00 MBytes 8.39 Mbits/sec [ 3] 2.0- 3.0 sec 768 KBytes 6.29 Mbits/sec [ 3] 3.0- 4.0 sec 512…
rubo77
  • 27,777
  • 43
  • 130
  • 199
118
votes
6 answers

Replace multiple spaces with one using 'tr' only

I have a file, f1.txt: ID Name 1 a 2 b 3 g 6 f The number of spaces is not fixed. What is the best way to replace all the white spaces with one space using only tr? This is what I have so far: cat f1.txt | tr -d "…
gkmohit
  • 3,219
  • 8
  • 27
  • 30
95
votes
1 answer

How to truncate text lines to N characters maximum?

Given a text file, or the output of a command, how can I truncate it so that every line longer than N characters (usually N=80 in a terminal) gets shorten to N characters maximum?
Totor
  • 19,302
  • 17
  • 75
  • 102
83
votes
8 answers

get first X characters from the cat command?

I have a text file I'm outputting to a variable in my shell script. I only need the first 50 characters however. I've tried using cat ${filename} cut -c1-50 but I'm getting far more than the first 50 characters? That may be due to cut looking for…
jkj2000
  • 1,109
  • 2
  • 9
  • 9
65
votes
2 answers

Counting occurrences of word in text file

I have a text file containing tweets and I'm required to count the number of times a word is mentioned in the tweet. For example, the file contains: Apple iPhone X is going to worth a fortune The iPhone X is Apple's latest flagship iPhone. How will…
Maxxx
  • 801
  • 1
  • 8
  • 8
57
votes
3 answers

How to cut (select) a field from text line counting from the end?

I know how to select a field from a line using the cut command. For instance, given the following data: a,b,c,d,e f,g,h,i,j k,l,m,n,o This command: cut -d, -f2 # returns the second field of the input line Returns: b g l My question: How can I…
ssn
  • 673
  • 1
  • 5
  • 6
45
votes
3 answers

What are the exact differences between awk and cut with grep?

We know that we can get the second column of the line we want from a file using these two techniques: awk '/WORD/ { print $2 }' filename or grep WORD filename| cut -f 2 -d ' ' My questions are: What are the differences between the two commands…
Nidal
  • 8,856
  • 11
  • 55
  • 74
41
votes
5 answers

How to get last part of http link in Bash?

I have an http link : http://www.test.com/abc/def/efg/file.jar and I want to save the last part file.jar to variable, so the output string is "file.jar". Condition: link can has different length e.g.: http://www.test.com/abc/def/file.jar. I…
FunTomas
  • 421
  • 1
  • 4
  • 5
41
votes
12 answers

A command to print only last 3 characters of a string

I know that the cut command can print the first n characters of a string but how to select the last n characters? If I have a string with a variable number of characters, how can I print only the last three characters of the string. eg. "unlimited"…
odyssey
  • 411
  • 1
  • 4
  • 3
30
votes
2 answers

Getting first n characters from a file

I want to get the first 20 or so characters from a number of files. I've seen examples using cut but they all seem to get the first 20 characters of each line in the file, while I only want the first characters in the file itself (ie. from the…
interstar
  • 1,027
  • 3
  • 14
  • 27
30
votes
7 answers

cut column 2 from text file

My text file has no delimiter to specify separator just spaces, how do I cut out column 2 to output file, 39 207 City and County of San Francisc REJECTED MAT = 0 78 412 Cases and materials on corporat REJECTED MAT =…
riley
  • 331
  • 1
  • 4
  • 4
28
votes
3 answers

cut string on last delimiter

I have a filename like a.b.c.txt, I want this string to be split as string1=a.b.c string2=txt Basically I want to split filename and its extension. I used cut but it splits as a,b,c and txt. I want to cut the string on the last delimiter. Can…
chhaya vishwakarma
  • 693
  • 6
  • 12
  • 18
27
votes
8 answers

Is piping, shifting, or parameter expansion more efficient?

I'm trying to find the most efficient way to iterate through certain values that are a consistent number of values away from each other in a space separated list of words(I don't want to use an array). For example, list="1 ant bat 5 cat dingo 6 emu…
Levi Uzodike
  • 458
  • 4
  • 13
1
2 3
29 30