perl split funciton - special character "/"


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers perl split funciton - special character "/"
# 1  
Old 02-07-2008
perl split funciton - special character "/"

HI,
I have a directory structure. /abc/def/ghi/
I want to store it into array.
So that if I do a pop function on that array I can easily go to previous directory.
But how can i split and store it.

@Directory = split(/\//,$DirectoryVarialbe)

That doest works. Any other escape sequence should be given??

Kindly help.
# 2  
Old 02-07-2008
Perl split function

Hi,
It should work fine. I tried the following...

Code:
#!/usr/bin/perl -w
use strict;

# Program to accept a file path and store it in a array

my $dir = "/abc/def/ghi/";

my @Dir = split(/\//, $dir);

pop(@Dir);

my $PreviousDir = join("/", @Dir);
print "The original Directory is $dir\n";
print "The new Directory is $PreviousDir\n";

# 3  
Old 02-07-2008
Following Perl 5.6.1 test code works for me:

Code:
#!/usr/local/bin/perl -w

my $DirectoryVariable="/abc/def/ghi";

my @Directory = split(/\//, $DirectoryVariable);

print "@Directory";

# 4  
Old 02-07-2008
Quote:
Originally Posted by deepakwins
That doest works. Any other escape sequence should be given??
Kindly help.
Are you running into the first element being the "/" and undef as a problem?
# 5  
Old 02-07-2008
Thakns this worked!!!

Is there any simple method to do to parent directory.
similar to "cd .."??
Quote:
Originally Posted by MobileUser
Hi,
It should work fine. I tried the following...

Code:
#!/usr/bin/perl -w
use strict;

# Program to accept a file path and store it in a array

my $dir = "/abc/def/ghi/";

my @Dir = split(/\//, $dir);

pop(@Dir);

my $PreviousDir = join("/", @Dir);
print "The original Directory is $dir\n";
print "The new Directory is $PreviousDir\n";

# 6  
Old 02-08-2008
Quote:
Originally Posted by HPAVC
Are you running into the first element being the "/" and undef as a problem?
Thanks for your reply. The escape sequence worked. Initially i managed to load the array without even spliting the fields from the sclar variable. That was in an attempt of trail and error. Now I forgot how I did that though.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove a word that ends with special character "!"

Hi all. I want to use sed to remove a word that ends with "!" in the first page of a file. The word I want to remove is: "DNA!". I have search for an answer and nothing of what I found helped me. ~faizlo (2 Replies)
Discussion started by: faizlo
2 Replies

2. Shell Programming and Scripting

Remove word after special character "/"

Hi There, I have one requirement to remove word after character "/". Input file is 2017-07-12|02|user1l|domain1/userl|0 2017-07-12|02|user2|domain1/user2|5 2017-07-12|02|user3|domain2/user3|0 2017-07-12|02|user4|domain1/user4|432 and require OP file is ... (2 Replies)
Discussion started by: anshu ranjan
2 Replies

3. Shell Programming and Scripting

Perl split string separated by special character

Hello I have string (string can have more sections) LINE="AA;BB;CC;DD;EE"I would like to assigne each part of string separated by ";" to some new variable. Can someone help? (4 Replies)
Discussion started by: vikus
4 Replies

4. Shell Programming and Scripting

Need HELP with AWK split. Need to check for "special characters" in string before splitting the file

Hi Experts. I'm stuck with the below AWK code where i'm trying to move the records containing any special characters in the last field to a bad file. awk -F, '{if ($NF ~ /^|^/) print >"goodfile";else print >"badfile"}' filename sample data 1,abc,def,1234,A * 2,bed,dec,342,* A ... (6 Replies)
Discussion started by: shell_boy23
6 Replies

5. Shell Programming and Scripting

How to print range of lines using sed when pattern has special character "["

Hi, My input has much more lines, but few of them are below pin(IDF) { direction : input; drc_pinsigtype : signal; pin(SELDIV6) { direction : input; drc_pinsigtype : ... (3 Replies)
Discussion started by: nehashine
3 Replies

6. Shell Programming and Scripting

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

7. UNIX for Advanced & Expert Users

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

8. UNIX for Dummies Questions & Answers

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

9. Shell Programming and Scripting

Unix Perl split special character $

All I'm trying to split a string at the $ into arrays @data:=<dataFile> a $3.33 b $4.44 dfg $0.56 The split command I have been playing with is: split(/\$/, @data) which results with a .33 b .44 dfg .56 any help with this is appreciated /r Rick (9 Replies)
Discussion started by: schultz2146
9 Replies

10. Shell Programming and Scripting

how to split special characters "|" using awk

Hi friends I need to splict special character "|" here. Here is my script which giving error LINE=INVTRAN|cd /home/msgGoogle TraxFolderType=`awk -F"|" '{print $1}' $LINE` filePath=`awk -F"|" '{print $2}' $LINE` echo "TraxFolderType: "$TraxFolderType echo "filePath :"$filePath ... (3 Replies)
Discussion started by: krishna9
3 Replies
Login or Register to Ask a Question