Solved

pulling parts of a string off with awk

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

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 …
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

930 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

13 Experts available now in Live!

Get 1:1 Help Now