Hot answers tagged

262 votes
Accepted

How do I use cut to separate by multiple whitespace?

If we use tr command along with squeeze option (-s flag ) to convert all multiple consecutive spaces to a single space and then perform cut operation with space as delimiter – we can access the ...
Wald Schilfrohr's user avatar
108 votes
Accepted

Counting occurrences of word in text file

Given such a requirement, I would use a GNU grep (for the -o option), then pass it through wc to count the total number of occurrences: $ grep -o -i iphone Tweet_Data | wc -l 3 Plain grep -c on the ...
Jeff Schaller's user avatar
  • 66.3k
75 votes
Accepted

How to get last part of http link in Bash?

Using awk for this would work, but it's kind of deer hunting with a howitzer. If you already have your URL bare, it's pretty simple to do what you want if you put it into a shell variable and use ...
DopeGhoti's user avatar
  • 74.1k
53 votes
Accepted

cut string on last delimiter

#For Filename echo "a.b.c.txt" | rev | cut -d"." -f2- | rev #For extension echo "a.b.c.txt" | rev | cut -d"." -f1 | rev
jijinp's user avatar
  • 1,371
52 votes

Getting first n characters from a file

Complete command would be: head -c 20 yourFile.txt
Philip Kirkbride's user avatar
36 votes

Is piping, shifting, or parameter expansion more efficient?

First rule of software optimization: Don't. Until you know the speed of the program is an issue, there's no need to think about how fast it is. If your list is about that length or just ~100-1000 ...
ilkkachu's user avatar
  • 134k
32 votes

cut column 2 from text file

It is easiest with awk which treats multiple consecutive spaces as a single one, so awk '{print $2}' file prints 207 412 431 But obviously there are many, many other tools which will do the job, ...
jimmij's user avatar
  • 46.2k
31 votes

How to get last part of http link in Bash?

basename and dirname work good for URLs too: > url="http://www.test.com/abc/def/efg/file.jar" > basename "$url"; basename -s .jar "$url"; dirname "$url" file.jar file http://www.test.com/abc/...
Fedor Dikarev's user avatar
28 votes
Accepted

How to unbuffer cut?

If you're on a system using GNU Coreutils (almost any Linux), you can try stdbuf: … | stdbuf -oL cut -d '@' -f 1 | … -oL makes it line buffered, which seems like what you want.
derobert's user avatar
  • 108k
25 votes
Accepted

cut command seems not to work in a pipe

Every space counts towards the field number, even leading and consecutive ones. Hence, you need to use -f9 instead of -f2. Alternatively, you can use awk '{ print $2 }' in place of the cut command ...
Joseph Sible-Reinstate Monica's user avatar
18 votes

cut string on last delimiter

There are many tools to do this. As you were using cut : $ string1="$(cut -d. -f1-3 <<<'a.b.c.txt')" $ string2="$(cut -d. -f4 <<<'a.b.c.txt')" $ echo "$string1" a.b.c $ echo "$...
heemayl's user avatar
  • 55k
18 votes
Accepted

Is piping, shifting, or parameter expansion more efficient?

Pretty simple with awk. This will get you the value of every fourth field for input of any length: $ awk -F' ' '{for( i=1;i<=NF;i+=3) { printf( "%s%s", $i, OFS ) }; printf( "\n" ) }' <<< ...
DopeGhoti's user avatar
  • 74.1k
18 votes

How to delete the first word in each line of a file?

To do it using cut cut -f 2- -d ' ' file.txt > new_file.txt "Give me the second and any other field beyond, using space as a delimiter, from the file.txt file and direct the output to new_file....
ivanivan's user avatar
  • 4,890
18 votes

How to create numeronyms in bash

Although I find the use of awk with an empty field separator somewhat 'innovative', the simplest solution in my opinion is just a small expansion of yours: awk -F "" '{print $1 (NF-2) $NF}' This ...
AdminBee's user avatar
  • 21.8k
17 votes

cut column 2 from text file

Use pipes to squeeze the extra whitespaces and send your data (e.g, in columns.txt) into cut: tr -s ' ' < columns.txt | cut -d" " -f2 In the example data you provided, a single space ...
aeiounix's user avatar
  • 394
17 votes

"Cutting" id output

Don't bother trying to parse it. POSIX requires that id support various options to do this automatically: printf "Login: %s\nId: %s\nGroup: %s\n" "$(id -un)" "$(id -u)" "$(id -gn)" In addition to ...
Kevin's user avatar
  • 756
17 votes
Accepted

How do I remove the last characters from a string?

To remove everything from after the last _ in var and assign the result to var2: var2=${var%_*} The parameter expansion ${parameter%word} removes the pattern word (_* in this case) from the end of ...
Kusalananda's user avatar
  • 323k
17 votes
Accepted

"No such file or directory" when using "-exec" with find

You write because it starts with a ./ it will just be executed ("No such file or directory"). This isn't what's happening. You have provided a single command to the find ... -exec parameter of ...
roaima's user avatar
  • 108k
17 votes
Accepted

Print last N characters from all lines in a file using cut

cut by itself doesn't have the concept of "the last N characters" on a line. However if you combine this with the rev program you can reverse each line, select the first N characters, and then reverse ...
icarus's user avatar
  • 17.5k
16 votes

grep first n and last n characters from a line in a file

You can use cut as: Command: cut --complement -c17-43 file1.txt Output: Mar 23 08:20:23 : 235 Mar 23 08:21:45 : 127 Mar 23 08:22:34 : 875 Mar 23 08:25:46 : 322 Mar 23 08:26:12 : 639
Stephen Rauch's user avatar
16 votes
Accepted

How to use Unix Shell to show only the first n columns and last n columns?

awk -F, '{print $1, $2, $(NF-1), $NF}' < input More generally (per the Question's title), to print the first and last n columns of the input -- without checking to see whether that means printing ...
Jeff Schaller's user avatar
  • 66.3k
15 votes

How to create numeronyms in bash

In ksh93, bash or zsh: numeronym() { (( ${#1} > 2 )) || return printf '%s%d%s\n' "${1:0:1}" "$(( ${#1} - 2 ))" "${1: -1:1}" } This works on the first (only) parameter by printing the first ...
Jeff Schaller's user avatar
  • 66.3k
14 votes

How to get last part of http link in Bash?

With awk, you can use $NF, to get the last field, regardless of number of fields: awk -F / '{print $NF}' If you store that string in shell variable, you can use: a=http://www.test.com/abc/def/efg/...
cuonglm's user avatar
  • 151k
14 votes

cut with 2 character delimiter

awk's field separator is treated as a regexp as long as it's more than two characters. .. as a regexp, means any 2 characters. You'd need to escape that . either with [.] or with \.. awk -F'[.][.]' .....
Stéphane Chazelas's user avatar
14 votes
Accepted

Using tab delimiter in Cut in Unix Shell Scripting

cut -f 1 input.txt This gives you the first column from the tab-delimited file input.txt. The default field delimiter for cut is the tab character, so there's no need to further specify this. If ...
Kusalananda's user avatar
  • 323k
14 votes

What does cut return if the specified field does not exist?

cut has an odd API. cut -f n will output the nth field of each line that has at least one delimiter (empty if the line has fewer than n-1 delimiters (fewer than n fields)), and returns the full line (...
Stéphane Chazelas's user avatar
14 votes

Is piping, shifting, or parameter expansion more efficient?

I'm only going to give some general advice in this answer, and not benchmarks. Benchmarks are the only way to reliably answer questions about performance. But since you don't say how much data you're ...
Gilles 'SO- stop being evil''s user avatar
13 votes

Can not use `cut -c` (`--characters`) with UTF-8?

colrm (part of util-linux, should be already installed on most distributions) seems to handle internationalization much better : $ echo 'αβγ' | colrm 3 αβ $ echo 'αβγ' | colrm 2 α Beware of the ...
Skippy le Grand Gourou's user avatar
13 votes
Accepted

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

You can take help from paste to serialize the numbers in a format suitable for bc to do the addition: % grep "30201" logfile.txt | cut -f6 -d "|" 650 1389 945 % grep "30201" logfile.txt | cut -f6 -d ...
heemayl's user avatar
  • 55k
13 votes

Grep and Cut command in linux

Although you state awk is not a possibility - for the sake of completeness: awk -F',' '$9>=1' input.csv This will instruct awk to consider , as field separator and print only lines where field 9 ...
AdminBee's user avatar
  • 21.8k

Only top scored, non community-wiki answers of a minimum length are eligible