The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl: Search for string on line then search and replace text Crypto Shell Programming and Scripting 4 01-04-2008 10:24 AM
read file backwards bluemoon1 Shell Programming and Scripting 3 09-28-2007 01:32 AM
Scanning file backwards tekster757 UNIX for Advanced & Expert Users 2 09-21-2007 05:07 PM
ML level went backwards? pschlesinger AIX 1 08-09-2006 05:12 PM
Translate date value to normal date and backwards. Frederik UNIX for Advanced & Expert Users 4 12-10-2003 03:44 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-06-2008
giri_luck giri_luck is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 23
Search backwards

Hi,

I have a variable , lets say
a=/disk1/net/first.ksh

i need to grep "first.ksh"

everytime "a" gets changed dynamically and i do not know how many '"/" are there in my variable.

Can somebody help me out.
  #2 (permalink)  
Old 10-06-2008
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,262
in awk
Code:
echo $a| awk -F/ '{print $NF}'
  #3 (permalink)  
Old 10-06-2008
giri_luck giri_luck is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 23
great it works....

Can you please explain me the command
  #4 (permalink)  
Old 10-06-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Or simply use basename -- or if your shell understands ${a##*/} then by all means use that.
  #5 (permalink)  
Old 10-06-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,884
explanation

The awk command splits its input into the character specified by the -F option (in this case "/"). So awk sees something like:
disk1 net first.ksh
Awk has an internal variable named "NF" which stands for the Number of Fields on the current line. In this case that's 3. So it's like writing:
awk '{ print $3 '}
which would print the third field of the input. Of course, it's not really 3 every time, just this time. So the last part of the path gets printed.
  #6 (permalink)  
Old 10-07-2008
giri_luck giri_luck is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 23
Thanks a lot....

is that i can take

/disk/net in one variable and first.ksh in one variable ?
  #7 (permalink)  
Old 10-07-2008
vijay_0209 vijay_0209 is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 52
basename $a
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:46 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0