Cut all but last field in strings


Sometimes, all you want is the last field for a bunch of strings. In the following, we will be taking a list of file names and grabbing the base name from it. basename is fine, but you must know the extension to remove. By using cut and rev, you can get the same thing. The most basic form of this is:

list of strings | rev | cut -d'delimiter' -f 2- | rev

The following example searches a directory tree using find to locate all files under it. This also gives us the opportunity to also remove the path.

The directory /media/multimedia/Movies contains movie files, but they may be mkv, m4v, avi, whatever. There is also a meta file associated with each file that has the same name as the file but with a .nfo extension. Since I assume nfo is not readily found in a file name, I chose to simply filter out anything with nfo in it, though that may not work in your situation (so, you would want to change that part here)

find /media/multimedia/Movies/ -type f | grep -v nfo | rev | cut -d'.' -f2- | cut -d'/' -f1 | rev

With basename, you would need to know what extension to remove, but we can use cut to grab only what we need. Unfortunately, cut will allow you to grab the first field, or the second and all subsequent fields, but it will not let you grab "everything but the last field", so we use rev where we need to.

Each of the commands is shown and described below, with examples of the output at that point (ie, the previous commands are assumed).

1. find /media/multimedia/Movies/ -type f # grab all files under the Movies subdirectory

/media/multimedia/Movies/2001 A Space Odyssey.mkv
/media/multimedia/Movies/2001 A Space Odyssey.nfo
/media/multimedia/Movies/2012.mkv
/media/multimedia/Movies/2012.nfo
/media/multimedia/Movies/3 Days of the Condor.m4v
/media/multimedia/Movies/3 Days of the Condor.nfo
/media/multimedia/Movies/47 Ronin.m4v
/media/multimedia/Movies/47 Ronin.nfo
/media/multimedia/Movies/Absolute Power.m4v
/media/multimedia/Movies/Absolute Power.nfo
/media/multimedia/Movies/Abyss.mkv
/media/multimedia/Movies/Abyss.nfo
/media/multimedia/Movies/Aeon flux.mkv
/media/multimedia/Movies/Aeon flux.nfo
/media/multimedia/Movies/A.I. Artificial Intelligence.mkv
/media/multimedia/Movies/A.I. Artificial Intelligence.nfo

2. grep -v nfo # grab everything EXCEPT .nfo files which contain metadata

/media/multimedia/Movies/2001 A Space Odyssey.mkv
/media/multimedia/Movies/2012.mkv
/media/multimedia/Movies/3 Days of the Condor.m4v
/media/multimedia/Movies/47 Ronin.m4v
/media/multimedia/Movies/Absolute Power.m4v
/media/multimedia/Movies/Abyss.mkv
/media/multimedia/Movies/Aeon flux.mkv
/media/multimedia/Movies/A.I. Artificial Intelligence.mkv

3. rev # reverse each string

vkm.yessydO ecapS A 1002/seivoM/aidemitlum/aidem/
vkm.2102/seivoM/aidemitlum/aidem/
v4m.rodnoC eht fo syaD 3/seivoM/aidemitlum/aidem/
v4m.ninoR 74/seivoM/aidemitlum/aidem/
v4m.rewoP etulosbA/seivoM/aidemitlum/aidem/
vkm.ssybA/seivoM/aidemitlum/aidem/
vkm.xulf noeA/seivoM/aidemitlum/aidem/
vkm.ecnegilletnI laicifitrA .I.A/seivoM/aidemitlum/aidem/

4. cut -d'.' -f2- # remove the first field (extension) using periods as the delimiter

yessydO ecapS A 1002/seivoM/aidemitlum/aidem/
2102/seivoM/aidemitlum/aidem/
rodnoC eht fo syaD 3/seivoM/aidemitlum/aidem/
ninoR 74/seivoM/aidemitlum/aidem/
rewoP etulosbA/seivoM/aidemitlum/aidem/
ssybA/seivoM/aidemitlum/aidem/
xulf noeA/seivoM/aidemitlum/aidem/
ecnegilletnI laicifitrA .I.A/seivoM/aidemitlum/aidem/

5. cut -d'/' -f1 # keep only the first field, ie the filename, using / as the delimiter

yessydO ecapS A 1002
2102
rodnoC eht fo syaD 3
ninoR 74
rewoP etulosbA
ssybA
xulf noeA
ecnegilletnI laicifitrA .I.A

6. rev # put the string back in the correct order.

2001 A Space Odyssey
2012
3 Days of the Condor
47 Ronin
Absolute Power
Abyss
Aeon flux
A.I. Artificial Intelligence

Note: There are more than one way to skin a cat. The following would work also, getting rid of the duplicates with the meta data file and the actual file (it removes all dups). However, I like to get rid of duplicates as early as possible, so I do like the above better. However, if you are wanting to check on whether you have duplicate file names also (in separate directories), adding the -c option to uniq will let you find that.

find /media/multimedia/Movies/ -type f | grep -v nfo | rev | cut -d'.' -f2- | cut -d'/' -f1 | rev
Last update:
2014-09-25 02:47
Author:
Rod
Revision:
1.0
Average rating:0 (0 Votes)

You can comment this FAQ

Chuck Norris has counted to infinity. Twice.