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
Minicom runscript: How to send escape character ( \ ) rkhanna Shell Programming and Scripting 1 10-02-2008 10:14 AM
read a variable character by character, substitute characters with something else vipervenom25 UNIX for Dummies Questions & Answers 2 06-06-2008 03:18 PM
Escape character deepakpv Shell Programming and Scripting 4 02-16-2007 03:19 AM
Escape character in vi? stevefox Shell Programming and Scripting 4 11-17-2005 03:38 PM
possible to escape the \ character in sed? gammaman UNIX for Dummies Questions & Answers 1 07-07-2005 02:49 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-29-2006
OFFSIHR OFFSIHR is offline
Registered User
  
 

Join Date: Oct 2006
Posts: 23
awk / escape character

Hi
I'm trying to split a dir listing
eg
/home/foo1/foo2
I'm using ksh

I've tried
dir=/home/foo1/foo2
splitit=`echo $dir | awk -F '\/' '{print $1}'`
echo $splitit

nothing is output!
I have checked the escape character. The only one I have found is \

BTW `pwd` | awk -F \/ '{print $1}' works from cmdline


Why is this not working?

I want to split the string and then put it into an array, get the size of it and then get the right most directory

Last edited by OFFSIHR; 11-29-2006 at 12:08 PM..
  #2 (permalink)  
Old 11-29-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
no need to 'escape'.
Given a sample input, if you need to echo 'foo2':
Code:
dir=/home/foo1/foo2
splitit=`echo $dir | awk -F '/' '{print $NF}'`
echo $splitit
the same could be accomplished easier using 'basename' - 'man basename'
  #3 (permalink)  
Old 11-29-2006
OFFSIHR OFFSIHR is offline
Registered User
  
 

Join Date: Oct 2006
Posts: 23
thank you for reply vgersh99 I am on Linux but '/' returns blank in inline awk

I look at basename, but is it only for directory name that you know already as argument passed?

thanks
  #4 (permalink)  
Old 11-29-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Quote:
Originally Posted by OFFSIHR
thank you for reply vgersh99 I am on Linux but '/' returns blank in inline awk

I look at basename, but is it only for directory name that you know already as argument passed?

thanks
I'm not following what's being said here - could you provide a sample input AND a desired output, pls?
  #5 (permalink)  
Old 11-29-2006
OFFSIHR OFFSIHR is offline
Registered User
  
 

Join Date: Oct 2006
Posts: 23
Quote:
Originally Posted by vgersh99
I'm not following what's being said here - could you provide a smple input AND a desired output, pls?
Sorry
I have used basename foo2 and it returns foo2

I did not see the NF on the end - now I get 6 (yes 6!) returned to this script
dir=/home/foo1/foo2
splitit=`echo $dir | awk -F '/' '{print $NF}'`
echo $splitit

when i use the script below blank is returned
dir=/home/foo1/foo2
splitit=`echo $dir | awk -F '/' '{print $1}'`
echo $splitit

desired output is foo2.
last folder name is only known when the script is run (its run from another script)

thanks
  #6 (permalink)  
Old 11-29-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Quote:
Originally Posted by OFFSIHR
Sorry
I have used basename foo2 and it returns foo2

I did not see the NF on the end - now I get 6 (yes 6!) returned to this script
dir=/home/foo1/foo2
splitit=`echo $dir | awk -F '/' '{print $NF}'`
echo $splitit
Hm...... I don't see where '6' would be returned...

Quote:
Originally Posted by OFFSIHR
when i use the script below blank is returned
dir=/home/foo1/foo2
splitit=`echo $dir | awk -F '/' '{print $1}'`
echo $splitit

desired output is foo2.
last folder name is only known when the script is run (its run from another script)
a 'blank' is what I would expect.
you're outputting '$1' - FIRST field. You have an ABSOLUTE path with the LEADING '/'.
What would be the FIRST field in a string '/a/b/c/d'?
It would be the string BEFORE the FIRST fieldSeparator. What is it in this case? It's the 'blank' character.
field1 -> 'blank'
field2 -> a
field3 -> b
field4 -> c
field5 -> d
Quote:
Originally Posted by OFFSIHR
thanks
Closed Thread

Bookmarks

Tags
linux

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 10:10 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