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
28
votes
8 answers

get the first 5 characters from each line in shell script

Here is my sample.txt file it contains following 31113 70:54:D2 - a-31003 31114 70:54:D2 - b-31304 31111 4C:72:B9 - c-31303 31112 4C:72:B9 - d-31302 I have to write the shell script in that I am passing first 5 characters (eg 31113) as…
user2622247
  • 1,059
  • 2
  • 15
  • 26
27
votes
6 answers

How do you parse a filename in bash?

I have a filename in a format like: system-source-yyyymmdd.dat I'd like to be able to parse out the different bits of the filename using the "-" as a delimiter.
Nick Pierpoint
  • 17,641
  • 9
  • 46
  • 74
27
votes
8 answers

Using CUT and Quartile to generate breaks in R function

Following some great advice from before, I'm now writing my 2nd R function and using a similar logic. However, I'm trying to automate a bit more and may be getting too smart for my own good. I want to break the clients into quintiles based on the…
mikebmassey
  • 8,354
  • 26
  • 70
  • 95
26
votes
8 answers

How to strip out all of the links of an HTML file in Bash or grep or batch and store them in a text file

I have a file that is HTML, and it has about 150 anchor tags. I need only the links from these tags, AKA, . I want to get only the http://www.google.com part. When I run a grep, cat website.htm | grep -E '
A'sa Dickens
  • 2,045
  • 1
  • 16
  • 21
26
votes
4 answers

Unix cut except last two tokens

I'm trying to parse file names in specific directory. Filenames are of format: token1_token2_token3_token(N-1)_token(N).sh I need to cut the tokens using delimiter '_', and need to take string except the last two tokens. In above examlpe output…
user613114
  • 2,731
  • 11
  • 47
  • 73
25
votes
4 answers

how to subset a file - select a numbers of rows or columns

I would like to have your advice/help on how to subset a big file (millions of rows or lines). For example, (1) I have big file (millions of rows, tab-delimited). I want to a subset of this file with only rows from 10000 to 100000. (2) I have big…
jianfeng.mao
  • 945
  • 3
  • 13
  • 21
25
votes
3 answers

Cut command to specify the tab as the delimiter

There is a file which the delimiter is tab ,when i use the command cut -d \t file.txt #or "\t" or "\\t" I get this message cut: you must specify a list of bytes, characters, or fields Try `cut --help' for more information. How to use the cut…
showkey
  • 482
  • 42
  • 140
  • 295
24
votes
5 answers

Is it possible to use a string as a delimiter in unix cut command?

If I want to cut a list of text using a string as a delimiter, is that possible? For example I have a directory where a list of shell scripts call same perl script say abc.pl So when I do $grep abc.pl * in that directory, it gives me following…
Devang Kamdar
  • 5,617
  • 8
  • 24
  • 16
23
votes
11 answers

Bash: Parse CSV with quotes, commas and newlines

Say I have the following csv file: id,message,time 123,"Sorry, This message has commas and newlines",2016-03-28T20:26:39 456,"It makes the problem non-trivial",2016-03-28T20:26:41 I want to write a bash command that will return only the time…
Jacob Horbulyk
  • 2,366
  • 5
  • 22
  • 34
23
votes
4 answers

Is there a cleaner way of getting the last N characters of every line?

To simplify the discussion, let N = 3. My current approach to extracting the last three characters of every line in a file or stream is to use sed to capture the last three characters in a group and replace the entire line with that group. sed…
merlin2011
  • 71,677
  • 44
  • 195
  • 329
22
votes
1 answer

Avoid Scientific notation in cut function in R

How to avoid scientific notation present in the Intervals created by the cut function. a<-seq(10000,50000, by=500 ) cut(a, breaks = seq(0,max(a)+300, by = 300)) I have tried the below but it doesn't help. options("scipen"=100, "digits"=4)
Shoaibkhanz
  • 1,942
  • 3
  • 24
  • 41
22
votes
1 answer

Using the grep and cut delimiter command (in bash shell scripting UNIX) - and kind of "reversing" it?

So I have a file with the text: puddle2_1557936:/home/rogers.williams/folderz/puddle2 I want to use the grep command grep puddle2_1557936 Mixed in with the cut command (or another command if neccessary) to display just this…
DeaIss
  • 2,525
  • 7
  • 27
  • 37
21
votes
5 answers

Shell Script to download youtube files from playlist

I'm trying to write a bash script that will download all of the youtube videos from a playlist and save them to a specific file name based on the title of the youtube video itself. So far I have two separate pieces of code that do what I want but I…
wvandaal
  • 4,265
  • 2
  • 16
  • 27
21
votes
2 answers

Delete everyting preceding and including a certain substring from variable

In Bash, how can I delete characters from a variable until a certain substring? Example: ananas1kiwi2apple1banana2tree shall look like this: apple1banana2tree The substring in this case is 2.
Anne K.
  • 345
  • 2
  • 4
  • 7
21
votes
6 answers

Bash: How to trim whitespace before using cut

I have a line of output from a command like this: []$ | grep "Memory Limit" Memory Limit: 12345 KB I'm trying to pull out the 12345. The first step - separating by the colon - works fine []$ | grep "Memory Limit" | cut…
user2824889
  • 1,085
  • 5
  • 16
  • 28