1

The log output lines look like follows

HLGAPL65.HOU150.CHEVRONTEXACO.NET/UPSTREAM_MDM_D2/Jobs/Keystone Release 2.0.2.0/0.0. Loading_SOR_to_Landing/EGI/EGI_WV_WELLHDR.pjb

I need to extract the name that appears right after the last "/" and before the ".pjb"

In this particular case - the needed name is EGI_WV_WELLHDR.

What is the most efficient and simple way to do that?

Denys
  • 3,927
  • 6
  • 47
  • 76

5 Answers5

2

You can use this awk:

$ awk -F"[/.]" '{print $(NF-1)}' file
EGI_WV_WELLHDR

Explanation

  • -F"[/.]" sets delimiter as dot or slash.
  • {print $(NF-1)} prints the penultimate field based on those field separators.

If what you want is the filename without extension, then you can do the following:

See:

$ awk '{print $NF}' file
Loading_SOR_to_Landing/EGI/EGI_WV_WELLHDR.pjb
$ t=$(basename $(awk '{print $NF}' a))
$ echo "$t"
EGI_WV_WELLHDR.pjb
$ echo ${t%.*}
EGI_WV_WELLHDR
Community
  • 1
  • 1
fedorqui
  • 252,262
  • 96
  • 511
  • 570
2

Try this sed command,

$ sed -r 's/^.*\/([^.]*)\.pjb$/\1/g' file
EGI_WV_WELLHDR

-r --> Extended regex.

^.*\/([^.]*)\.pjb$

The above regex fetches the characters that are inbetween the last / and .pjb. Then the fetched characters in group are printed through backreference.

Avinash Raj
  • 166,785
  • 24
  • 204
  • 249
2

Using sed, run the following:

sed -r 's/.*\/([^\/]+)\.pjb/\1/g' logfile
David Rabinowitz
  • 28,996
  • 14
  • 92
  • 124
1

using cut and rev:

rev | cut -d'/' -f1 | cut -d'.' -f2 | rev
Taher Khorshidi
  • 5,168
  • 5
  • 31
  • 53
0

As your separator is a slash, you can use basename :

$ basename "HLGAPL65.HOU150.CHEVRONTEXACO.NET/UPSTREAM_MDM_D2/Jobs/Keystone Release 2.0.2.0/0.0. Loading_SOR_to_Landing/EGI/EGI_WV_WELLHDR.pjb" .pjb
EGI_WV_WELLHDR
Mickaël Bucas
  • 651
  • 7
  • 19