How to extract the path


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to extract the path
# 1  
Old 03-24-2010
How to extract the path

I make this script that uses "strace" to trace programs. The script looks for files that the program opens. It looks like this
Code:
open("/lib/libtermcap.so.2", O_RDONLY)  = 3
open("/dev/tty", O_RDWR|O_NONBLOCK|O_LARGEFILE) = 3

I want to extract the path without the quotes, preferably using grep and cut, but it can be also sed or awk (I just don't know these much). I worked out a regular expression egrep -o "\(\"[^\"]*\"," | cut -f 2 -d \" but then I realized the path can contain characters like " as well.

I thought the solution could look something like this egrep -o "\(\"[^{[^\\]\",[:space:]}]*\"," (any characters except string "[^\\]\",[:space:]" and these characters repeating)... Because I found out that strace doesn't work correctly if "\" without quotes is at the end of the file. So I thought I could use it as a benefit. However, it doesn't work Smilie .. I'm not very good in regular expressions indeed.

EDIT: And I'm sorry ... [:space:] is not the solution, because the script should work in FreeBSD as well and it uses "truss" instead of "strace" and there's no space after ", Smilie

Last edited by Yogesh Sawant; 03-24-2010 at 11:02 PM.. Reason: added code tags
# 2  
Old 03-24-2010
Code:
sed '/^open/s/.[^"]*\"//;s/\".*//'

# 3  
Old 03-25-2010
Unfortunately doesn't work for input like this:
Code:
open("/homes/eva/xs/username/script\",\"", O_RDONLY|O_LARGEFILE) = 3

# 4  
Old 03-26-2010
Guys, really don't know anybody? Smilie Just how to extract the path from the first " up to the last sequence ", I can't figure it out.

Here's another example of what this script needs to be capable extract (in this case /homes/xx):
Code:
openat(AT_FDCWD, "/homes/xx", O_WRONLY)

# 5  
Old 03-26-2010
Code:
$ echo 'openat(AT_FDCWD, "/homes/xx", O_WRONLY)' | sed 's/[^"][^"]*"\([^"][^"]*\).*/\1/'
/homes/xx

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract directory path from a parameter

i was attempting to extract a directory path that was passed from a parameter with this code vdir=`dirname $p1` echo current directory $vdir it does not work when the parameter passed has wild card on it. for example $ sh sample1.sh "/sbin/log/c*.log" dirname: extra operand... (2 Replies)
Discussion started by: wtolentino
2 Replies

2. Shell Programming and Scripting

Need to extract a folder from a full path

Hi everyone, I have different folders which looks like this: /mnt/ecrm/master/ecrm/templates/brochure/de_DE/zeitlos.ott /mnt/ecrm/master/ecrm/templates/mail/en_US/default.html /templates/header_and_footer/en_US/default.txt I want to get the bold text only in a variable. I already have a... (3 Replies)
Discussion started by: evilass
3 Replies

3. Shell Programming and Scripting

Extract partial string from path.

Hi all, i've a string $DIR=/u/user/NDE/TEST_LOGS/20110622_000005_TEST_11_HD_120/HD/TEST_11_HD_120/hd-12 i need to extract string from 2011.... i.e i need it as 20110622_000005_TEST_11_HD_120 as matched string, and in turn i need to split values 20110622_000005_TEST_11_HD_120 into two.... (6 Replies)
Discussion started by: asak
6 Replies

4. Shell Programming and Scripting

How to extract strings from full path when full path is not fixed

/Path/snowbird9/nrfCompMgrRave1230100920.log.gz:09/20/2010 06:14:51 ERROR Error Message. /Path/snowbird6/nrfCompMgrRave1220100920.log.gz:09/20/2010 06:14:51 ERROR Error Message. /Path/snowbird14/nrfCompMgrRave920100920.log.gz:09/20/2010 06:14:51 ERROR Error Message.... (0 Replies)
Discussion started by: Shirisha
0 Replies

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

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

7. Shell Programming and Scripting

Extract path within path

Hi, I have a environmental variables, ORACLE_HOME=/u01/oracle/ORCL/db/tech/10.2.0 ORACLE_SID=ORCL Now I need to create a variable and need to extract some part from ORACLE_HOME. I need to get the path from ORACLE_HOME till ORACLE_SID as /u01/oracle/ORCL. I may need to check also... (6 Replies)
Discussion started by: sreejitnair123
6 Replies

8. Shell Programming and Scripting

fnsplit, Extract filename from path

Hi all, I know this has been covered a lot, I have been searching and reading for hours on the subject, however so far I have been unsuccessful at accomplishing the goal using sed. I know this can be done with parameter expansion (Thanks cfajohnson for a great explanation of parameter... (5 Replies)
Discussion started by: Festus Hagen
5 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

tar extract to new path

I need to extract a file,which contains the path to a new path. sample tar file tar -tfv class.tar | grep client -rw------- 0/1 99 Jan 22 12:46 2004 /usr/openv/netbackup/db/class/mariner/clients -rw------- 0/1 102 Sep 25 11:33 2007... (1 Reply)
Discussion started by: jouuu
1 Replies
Login or Register to Ask a Question