extracting a field from directory path ??????


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extracting a field from directory path ??????
# 8  
Old 07-09-2007
I need it urgent. not a homework

Quote:
Originally Posted by Neo
Is this a homework assignment?
# 9  
Old 07-09-2007
The whole thing is a DIRECTORY path. XXX is a fixed folder name.

Quote:
Originally Posted by vino
Oh. So XXX is not a placeholder but an actual text.
# 10  
Old 07-09-2007
Code:
sed -n -e "s+.*/\([^/]*\)/XXX.*+\1+p" input.txt

# 11  
Old 07-09-2007
The whole directory path is in a variable. Not in a file. How can we modify the below?

Thanks

Quote:
Originally Posted by vino
Code:
sed -n -e "s+.*/\([^/]*\)/XXX.*+\1+p" input.txt

# 12  
Old 07-09-2007
Code:
var="/abc/fsg/sdfhgsa/fasgfsd/adfghad/XXX/fhsad"
echo $var |awk ' BEGIN{FS="/"}
                 {
                        for(i=1;i<=NF;i++){
                                if ($i == "XXX") {
                                        print $(i-1)
                                        next
                                }

                        }
                 }'

# 13  
Old 07-09-2007
Thanks to all of you !!!!
# 14  
Old 07-10-2007
YOUR MAKING IT TOO HARD. Use Korn/Bash built-in variable operators.
##
#EXPRESSION RESULT
#/home/david/waffen/long.file.name
#${path%%.*} /home/david/waffen/long
#${path%.*} /home/david/waffen/long.file
#${path} /home/david/waffen/long.file.name
#${path#/*/} /david/waffen/long.file.name
#${path##/*/} long.file.name
#${path##*/} long.file.name

###

PATH1=/A/B/C/D/XXX/E/F/G
PATH2=/A/B/C/XXX/E

echo "${PATH1%%/XXX/*}"
echo "${PATH2%%/XXX/*}"

OUTPUT:
$ echo "${PATH1%%/XXX/*}"
/A/B/C/D
$ echo "${PATH2%%/XXX/*}"
/A/B/C

-David
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

What is the difference ../directory path and ./directory path in ksh?

What is the difference ../directory path and ./directory path in ksh? (1 Reply)
Discussion started by: TestKing
1 Replies

2. UNIX for Beginners Questions & Answers

Convert Relative path to Absolute path, without changing directory to the file location.

Hello, I am creating a file with all the source folders included in my git branch, when i grep for the used source, i found source included as relative path instead of absolute path, how can convert relative path to absolute path without changing directory to that folder and using readlink -f ? ... (4 Replies)
Discussion started by: Sekhar419
4 Replies

3. UNIX for Dummies Questions & Answers

Extract directory name from the full directory path in UNIX using shell scripting

My input is as below : /splunk/scrubbed/rebate/IFIND.REBTE.WROC.txt /splunk/scrubbed/rebate/IFIND.REBTE.WROC.txt /splunk/scrubbed/loyal/IFIND.HELLO.WROC.txt /splunk/scrubbed/triumph/ifind.triumph.txt From the above input I want to extract the file names only . Basically I want to... (5 Replies)
Discussion started by: IshuGupta
5 Replies

4. Shell Programming and Scripting

sed/awk for extracting directory from file path

Hi, I have following path: set file_path = D:/forums/prac/somedir/new1/file1.txt or set file_path = E:/new/forums1/prac/somedir/new2/file2.txt I need to grep "somedir" from file path. In this case preceding directory "prac" remains same for both the paths, but directories preceding... (7 Replies)
Discussion started by: sarbjit
7 Replies

5. Shell Programming and Scripting

Extracting Directory From Path

Hi guys. I'm doing some bash scripting and have run into a snag. Say I have the path: /home/one/two/three/ All I need is the 'three' while making a filename. Is there an easy way to do this? I've tried using grep (because I'm that smart.) cut (as I'm unable to tell how many fields there... (3 Replies)
Discussion started by: Drayol
3 Replies

6. Shell Programming and Scripting

Retrieve directory path from full file path through sh

Hi, I have a file abcd.txt which has contents in the form of full path file names i.e. $home> vi abcd.txt /a/b/c/r1.txt /q/w/e/r2.txt /z/x/c/r3.txt Now I want to retrieve only the directory path name for each row i.e /a/b/c/ /q/w/e/ How to get the same through shell script?... (7 Replies)
Discussion started by: royzlife
7 Replies

7. UNIX for Dummies Questions & Answers

Extracting parts from an absolute path

Hi, How can I extract parts from an absolute path? For example : The absolute path is /dir1/dir2/dir3/dir4/dir5.I need the relative path starting with directory given as parameter : for instance if the parameter is dir3 then the result should be dir3/dir4/dir5 I need generic solution... (9 Replies)
Discussion started by: mortanon
9 Replies

8. Shell Programming and Scripting

Need help with extracting field

hi there, i'm new to shell scripting as i usually just use our solaris server for db administration and to get some basic system information. however i'm stuck with something that requires further scripting knowledge. I need to extract the Name, Desc and Status from pkginfo-l. I need them... (10 Replies)
Discussion started by: sbavanis
10 Replies

9. Shell Programming and Scripting

Extracting file name from path

if I have a variable value "filetoprocess" equal to "/aaa/bbb/ccc" How can I simply extract the file name of "ccc" (4 Replies)
Discussion started by: ytakbob
4 Replies

10. Shell Programming and Scripting

Extracting field from array

I have a problem with the following lines of code: SaveIFS=$IFS IFS="/" declare -a Array=($1)#take unix path, split by '/' website=${Array} #stores website name The confusing part is that it works on one machine, but not on the other. If the code is... (2 Replies)
Discussion started by: montana24
2 Replies
Login or Register to Ask a Question