Solved

pulling parts of a string off with awk

Posted on 2013-01-14
4
327 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
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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 …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now