Extract The File Path using SED


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract The File Path using SED
# 1  
Old 03-02-2009
Extract The File Path using SED

hi,

I have a file path like usr/scripts/pass/bin and usr/scripts/pass/line

I want to extract first three characters using sed

Like for path usr/scripts/pass/bin i want to extract usr/scripts/pass
and for path usr/scripts/pass/line i want to extract usr/scripts/pass
# 2  
Old 03-02-2009
Code:
man dirname

# 3  
Old 03-02-2009
thanks..but it does not solve my purpose...lets say that we have a text file, in which we want to extract first three characters seprated by '/'(slash)...it can be implemented by SED...but i don't know how to do it :-(
# 4  
Old 03-02-2009
Code:
sed 's/\([^\/]*\/[^\/]*\/[^\/]*\)\/.*/\1/' infile

# 5  
Old 03-02-2009
thanks....

It worked...

What if i want extract only the third character....

like for usr/scripts/pass/bin i only want pass
# 6  
Old 03-02-2009
Quote:
Originally Posted by Diggi
thanks....

It worked...

What if i want extract only the third character....

like for usr/scripts/pass/bin i only want pass
Code:
 awk -F"/" '{print $3}' infile

# 7  
Old 03-02-2009
Code:
echo 'usr/scripts/pass/bin'|sed 's!.*/\(.*\)/.*!\1!'
pass

Code:
echo 'usr/scripts/pass/bin'|sed 's!\(.*\)/.*!\1!'
usr/scripts/pass

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use sed to insert character in the beginning of file path?

I need to manipulate one Database file on Solaris 11 in which contains more than 5000 lines of data file path like this: '/data1/oradata/DBNAME/system01.dbf', '/data7/oradata/DBNAME/undotbs1_01.dbf', '/data1/oradata/DBNAME/sysaux01.dbf', '/data28/oradata/DBNAME/userdata01.dbf', ... (6 Replies)
Discussion started by: duke0001
6 Replies

2. Shell Programming and Scripting

sed - replacement file path with variable - Escaping / character

Hi,, I have the line below in a file: $!VarSet |LFDSFN1| = '"E:\APC\Trials\20140705_427_Prototype Trial\Data\T4_20140705_Trial_Cycle_Data_13_T_Norm.txt" "VERSION=100 FILEEXT=\"*.txt\" FILEDESC=\"General Text\" "+""+"TITLE{SEARCH=NONE NAME=\"New Dataset\" LINE=1I want to write a script to change... (2 Replies)
Discussion started by: carlr
2 Replies

3. Shell Programming and Scripting

sed -Replacing file path within .txt file

Hi, I am trying to use sed to replace a file path within all the .lay (.txt) files in a folder. I feel that this should be easy but I can't get it to work no matter what i try. I'm using cygwin. For a .txt file containing the below line I want to replace this file path with a new one. ... (1 Reply)
Discussion started by: carlr
1 Replies

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

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

6. Shell Programming and Scripting

problem with path extract from file

hello, i have a configuration file app.conf under /tmp, containing values like : param1=/data/something param2=/data/somethingelse i have a bash script that has to list the files under the path that corresponds to param2 : #!/bin/bash dir=$(cat tmp/app.conf | grep param2 | sed ... (6 Replies)
Discussion started by: chaa
6 Replies

7. Shell Programming and Scripting

one liner to extract path from PATH variable

Hi, Could anyone help me in writing a single line code by either using (sed, awk, perl or whatever) to extract a specific path from the PATH environment variable? for eg: suppose the PATH is being set as follows PATH=/usr/bin/:/usr/local/bin:/bin:/usr/sbin:/usr/bin/java:/usr/bin/perl3.4 ... (2 Replies)
Discussion started by: royalibrahim
2 Replies

8. Shell Programming and Scripting

How to get file path using sed

Hi , Hi I want to know how can i extract file path using sed? Eg: C:/folder1/abc/file.txt Now , i want C:/folder1/abc/ only, So that i can move to that directory containing that file . So how can i do it using sed? Thanks in Advance Sarbjit (2 Replies)
Discussion started by: sarbjit
2 Replies

9. Shell Programming and Scripting

Extract directory from a file path

Im trying to extract a directory from a path entered by the user Lets say the path is path=/home/bliss/files/myfile.txt i wanna extract "/home/bliss/files" from $path ... how can i do this? (4 Replies)
Discussion started by: mrudula009
4 Replies

10. UNIX for Dummies Questions & Answers

extract only file name from full path file name

What is the smartest way to just extract file name from a full path name. e.g. if I have /usr/sanjay/bin/file_name.c I want only file_name.c Sanjay (2 Replies)
Discussion started by: sanjay92
2 Replies
Login or Register to Ask a Question