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.

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

Can't redirect cut output

When I try to redirect the output of cut it always seems to be empty. If don't redirect it, the output shows in terminal as expected. This is true for OS X 10.10 and Linux 4.1.6. This works: root@...
198
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?
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?
34
votes
6answers
124k 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 ...
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 ...
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 ...
1
vote
2answers
2k views

cut out specific fields from text file [duplicate]

I have a text file with 9 fields separated by : survey:m1111771::rent:travel:::Morning: How do I pull field 1 and 8 only to a separate file for example it would look like this, survey:Morning
0
votes
0answers
57 views

Split Logs into different files - BASH/LINUX [duplicate]

I want to do the following for my official assignment. I have a user.log file with mammoth amount of logs, I need to cut down that file into small files. Requirements are as follows: Main User.log ...
0
votes
1answer
2k views

How to use grep and cut together?

Lets say I'm doing a database for fruits. Using a text file for it, inside contains, in this format, (Fruit:Quantity:Cost) Apple:10:1 Orange:20:2 Pears:10:3 Banana:20:4 10 Apples at $1 each. 20 ...
61
votes
5answers
148k 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? ...
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 ...
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 ...
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 (...
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 '<'...
3
votes
4answers
3k views

Using bash variable substitution instead of cut/awk

Can I use bash variable substitution to extract a piece of a variable based on a delimeter? I'm trying to get the immediate directory name of a filename (in this case, foo). $ filename=./foo/bar/baz....
3
votes
2answers
571 views

How to split a file by counting digit numbers within a row?

I have a file with 45000 character within each line and I I want to split the original file based on specific numbers of character within a line. as an small example my input file looks like: input....
2
votes
0answers
181 views

use gff2fasta instead of a bash script to get parts of DNA sequences out of a full genome

EDIT and a solution Because my original question was badly phrased and I was trying to re-invent the wheel I am answering my own question now (maybe it helps someone else): gff2fasta is a tool which ...
3
votes
1answer
3k views

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

Using cut/awk/sed with two different delimiters

I have the following cases: case@test.com case_1_2@test.com case_1@test.com I'm trying to convert these to case@test.com case@test.com case@test.com So it should remove everything from the ...
1
vote
1answer
92 views

Linux log file structure change for usage with kst

My file (tcp dump from networked sensors) has this shape: 20:06:57.049686 IP (tos 0x0, ttl 64, id 26871, offset 0, flags [DF], proto UDP (17), length 68) e108-193.eduroam.tugraz.ac.at.34225 > ...
1
vote
3answers
99 views

Return the first few columns, omit everything after it until a delimiter, and add the rest?

The input is a file (text.txt) containing the following lines of text (all whitespaces are space characters): 2016-10-24 10:25:48.939279-0400 0x63a55 Info 0x0 1416 ...
0
votes
2answers
331 views

Ambiguous redirect for stdin to while loop [closed]

I would like to run a command for each entry in a column of a file. I am currently doing the following, but am getting an ambiguous redirect error. while read -r entry; do cmd "$entry" ; done < $...
0
votes
2answers
884 views

How to filter range of date from a particular column of a csv file?

Consider the input file 1,10/22/2017,Scheduled 2,10/23/2017,Confimred 1,10/24/2017,NA 1,10/29/2017,Scheduled 3,11/1/2017,Scheduled 1,11/2/2017,Scheduled How do I filter date(within range) in 2nd ...
-1
votes
3answers
100 views

Listing a Summary with limits

I am trying to list a number of offending IP's with a one line command, and am not sure how to do the last little bit, maybe someone can point me in the right direction. cat /var/log/syslog* | grep "...