How to select First two directory in from path name?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to select First two directory in from path name?
# 1  
Old 11-12-2012
How to select First two directory in from path name?

Can somebody help me on below question

I have path and i want to select first two directory name in variable how we can do this.

/Myname/xyz/yourname/abc/somebodyname

i want to select /Myname/xyz in variable.

Quick help will be appriciated.

Thanks in advance

Regards,
Kumar
# 2  
Old 11-12-2012
Code:
$ var='/Myname/xyz/yourname/abc/somebodyname'

$ echo "$var"|sed -n 's:^\(/[^/]\{1,\}/[^/]\{1,\}\).*:\1:p'
/Myname/xyz

# 3  
Old 11-12-2012
Quote:
Originally Posted by elixir_sinari
Code:
$ var='/Myname/xyz/yourname/abc/somebodyname'
 
$ echo "$var"|sed -n 's:^\(/[^/]\{1,\}/[^/]\{1,\}\).*:\1:p'
/Myname/xyz

Dear elixir,
how we can assign the output "/Myname/xyz" to variable.
i am able to print it but not able to assign to variable. its giving error "Var is Directory"
# 4  
Old 11-12-2012
Code:
newvar=$(echo "$var"|sed -n 's:^\(/[^/]\{1,\}/[^/]\{1,\}\).*:\1:p')

This User Gave Thanks to elixir_sinari For This Post:
# 5  
Old 11-12-2012
with awk..

Code:
var='/Myname/xyz/yourname/abc/somebodyname'

dir_path=$(echo "$var" | awk -F "/" '{print "/"$2"/"$3}')

# 6  
Old 11-12-2012
how to Fetch value from varible

Can somebody help to me to get the required value from below path

./mydata/abc/youdata/xyz/OTHERDATA/zsoddata_testsod_labl_20120111_1.zip

i want to fetch the "testsod" from below path and file name..

Kind Regards,
kumar
# 7  
Old 11-12-2012
Code:
var=$( echo "./mydata/abc/youdata/xyz/OTHERDATA/zsoddata_testsod_labl_20120111_1.zip" | awk -F_ ' { print $2 } ' )

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. Shell Programming and Scripting

Bash to select and save file in new directory

I am trying to select a file in bash and save it to a directory. The below does run but no selected file is saved. Thank you :). bash # select file printf "please select a file to analyze with entered gene or genes \n" select file in $(cd... (4 Replies)
Discussion started by: cmccabe
4 Replies

4. Shell Programming and Scripting

Bash to select panel then specific file in directory

I am using bash to prompt a user for a choice using: where a "y" response opens a menu with available panels that can be used. while true; do read -p "Do you want to get coverage of a specific panel?" yn case $yn in * ) menu; break;; * ) exit;; * ) echo... (6 Replies)
Discussion started by: cmccabe
6 Replies

5. 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

6. Shell Programming and Scripting

Select files for two different directory

I have run one file.but this file is two different directory is there.I wrote the if loop but one directory to find it the file and another directory is not find. #!bin/bash a=/tmp mau="manual.sh" if then echo `ls -l $mau` else echo "file not there" b=/scr #not find the directory... (1 Reply)
Discussion started by: rajivgandhi
1 Replies

7. Shell Programming and Scripting

How to select the shortest path in grep search?

Hi, How can I display only one shortest path (A/B/configure)? $ grep configure file.txt A/B/configure A/B/C/configure A/B/C/D/configure Thank you. (9 Replies)
Discussion started by: hce
9 Replies

8. 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

9. Shell Programming and Scripting

regex to select last part of a path

Hi all, I am learning the use of regular expression and I would like to know which regex can be used to select only the last part of a directory path name. Something like: /dir1/dir2/dir2 and I want to select the last /dir2 where dir2 can be any kind of string. Thanks a lot for your help.... (7 Replies)
Discussion started by: elric
7 Replies

10. Shell Programming and Scripting

How to select the path that contains a certain string from a certain file?

Hi, I am new to this world of shell programming. I am facing a problem that is : I have directory which has many sub directories at different depth. say A/B/C/files A/B/files A/B/C/D/files In this directory structure there exists a file called ".project" in some of the sub... (2 Replies)
Discussion started by: bhaskar_m
2 Replies
Login or Register to Ask a Question