Solved

pulling parts of a string off with awk

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Choosing CentOS 16 82
Delete a folder on a linux computer on a regular basis 10 37
Run Secure WMI query from CentOS 5 33
PHP error function not working on AWS 10 8
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
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 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…
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.

803 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