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 ...
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 ...
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 ...
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
52
votes
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 ...
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, ...
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/...
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.
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 ...
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 "$...
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" ) }' <<< ...
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....
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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
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 ...
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 ...
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/...
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'[.][.]' .....
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 ...
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 (...
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 ...
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 ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
cut × 438awk × 148
sed × 111
text-processing × 108
grep × 95
bash × 67
shell-script × 54
linux × 47
shell × 36
command-line × 22
sort × 17
string × 13
columns × 13
scripting × 11
uniq × 11
csv × 10
paste × 10
files × 9
find × 9
regular-expression × 9
text-formatting × 9
echo × 9
pipe × 8
tail × 8
perl × 7