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
52
votes
6 answers

How to get second last field from a cut command

I have a set of data as input and need the second last field based on deleimiter. The lines may have different numbers of delimiter. How can I get second last field ? example input text,blah,blaah,foo this,is,another,text,line expected…
Archit Jain
  • 2,154
  • 1
  • 18
  • 32
50
votes
4 answers

Parsing the first column of a csv file to a new file

Operating System: OSX Method: From the command line, so using sed, cut, gawk, although preferably no installing modules. Essentially I am trying to take the first column of a csv file and parse it to a new file. Example input…
S1syphus
  • 1,401
  • 5
  • 20
  • 29
49
votes
5 answers

cat, grep and cut - translated to python

maybe there are enough questions and/or solutions for this, but I just can't help myself with this one question: I've got the following command I'm using in a bash-script: var=$(cat "$filename" | grep "something" | cut -d'"' -f2) Now, because…
dnc
  • 600
  • 1
  • 5
  • 13
45
votes
2 answers

How to use cut with multiple character delimiter in Unix?

My file looks like this abc ||| xyz ||| foo bar hello world ||| spam ham jam ||| blah blah I want to extract a specific column, e.g. I could have done: sed 's/\s|||\s/\\t/g' file | cut -f1 But is there another way of doing that?
alvas
  • 115,346
  • 109
  • 446
  • 738
43
votes
3 answers

cutting a string into several lines in bash

I want to take the path of the local directory and put each directory on the path in a different line. I've tried to do it using cut: pwd | cut -f 1- -d\/ --output-delimiter=\n but it doesn't change the '/'s into EOL, but puts n's instead. What am I…
SIMEL
  • 8,745
  • 28
  • 84
  • 130
43
votes
5 answers

Remove first n character from bunch of file names with cut

I am using ls | cut -c 5- This does return a list of the file names in the format i want them, but doesn't actually perform the action. Please advise.
Ridalgo
  • 651
  • 1
  • 6
  • 10
41
votes
4 answers

How to use cut command in bash to show all columns except those indicated?

I need to remove a column from a plain text file. I think this could be done using the inverse of the cut command. I mean, something like this: If this is my file: 01 Procedimiento_tal retiro aceptado 01 tx1 01 tx2 01 tx3 02 Procedimiento_tal retiro…
Hermandroid
  • 2,120
  • 4
  • 29
  • 35
41
votes
3 answers

Printing only the first field in a string

I have a date as 12/12/2013 14:32 I want to convert it into only 12/12/2013. The string can be 1/1/2013 12:32 or 1/10/2013 23:41 I need only the date part.
user2099444
  • 421
  • 1
  • 4
  • 3
39
votes
4 answers

Using bash ps and cut together

I need to extract PID, UID and command fields from 'ps' and I have tried it like this: ps -L u n | cut -f 1,2,13 For some reason, this behaves as there is no cut command whatsoever. It just returns normal ps output. Then, I tried ps -L u n | tr -s…
darxsys
  • 1,560
  • 4
  • 20
  • 34
35
votes
3 answers

How to always cut the PID from `ps aux` command?

I'd like to get the pid from my processes. I do ps aux | cut -d ' ' -f 2 but I notice that sometimes it gets the pid and sometimes it does not: [user@ip ~]$ ps aux user 2049 0.5 10.4 6059216 1623520 ? Sl date 8:48 process user 12290 …
makansij
  • 9,303
  • 37
  • 105
  • 183
35
votes
5 answers

Get free disk space with df to just display free space in kb?

I'm trying to output the amount of free disk space on the filesystem /example. If I run the command df -k /example I can get good information about available disk space in kb but only by being human and actually looking at it. I need to take this…
Whoppa
  • 949
  • 3
  • 13
  • 23
30
votes
2 answers

bash script use cut command at variable and store result at another variable

I have a config.txt file with IP addresses as content like this 10.10.10.1:80 10.10.10.13:8080 10.10.10.11:443 10.10.10.12:80 I want to ping every ip address in that file #!/bin/bash file=config.txt for line in `cat $file` do ##this line is not…
CodingYourLife
  • 7,172
  • 5
  • 55
  • 69
29
votes
5 answers

How to cut the last field from a shell string

How to cut the last field in this shell string LINE="/string/to/cut.txt" So that the string would look like this LINE="/string/to/" Thanks in advance!
user558134
  • 1,071
  • 6
  • 25
  • 38
28
votes
3 answers

How do you pipe input through grep to another utility?

I am using 'tail -f' to follow a log file as it's updated; next I pipe the output of that to grep to show only the lines containing a search term ("org.springframework" in this case); finally I'd like to make is piping the output from grep to a…
les2
  • 14,093
  • 16
  • 59
  • 76
28
votes
4 answers

Extract The Second Word In A Line From A Text File

I am currently in need to extract the second word (CRISTOBAL) in a line in a text file. * CRISTOBAL AL042014 08/05/14 12 UTC * The second word in the line "CRISTOBAL" will vary from day to day so, I just need to find a way…
Aaron Perry
  • 1,021
  • 1
  • 13
  • 33