Solved

pulling parts of a string off with awk

Posted on 2013-01-14
4
338 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
 
LVL 16

Expert Comment

by:choward16980
ID: 38776677
0
 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

751 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