Questions tagged [cut]

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

17
votes
2answers
58k 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 ...
2
votes
2answers
22 views

how to cut first 3 folders from this echo “/home/test/var/opt” ? i need the result “/home/test/var”

How to cut first 3 folders from a string? For example: echo "/home/source/Oracle/test" |cut -d'/' -f4 gives only one folder name, but I need up to the 3rd folder, like below: echo "/home/test/...
0
votes
2answers
18 views

Group items from column using awk

Trying to do the following, I have txt file which as the data of the interfaces. Eth1/15 desc01 1 Eth101/1/11 desc01 1 Eth101/1/16 desc01 1 Eth103/1/21 desc01 1 Eth1/2 ...
-1
votes
3answers
75 views

How to print first two fields in a file?

I have a file with country names and then the mobile country code prefix for that country like this: United Kingdom +44 ...more fields United States Virgin Islands +1 ...more fields I need to ...
1
vote
3answers
61 views

I want to know which word is duplicated on a line and how many times it appears in a file

example: I have file a.txt that contains the following: aunt bear bear true I also have a file b.txt that contains the following: tru tru high the end result I want is : for a.txt : bear x 2 for ...
207
votes
4answers
277k 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?
0
votes
1answer
41 views

What '\t' mean with 'tab' delimiter with 'cut' in BASH [duplicate]

in this command : cut -f2 -d$'\t' ('\t') what is it mean ? i saw it another question, i got the idea about 'tap' delimiter but still want to know '\t' shortcut or sign or command for something How ...
1
vote
3answers
1k views

Printing Characters after a Colon

How do I print anything after the colon? Input: color:white,name:green so I would like to print anything after : Output: white,green
5
votes
7answers
8k 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 ...
0
votes
2answers
31 views

bash - split by single character and match found items by string

tl;dr: Considering following input, which is some kind of a "list" of devices from nodes, I want to get the IP addresses, but only from devices named 'router.' (not router1., nor gw. or others): gw....
0
votes
1answer
2k views

wget: Retrieving a list of URLs when modifying input data file on the fly

This issue is currently driving me up the wall. It just does not work as it should. I have a file inp with audio samples to download where I preserved the internal ID number by parsing some other ...
30
votes
3answers
62k 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 ...
0
votes
2answers
44 views

Use grep command to read a file from specific line number to end of file

Suppose I have a file which is getting appended in every seconds/minutes. At first time I have read whole file and stored the last line number(eg: last_line_numer was : 2000) later I want to read file ...
0
votes
1answer
23 views

Extract some portion from line with regular expression

I have file which consist lines like : 50.57.61.8 - - [04/Apr/2019:09:52:01 -0400] "GET /tracker.php?q=1&p=2 HTTP/1.1" 302 20 "-" "Rackspace Monitoring/1.1 (https://monitoring.api.rackspacecloud....
2
votes
4answers
2k views

how to pipe the output of cut to the foreach command?

I have the cut command I want that grabs the first word in each line of a file. I then want to put each word from the cut command into a foreach. I then want to do a grep command inside the body of ...
0
votes
4answers
68 views

shell script - Remove character before a specific pattern

I've my /etc/hosts : #192.168.54.144 name1 #192.168.54.144 name2 192.168.54.143 name3 I would like to remove all character # which is followed by an ip address Ipv4 to obtain that : 192.168....
0
votes
0answers
21 views

Remove unwanted fields in source file itself [duplicate]

I have file like below with four fields separated by space.I want to keep only field1 in the input_file. With awk '{print $1}' input_file > output_file we can extract the required field and re-...
2
votes
1answer
77 views

Remove deduplicates in a very large file()

In this given input file, there are 4 columns. We have to remove the duplicates but there is a catch. There is a preference order C2>C3>C4. so in the output there is only one row with a, one row with ...
-1
votes
3answers
55 views

Swapping the contents of two columns

I am trying to understand cut better and moving cut data to another file. I have a file called numbers with two columns. Each column is separated with one tab. I am trying to swap the columns by ...
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@...
0
votes
2answers
37 views

Show line numbers of a file after “cut -d ”;“ -f3 |uniq -d”

I am trying to make a simple command that can show me the duplicate data from one specific column and also give me the original line number. Example of file: JENNIE;30;DOCTOR;F SARA;26;POLICE;F ...
0
votes
7answers
224 views

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.
0
votes
4answers
86 views

How can I delete everything but an alphanumeric pattern?

I have a file that has a lot of junk and special characters as well. I want to keep a particular alphanumeric pattern and ignore everything else - e.g AB123456789 - I want to extract only this keyword ...
0
votes
3answers
85 views

Remove last digit from string

I have a file test.txt, below the content: SCDE1 SF9 STR1D2 SREDF21 FRED STER2R4 I want to remove only the last character if it's a digit, so the output should be as below: SCDE SF STR1D SREDF2 ...
1
vote
4answers
101 views

Remove a specific portion of line

I have a csv file that has many lines of timestamps in following format HH:MM:SS:MS For example: 00.00.07.38 00.00.08.13 00.00.08.88 The hour is not relevant to me so I would like to cut it ...
2
votes
3answers
199 views

How to get single character after space?

How can I obtain below output? I want the first field as it is and a single character after space. echo "Hello world" Hellow If it also has a 3rd field than the beginning character of the 3rd field ...
0
votes
3answers
86 views

Regarding “<”, “<<”, and reading files [closed]

I know that I can do tr a b < file, but how do I do that for ls? So for example, How to feed ls -l a list of files from a 'file'. I tried the below and it did't work. $ cat>test A B C $ ls -l &...
65
votes
5answers
154k 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? ...
3
votes
4answers
200 views

How to grep and cut numbers from a file and sum them

I have a log file. For every line with a specific number, I want to sum the last number of those lines. To grep and cut is no problem but I don't know how to sum the numbers. I tried some solutions ...
0
votes
2answers
6k views

Bash to join columns from multiple files

I have around 20 files in different folders for which I have created a separate file called pathtofiles.in which lists path information: /home/users/gray_wolf/unix/File_1.tsv /home/users/gray_wolf/...
-1
votes
3answers
2k views

How to filter log file for string inside single quotes?

I have a log file, which outputs information this way: 2016-01-01: foo bar fnord 2016-01-01: this is static 'this is of interest' some blob bar 2016-01-01: this is static 'this is of interest' some ...
13
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 (...
-1
votes
2answers
2k views

Replace special characters in specific field

how to replace special character in specific field? INPUT xxxx,11/2019,xxx OUTPUT xxxx,11,2019,xxx As you see that am looking to replace / with , in $2 only. How achieve that?
0
votes
3answers
105 views

Print only the multiple first characters

I have a big text file I want to print only the first 4 and the first 5 and the first 8 characters of each line in one command line. For example I have the lines: 123456789ab ABCdefgih55 So the ...
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)....
0
votes
4answers
207 views

grep and cut text from unstructured file?

From below 3lines I need to grep and cut jar file name only. How to grep jar name from the line and cut from there. Downloading:https://repo.maven.apache.org/maven2/org/springframework/spring-aop/5....
0
votes
3answers
64 views

Get a certain column of an output with content aligned right and some columns not always filled

This is a problem I often encounter, this time with the output of lsof, but I am searching for a general solution for such problems: selecting a column. Here I try to get the TYPE column of the ...
0
votes
0answers
72 views

sed/awk/grep/cut: multiply a number in an xml-attribute by factor of ten

I would like to multiply the attribute in viewBox of svg-files by a factor of ten. I want to change <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" version="1.1"> to <svg xmlns=...
15
votes
6answers
13k 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.
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 | ...
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 ...
0
votes
2answers
35 views

Find number of cores available in a given node

I need to fix the following piece of code. The code aims to determine the number of cores available in a given node. Step 1: Get the number of cores with TC=$(grep -c process /proc/cpuinfo) Step 2:...
2
votes
3answers
466 views

find string and print first and last characters of line

I have files with hundreds of lines of varying length. I want to find each line with the string "New" and print the first 7 characters and the 10th from the last character. For example, cat file1....
0
votes
3answers
123 views

Script - Cutting various lines and sort them

i want to sort the process of one user by the percentage of cpu used. I tried this ps aux|tr -s " "|cut -f3 -d " "| sort -| grep "user" > file name does this work?
0
votes
2answers
42 views

Unix Split Function ~ select 2 different split words

I am using cut -d' ' -f1 to split a string in unix scripts. I want only f1 and f5, though, and I'm unable achieve this. I tried giving -f1-5, but that prints all the fields from 1st through 5th.
2
votes
1answer
94 views

Command modification

I'm using the following command to get the highest number of requests per second in a log file and it works well. grep "2017-02-22" "LogFile.log" | cut -c1-20 | uniq -c | sort -n | tail -n1 Now I ...
0
votes
1answer
74 views

grep pattern and the content after that, and remove others

I'm having a problem to grep specific keyword and its content. This is sample file, the actual file is larger than this. Example user@linux:~$ cat url.txt abcrandomtextdef another random text ...
1
vote
4answers
4k views

What does this command line (`echo $1 | xargs -n 1 basename | cut -d '.' -f1`) do? [closed]

I need to understand this command line: file=`echo $1 | xargs -n 1 basename | cut -d '.' -f1`
0
votes
1answer
58 views

Get string from filename to output file plus string inside same file

I have some directory with multiple files with the extention .failed This number of files can change every day. This files have the following format: file1_string2_1.failed: FHEAD|string2|...
1
vote
2answers
121 views

How to transpose x and y axis of table, using cut & paste shell commands

I need to transpose x and y axis, of a file of 450.000 × 15.000 tab separated fields, so I tried first with a small 5 × 4 test file named A.txt: x column1 column2 column3 row1 0 1 2 row2 ...