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
  • 4,287
  • 8
  • 50
  • 80

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
  • 275,237
  • 103
  • 548
  • 598
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
  • 172,303
  • 28
  • 230
  • 274
2

Using sed, run the following:

sed -r 's/.*\/([^\/]+)\.pjb/\1/g' logfile
David Rabinowitz
  • 29,904
  • 14
  • 93
  • 125
1

using cut and rev:

rev | cut -d'/' -f1 | cut -d'.' -f2 | rev
Farvardin
  • 5,336
  • 5
  • 33
  • 54
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
  • 683
  • 5
  • 20