Solved

pulling parts of a string off with awk

Posted on 2013-01-14
4
341 Views
Last Modified: 2013-01-21
$File="/etc/lvm/lvm.conf"

using awk, I would like to pull off the file name regardless of what directory it is in (/etc/home/lvm/lvm.conf   /etc/lvm.conf etc )

I am using redhat however I know I will need the same solution for aix, solaris and redhat :)
0
Comment
Question by:TIMFOX123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 

Author Comment

by:TIMFOX123
ID: 38776784
thanks for trying, that is not what I was looking for
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 38777314
Does it have to be awk?

The easiest solution (shell only, without awk) is this:

File="/etc/lvm/lvm.conf"
Filename=${File##*/}
echo $Filename

The above requires bash or ksh.

Another easy one without awk:

File="/etc/lvm/lvm.conf"
Filename=$(basename $File)
echo $Filename

But awk is also possible (of course):

File="/etc/lvm/lvm.conf"
Filename=$(echo $File | awk -F/ '{print $NF}')
echo $Filename

Btw., do you like "sed"?

File="/etc/lvm/lvm.conf"
Filename=$(echo $File|sed 's/^.*\///')
echo $Filename

All solutions work on AIX, Linux and Solaris.

With older or exotic shells it might be necessary using backticks ( `   ` ) instead of "$(   )".

Those backticks also work on any newer shell, for that matter.


Please note, it's "File=...", not "$File=...". Or is that a shell prompt in your Q?   ;-)
0
 
LVL 19

Expert Comment

by:jools
ID: 38780517
have a look at `basename` It's definately on linux, not sure about AIX etc, been a while since I connected to one of those.
0

Featured Post

 Watch the Recording: Learning MySQL 5.7

MySQL 5.7 has a lot of new features. If you've dabbled with an older version of MySQL, it is definitely worth learning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question