Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [cut]

cut is a filter program which extracts fields or columns of each line of input.

199
votes
4answers
268k views

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?
62
votes
5answers
149k views

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? ...
54
votes
7answers
217k views

Split string by delimiter and get N-th element

I have a string: one_two_three_four_five I need to save in a variable A value two and in variable B value fourfrom the above string
34
votes
6answers
125k views

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 ...
29
votes
10answers
145k views

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 ...
29
votes
2answers
61k views

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 ...
27
votes
3answers
37k views

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: ...
26
votes
1answer
30k views

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?
24
votes
8answers
2k views

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)....
19
votes
4answers
2k views

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 ...
18
votes
5answers
12k views

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 ...
18
votes
4answers
25k views

How do I use cut to separate by multiple whitespace?

I would like to get the last column 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 KBytes 4.19 Mbits/sec [...
16
votes
7answers
63k views

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="http://examplewebsite.com/"> Other websites have .net, .gov, but I assume I could make the cut ...
16
votes
6answers
17k views

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 ...
16
votes
2answers
2k views

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 ...
15
votes
6answers
12k views

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.
14
votes
7answers
16k views

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 ...
12
votes
3answers
3k views

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 (...
10
votes
1answer
44k views

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 ...
10
votes
3answers
61k views

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 ...
10
votes
4answers
31k views

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 ...
9
votes
6answers
307 views

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 ...
9
votes
3answers
7k views

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 ...
8
votes
1answer
2k views

How to unbuffer cut?

I want to get just e-mail addresses that end in "@xyz.nl" from my mail logfile. To achieve this I do: # tail -f /var/log/mail.log | grep --i --line-buffered "@xyz.nl" | cut -d '@' -f 1 | cut -d '<'...
7
votes
7answers
1k views

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 ...
6
votes
4answers
878 views

“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 ...
6
votes
5answers
10k views

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 ...
6
votes
3answers
10k views

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 ...
6
votes
2answers
7k views

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 ...
6
votes
4answers
5k views

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 ...
6
votes
5answers
9k views

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

Say I had a config file /etc/emails.conf email1 = user@dinkum.dorg email2 = user@winkum.worg email3 = user@stinkum.storg and I wanted to get email2 I could do a: grep email2 /etc/emails.conf | ...
6
votes
3answers
336 views

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 ...
5
votes
7answers
7k views

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 ...
5
votes
3answers
2k views

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&...
5
votes
4answers
312 views

Excluding enclosed delimiters with cut

Let's say I have a CSV file: "col1","col2","col3" "col4","col5,subtext","col6 The problem I have is as follows : cut -d, -f1,2 test.txt "coll1","col2" "col4","col5 The desired output is : "col1","...
5
votes
4answers
9k views

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 ...
5
votes
2answers
7k views

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 ...
5
votes
4answers
940 views

how to split a file into separate files based on the column headers in original file?

I would like to split a file to different files based on the information in the first line. For example, I have: Input: 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 4 30 30 30 30 0 2 2 0 2 ...
5
votes
4answers
4k views

How to show only the last k columns in ls -l?

I like to restrict the columns shown in ls -l command, by eliminating the first 4 columns. ls -lh shows: drwxr-sr-x 20 gamma alpha 4.0K May 22 13:18 Desktop drwxr-sr-x 3 gamma alpha 22 Oct 6 ...
5
votes
5answers
1k views

A shell script to write selected fields to a single line

I have a log file trace.log which prints the time stamp, thread name and Transaction method and transaction ID as below. 2014-01-23 15:50:41,724 [catalina-exec-35] INFO TRANSACTION getConnection ...
5
votes
1answer
5k views

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 ...
5
votes
3answers
25k views

Search for values in 2 columns from CSV

I have a csv files with say 20 odd columns. I need to get the 14th and 15th column values where value in 14th column is say "VALUE1" and value in 15th column is "VALUE2". My condition gets satisfied ...
5
votes
2answers
1k views

ls for cut -f string not working

I have a problem with feeding cut output to ls. This doesn't work: rep="S1_1000,0000-00-00c,0000-00-00d,0000-00-00e" ls tab_yeast/{`echo $rep | cut -f2- -d','`}*.tab.gz ls: cannot access ...
4
votes
5answers
3k views

How to cut a date/timestamp String?

So I am using TIMESTAMP=$( stat -c "%y" $JARNAME.jar ) print $TIMESTAMP and its output is something like 2015-04-24 17:23:03.000000000 -0500 I need only the 2015-04-24 17:23:03 part
4
votes
3answers
1k views

How can I remove first name from surname when the length is unknown?

I'm trying to write a simple bash script in which the user inputs their username, then they are greeted, depending on the time of day by their surname. I currently have the following: echo Please ...
4
votes
3answers
1k views

how to replace the first element in a CSV with a sequential number?

I have a csv file like this: 5/05/2017;03;07;30;35;43;01;03 9/05/2017;08;12;16;22;26;06;07 12/05/2017;02;20;28;29;44;03;09 16/05/2017;08;11;15;20;30;03;08 19/05/2017;09;11;12;19;30;04;09 23/05/2017;...
4
votes
2answers
2k views

“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 ...
4
votes
2answers
173 views

Getting the last lines that matches a pattern in multiple files

I have an application that outputs a set of log files to a central directory like this: /tmp/experiment/log/ ├── node01.log ├── node02.log ├── node03.log ├── node04.log ├── node05.log ├── node06....
4
votes
1answer
180 views

cut command fields

I noticed these two different fields behaviors using cut command: bash:~$ var=`cat /proc/cpuinfo | grep 'model name' | uniq | cut -d ' ' -f 3,4,5,6,7,8 ` echo $var outputs Intel(R) Core(TM) i7-...
4
votes
2answers
5k views

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 ...