sed/awk for extracting directory from file path


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed/awk for extracting directory from file path
# 1  
Old 08-12-2011
Error 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 "prac" or after "somedir" are variable.

I know sed code which can give only complete dir path as :

Code:
echo $somedir | sed 's#\(.*\)/.*#\1#'

Thanks in Advance
# 2  
Old 08-12-2011
If you don't mind, please post the sample output you desire. Would help us understand a little better.
# 3  
Old 08-12-2011
Quote:
Originally Posted by dude2cool
If you don't mind, please post the sample output you desire. Would help us understand a little better.
I want "somedir" as output from this file path keeping in mind that this file path is variable but somedir will always comes after "prac" directory.
# 4  
Old 08-12-2011
Here you go:

Code:
 echo "E:/new/forums1/prac/somedir/new2/file2.txt"|awk -F"/" '{split($0,a,"/")} {for(i=1;i<=NF;i++) { if (a[i] == "prac")print a[i+1]}}'

The above will output somedir.
# 5  
Old 08-12-2011
How about this:

Code:
$ cat infile
D:/forums/prac/somedir/new1/file1.txt
E:/new/forums1/prac/somedir/new2/file2.txt
X:/not/this/one/file3.txt
X:/unpractile/somedir/one/file4.txt
 
$ sed -n 's/.*\/prac\/.*somedir\/.*$/somedir/p' infile
somedir
somedir

# 6  
Old 08-12-2011
Or...
Code:
echo $somedir | awk '/prac/{getline;print}' RS=/

# 7  
Old 08-12-2011
Alternate sed..
Code:
echo 'E:/new/forums1/prac/somedir/new2/file2.txt'| sed 's|.*prac.\([^/]*\)/.*|\1|'

echo 'D:/forums/prac/somedir/new1/file1.txt'|      sed 's|.*prac.\([^/]*\)/.*|\1|'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Parse Directory path - awk

Hi All, Need some help in parsing a directory listing .. output into 2 files Input file level1,/level2/level3/level4/ora001,10,IBB23 level1,/level2/level3/level4/ora001/blu1,,IBB23 level1,/level2/level3/level4/ora001/clu1,,IBB23 level1,/level2/level3/level4/ora002,,IBB24... (10 Replies)
Discussion started by: greycells
10 Replies

3. Shell Programming and Scripting

Variable of Path directory is not parsing in awk

Hi All, i had to split one files into 10 equally. For that i have coded below awk. OUTPUT_FILE=/home/sit/path/Files/file_EXPORT.lst DIR_NM=`dirname ${OUTPUT_FILE}` awk -v CURR_DATE="$(date +'%d-%m-%Y-%H-%M')" -v pth=$DIR_NM '{print >> pth/"tgt_file_name"CURR_DATE"_"NR%10 }' ${OUTPUT_FILE} ... (7 Replies)
Discussion started by: looney
7 Replies

4. Shell Programming and Scripting

Extracting lines from a file with sed and awk

My source file is structured with two words on each line word1 word2 word1 word2 I am using sed and awk to grab groups of specific lines line=`awk 'NR>=4 && NR<=7' file1`; echo $line line=` sed -n '1,5'p file1`; echo $line The resulting output is word1 word2 word1 word2 word1... (1 Reply)
Discussion started by: bash_in_my_head
1 Replies

5. Shell Programming and Scripting

How to remove a directory path in a file using sed

Hi, I want to remove a directory path from a file starting with the bracket using sed command. eg. (cd /man/abc/def ; \ aaaaa bbbb (cd /man/aaaa/aa; \ op. aaaaa bbbb The "(cd /man" is to be consideres as the start. I tries the below thing, but it didnt worked. Can anyone help.... (3 Replies)
Discussion started by: vdhingra123
3 Replies

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

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

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

9. Shell Programming and Scripting

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

Hi I'll be getting a directory path as the input to the script. E.g. 1 abc/fsg/sdfhgsa/fasgfsd/adfghad/XXX/fhsad e.g. 2 sadfg/sadgjhgds/sd/dtuc/cghcx/dtyue/dfghsdd/XXX/qytq This input will be stored in a variable. My query is how to extract the field in a variable VAR which occurs... (15 Replies)
Discussion started by: skyineyes
15 Replies

10. Shell Programming and Scripting

sed, grep, awk, regex -- extracting a matched substring from a file/string

Ok, I'm stumped and can't seem to find relevant info. (I'm not even sure, I might have asked something similar before.): I'm trying to use shell scripting/UNIX commands to extract URLs from a fairly large web page, with a view to ultimately wrapping this in PHP with exec() and including the... (2 Replies)
Discussion started by: ropers
2 Replies
Login or Register to Ask a Question