Get last field using awk substr

UnixAwkSubstr

Unix Problem Overview


I am trying to use awk to get the name of a file given the absolute path to the file.
For example, when given the input path /home/parent/child/filename I would like to get filename I have tried:

awk -F "/" '{print $5}' input

which works perfectly.
However, I am hard coding $5 which would be incorrect if my input has the following structure:

/home/parent/child1/child2/filename

So a generic solution requires always taking the last field (which will be the filename).

Is there a simple way to do this with the awk substr function?

Unix Solutions


Solution 1 - Unix

Use the fact that awk splits the lines in fields based on a field separator, that you can define. Hence, defining the field separator to / you can say:

awk -F "/" '{print $NF}' input

as NF refers to the number of fields of the current record, printing $NF means printing the last one.

So given a file like this:

/home/parent/child1/child2/child3/filename
/home/parent/child1/child2/filename
/home/parent/child1/filename

This would be the output:

$ awk -F"/" '{print $NF}' file
filename
filename
filename

Solution 2 - Unix

In this case it is better to use basename instead of awk:

 $ basename /home/parent/child1/child2/filename
 filename

Solution 3 - Unix

If you're open to a Perl solution, here one similar to fedorqui's awk solution:

perl -F/ -lane 'print $F[-1]' input

-F/ specifies / as the field separator
$F[-1] is the last element in the @F autosplit array

Solution 4 - Unix

Another option is to use bash parameter substitution.

$ foo="/home/parent/child/filename"
$ echo ${foo##*/}
filename
$ foo="/home/parent/child/child2/filename"
$ echo ${foo##*/}
filename

Solution 5 - Unix

It should be a comment to the basename answer but I haven't enough point.

If you do not use double quotes, basename will not work with path where there is space character:

$ basename /home/foo/bar foo/bar.png
bar

ok with quotes " "

$ basename "/home/foo/bar foo/bar.png"
bar.png

file example

$ cat a
/home/parent/child 1/child 2/child 3/filename1
/home/parent/child 1/child2/filename2
/home/parent/child1/filename3

$ while read b ; do basename "$b" ; done < a
filename1
filename2
filename3

Solution 6 - Unix

I know I'm like 3 years late on this but.... you should consider parameter expansion, it's built-in and faster.

if your input is in a var, let's say, $var1, just do ${var1##*/}. Look below

$ var1='/home/parent/child1/filename'
$ echo ${var1##*/}
filename
$ var1='/home/parent/child1/child2/filename'
$ echo ${var1##*/}
filename
$ var1='/home/parent/child1/child2/child3/filename'
$ echo ${var1##*/}
filename

Solution 7 - Unix

Like 5 years late, I know, thanks for all the proposals, I used to do this the following way:

$ echo /home/parent/child1/child2/filename | rev | cut -d '/' -f1 | rev
filename

Glad to notice there are better manners

Solution 8 - Unix

you can skip all of that complex regex :

 echo '/home/parent/child1/child2/filename' | 

 mawk '$!_=$-_=$NF' FS='[/]'
                                  filename

2nd to last :

 mawk '$!--NF=$NF' FS='/'
                           child2

3rd last field :

 echo '/home/parent/child1/child2/filename' | 

 mawk '$!--NF=$--NF' FS='[/]'
 
                    child1

4th-last :

 mawk '$!--NF=$(--NF-!-FS)' FS='/'

 echo '/home/parent/child000/child00/child0/child1/child2/filename' |
                                     child0
 echo '/home/parent/child1/child2/filename'
             parent

major caveat :

- `gawk/nawk` has a slight discrepancy with `mawk` regarding 

   - how it tracks multiple,
   - and potentially conflicting, decrements to `NF`, 

   - so other than the 1st solution regarding last field, 
   - the rest for now, are only applicable to `mawk-1/2`

Solution 9 - Unix

You can also use:

    sed -n 's/.*\/\([^\/]\{1,\}\)$/\1/p'

or

    sed -n 's/.*\/\([^\/]*\)$/\1/p'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMariView Question on Stackoverflow
Solution 1 - UnixfedorquiView Answer on Stackoverflow
Solution 2 - UnixpiokucView Answer on Stackoverflow
Solution 3 - UnixChris KoknatView Answer on Stackoverflow
Solution 4 - UnixdevnullView Answer on Stackoverflow
Solution 5 - UnixPierre-DamienView Answer on Stackoverflow
Solution 6 - UnixAdriano_PinaffoView Answer on Stackoverflow
Solution 7 - UnixNicko GlayreView Answer on Stackoverflow
Solution 8 - UnixRARE Kpop ManifestoView Answer on Stackoverflow
Solution 9 - UnixSARATHView Answer on Stackoverflow