Skip to main content
The 2024 Developer Survey results are live! See the results

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.

Filter by
Sorted by
Tagged with
368 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's user avatar
203 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 ...
rubo77's user avatar
  • 29.4k
121 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? ...
gkmohit's user avatar
  • 3,349
98 votes
1 answer

How to truncate text lines to N characters maximum? [duplicate]

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's user avatar
  • 20.3k
88 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 ...
jkj2000's user avatar
  • 1,169
70 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 ...
Maxxx's user avatar
  • 851
60 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 ...
ssn's user avatar
  • 703
50 votes
8 answers

How to use grep and cut in script to obtain website URLs from an HTML file

I am trying to use grep and cut to extract URLs from an HTML file. The links look like: <a href=""> Other websites have .net, .gov, but I assume I could make the cut ...
eltigre's user avatar
  • 1,235
46 votes
5 answers

How to get last part of http link in Bash?

I have an http link : 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 ...
FunTomas's user avatar
  • 471
45 votes
3 answers

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

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: ...
Nidal's user avatar
  • 9,006
42 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 ...
odyssey's user avatar
  • 421
33 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 ...
chhaya vishwakarma's user avatar
33 votes
2 answers

Getting first n characters from a file [duplicate]

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 ...
interstar's user avatar
  • 1,077
33 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 ...
riley's user avatar
  • 361
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)....
Levi Uzodike's user avatar
24 votes
6 answers

Can I make `cut` change a file in place?

The man page doesn't give me much hope, but I'm hoping it's an undocumented (and/or GNU-specific) feature.
Hank Gay's user avatar
  • 3,559
23 votes
4 answers

Can not use `cut -c` (`--characters`) with UTF-8?

The command cut has an option -c to work on characters, instead of bytes with the option -b. But that does not seem to work, in en_US.UTF-8 locale: The second byte gives the second ASCII character (...
Volker Siegel's user avatar
19 votes
4 answers

What should I use when cut doesn't cut it?

I have a file cities like this: [1598] San Diego, US (inactive) [4517] St Louis, US (inactive) [6346] Orlando, US (inactive) I want to cut out the city names, so that I have: San Diego St Louis ...
Kit Sunde's user avatar
  • 4,524
19 votes
2 answers

coreutils that are utf aware?

When I used cut today, I found out it doesn't treat a UTF-8 character as a character, but 3 characters because it's 3 bytes long. This seems to be generally true for many tools. Are there versions ...
Chao Xu's user avatar
  • 365
16 votes
6 answers

What constitutes a 'field' for the cut command?

For example, the cut command can take a parameter -f, which according to man select only these fields; also print any line that contains no delimiter character, unless the -s ...
luca590's user avatar
  • 291
15 votes
7 answers

Combining 2 different cut outputs in a single command?

I have a file such as the following: 1234 ABCD EFGH I'd like to convert it to the following: 2341 BCDA FGHE The actual file has 4,000 words, so I would like to do this in an efficient manner. I ...
Senseful's user avatar
  • 283
14 votes
1 answer

How to unbuffer cut?

I want to get just e-mail addresses that end in "" from my mail logfile. To achieve this I do: # tail -f /var/log/mail.log | grep --i --line-buffered "" | cut -d '@' -f 1 | cut -d '<'...
Forkbeard's user avatar
  • 637
13 votes
3 answers

Dynamically trim stdout line width in Bash

Lately, I have been experimenting with the ps command, and sometimes long paths wrap to the next line (or two) and make it hard to read. I want to pipe the ps output into another program to limit the ...
lentils's user avatar
  • 133
12 votes
4 answers

Emacs command to cut OR copy to system clipboard?

As a very new Emacs user, I've been using Ctrl+w and Ctrl+y to cut/copy and paste between buffers in Emacs. Is there a way to do the same thing so that not just Emacs, but the entire system, can have ...
hpy's user avatar
  • 4,587
11 votes
3 answers

Get the part of a line before the last slash

I'm trying to write a conky script that shows my MPD album art, a 'folder.jpg' in the album folder. My current plan is to use mpc -f %file%, which prints out the file name and path, and then cut out ...
rargh's user avatar
  • 163
10 votes
4 answers

How to create numeronyms in bash

I am trying to write a code that would turn any given word to its numeronym. for example : internationalization = i18n (first char+number of chars in between+last char) I found how to find the first ...
arty's user avatar
  • 302
10 votes
7 answers

grep first n and last n characters from a line in a file

I have a log file which looks like: Mar 23 08:20:23 New file got created in sec: 235 Mar 23 08:21:45 New file got created in sec: 127 Mar 23 08:22:34 New file got created in sec: 875 Mar 23 08:25:46 ...
IAmNoob's user avatar
  • 209
10 votes
5 answers

How do I remove the last characters from a string?

I have a variable set with var='type_cardio_10-11-2017'. I need to remove the last 10 letters from the variable, and append the remaining value to var2. I tried with the following script, but it ...
Rak kundra's user avatar
10 votes
3 answers

cut command seems not to work in a pipe

I am able to filter out jobs which got stuck in our queueing system with: > qjobs | grep "racon" 5240703 racon-3/utg001564l-racon-3.fasta H 1 1 0 10.0 0.0 150 :03 ...
user977828's user avatar
10 votes
2 answers

Pipe the result of a cut command to curl

I have a csv file that contains 3 fields per line. firstname,lastname,url I'm trying to access the url via the following pipeline: grep theName file.csv | cut -d, -f 3 then I want to add another ...
wdowling's user avatar
  • 111
9 votes
4 answers

What does cut return if the specified field does not exist?

I have a string: 6.40.4 and a second one: 6.40 I am using var=$(echo $versionfull | cut -d'.' -f3) to get the third digit from the first string in bash. What does this command return for the ...
Kliwer's user avatar
  • 93
9 votes
6 answers

Grab certain contents of a file

So I know tools exist for this problem because I've heard about them, but I don't know what they are. I want to do something like filter out all data but the usernames in /etc/passwd. For example, I ...
mouche's user avatar
  • 1,325
9 votes
5 answers

cut with 2 character delimiter

I wanted to use cut to with a 2 charachter delimeter to process a file with many lines like this: 1F3C6..1F3CA 1F3CF..1F3D3 1F3E0..1F3F0 But cut only allows a single character. Instead of cut -d'..'...
Philip Kirkbride's user avatar
9 votes
7 answers

How can I apply `cut` to several files and then `paste` the results?

I often do operations like paste <(cut -d, -f1 file1.csv) <(cut -d, -f1 file2.csv) which is very tedious with more than a few files. Can I automate this process, e.g. with globbing? I can ...
shadowtalker's user avatar
  • 1,358
8 votes
2 answers

"No such file or directory" when using "-exec" with find

I have a bunch of folders which have a subfolder somewhere called 360. find . -name '360' -type d -exec 'echo "{}"' \; output: find: echo "./workspace/6875538616c6/raw/2850cd9cf25b/360": No such ...
Tamás's user avatar
  • 105
7 votes
8 answers

How to delete the first word in each line of a file?

consider a file names 'file.txt'. It contains the following. dove is a bird tiger is an animal cricket is a game. Expected output: is a bird is an animal is a game.
Velu's user avatar
  • 471
7 votes
3 answers

Using tab delimiter in Cut in Unix Shell Scripting [duplicate]

I have file input.txt with below data in TAB delimited format- 23776112 Inactive Active 23415312 Inactive Active As per requirement, Inside the while loop, I want to use ...
Praveen Verma's user avatar
7 votes
5 answers

What the equivalent of "grep | cut" using sed or awk?

Say I had a config file /etc/emails.conf email1 = [email protected] email2 = [email protected] email3 = [email protected] and I wanted to get email2 I could do a: grep email2 /etc/emails.conf | ...
Peter Turner's user avatar
  • 1,694
7 votes
3 answers

Does the inverse command of cut exist?

I like using the cut command in Linux with the -c flag. However, I'm interested in finding a command that sort of does the set inverse of cut. Essentially, given the input: drwxrwxrwx 2 root root ...
Paul Hendricks's user avatar
7 votes
3 answers

Is there a parameter substitution/expansion alternative for "| cut -f1,2,3 -d:" a.k.a. trim after and including n-th character occurence?

An ancient version of ipconfig (inside initramfs) requires its user input to supply only up to 7 colon separated elements, like: ip=client-ip:server-ip:gw-ip:netmask:hostname:device:autoconf result ...
Pro Backup's user avatar
  • 5,014
6 votes
4 answers

"Cutting" id output

I need to write a script that would "cut" the output of the id command into three parts. For example: Let's say id gives this output: uid=12345(mylogin) gid=100(users) And my script should output ...
Deividas's user avatar
6 votes
3 answers

UNIX command for replacing within delimiter based on position of the delimiter

I have a input string with | [pipe] delimiter and like to replace the empty string 3rd and 5th column by & character. Input File: a a|b b|c c|d d|e e f f|g g|h h|i i|j j Output File: a a|b b|c&...
user7952074's user avatar
6 votes
3 answers

How to use Unix Shell to show only the first n columns and last n columns?

I have many csv files. The original design was supposed to have five columns. I just found out that the middle column of the csv file has a string with arbitrary number of commas in it and it is not ...
PoorLifeChoicesMadeMeWhoIAm's user avatar
6 votes
4 answers

cut / grep and df -h

How can I grep or cut the "173G" under "Verf"? I need this for Unix scripting in school. jonas@jonaspc:~/$ df -h /dev/sda2 Dateisystem Größe Benutzt Verf. Verw% Eingehängt auf /dev/sda2 ...
jsterr's user avatar
  • 1,341
6 votes
2 answers

Get only the size of a mounted filesystem

I want to get only the total size of a mounted filesystem. But the catch is that I only know about the mount point. So, I thought of using the df command. To get the size of this mounted filesystem, ...
Abhimanyu Saharan's user avatar
6 votes
2 answers

Unix: how to read line's original content from file

I have a data file, the content is as follows: department: customer service section: A department: marketing section: A department: finance section: A When I read each line, I ...
Newbiee's user avatar
  • 85
6 votes
1 answer

Does cut return any fields if separator does not exist

I executed cut -d '~' -f 2 on input RSC AED FCB A~RS and I obtained RSC AED FCB RS I realized that cut will return entire line if the delimiter does not exist. It is that true?
nucandrei's user avatar
  • 195
6 votes
3 answers

Range of the command cut in unix

I'm trying to make a shell script, I want to cut a string using the 'cut' unix command, as follows: namecmpaux=$(echo $namecmp |cut -c0-19) But when I run the shell tells me the following error: ...
franvergara66's user avatar
6 votes
1 answer

How can I remove certain string from file name? [duplicate]

I have few files in the following directory: /var/lib/jenkins/bin/ -rwxr-xr-x. 1 root root 4430846 Apr 27 09:45 01-DSP-04.12_03_crc.bin -rwxr-xr-x. 1 root root 1659036 Apr 27 09:45 01-FL4-04.12_02-...
Sunil Shahu's user avatar
5 votes
2 answers

Print last N characters from all lines in a file using cut

I have following content in a file foobar bar bar foo foo bar bar foobar I would like to get get the last character from all the lines, also last 3 characters. I am using following for loop ...
smc's user avatar
  • 611

2 3 4 5