LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   shell script and regular expressions (https://www.linuxquestions.org/questions/linux-newbie-8/shell-script-and-regular-expressions-40431/)

Ripper951 01-05-2003 11:04 PM

shell script and regular expressions
 
I am trying to write a shell script that will extract the path from a string passed to it as an argument can anyone help?

I have read over the shell scripting tutorials on the web and regex documentation and can't make any sense of the regex stuff at all.

The main thing I need the script to do is exrtract the path from the string and only the path, dropping the filename that is included. For example.

I need /path/to/file/filename.txt
to be stripped to return only this part /path/to/file and drop the file name.

If anyone can help I sure would appreciate it!

In the mean time I will continue researching how to solve this.

Mik 01-06-2003 09:34 AM

The easiest would be to use 'dirname' for that.

Ex.
dirname /path/to/file/filename.txt

prints: /path/to/file

Ripper951 01-06-2003 09:58 PM

And that can be used in a script? (sorry newbie when it comes to scripting.)

and be passed a parameter (e.g. $1) ?

how would i call that in a script?

Like this?

path1=dirname $1

Would that be correct?

acid_kewpie 01-07-2003 01:32 AM

no...
Code:

PATH1=`dirname $1`

Ripper951 01-07-2003 01:58 AM

Now can you tell me how to add a CR 0x0D to the end of this line?

echo "**** This test was run on `date` ****" >> $1

when this line is appended to the file $1 it needsto append a CR to the end of the line.

I have been searching the web and pulling my hair out over this for 4 and a half hours now with no resolution.

Thanks in advance for any help.

J_Szucs 01-07-2003 03:54 AM

This did not work?:

echo **** This test was run on `date` **** $'\r' >> $1

(See: man bash, section 'QUOTING').

As for cutting out the path: you can cut out any part of a full filename.
Just do a google seach and you will find plenty of good samples how to do this.
(I did it myself some days ago to solve the same problem, but unfortunately my script is not available at the moment and I do not exactly remember the syntax)

Ripper951 01-07-2003 08:10 AM

did not know you had to put the $ in front of it?

What function does that serve?

Although I did try the '\r' and '\015' and '\0x0D' didnt know about the $.

None of the tutorials on scripting on the net that I could find explains any of it very well.

I will try that and let you know if it worked or not.

Thanks

J_Szucs 01-07-2003 10:38 PM

Now I found my script, so here are some useful examples of how to cut out different parts of /path/to/filename.ext:

FULLFILENAME=/path/to/filename.ext

# This will extract /path/to into RESULT:
RESULT="${FULLFILENAME%*/*}"

# This will extract filename.ext into RESULT:
RESULT="${FULLFILENAME##*/}"

# This will extract ext (if any) into RESULT:
# (Note: Might be improved a bit since it returns /path/to/filename if there is no period (.) in FULLFILENAME)
RESULT="${FULLFILENAME##*.}"

As I told you, these are only some useful examples of parameter expansion.
See 'Parameter expansion' in man bash.


All times are GMT -5. The time now is 04:31 AM.