LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-05-2003, 11:04 PM   #1
Ripper951
LQ Newbie
 
Registered: Jan 2003
Posts: 7

Rep: Reputation: 0
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.
 
Old 01-06-2003, 09:34 AM   #2
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
The easiest would be to use 'dirname' for that.

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

prints: /path/to/file
 
Old 01-06-2003, 09:58 PM   #3
Ripper951
LQ Newbie
 
Registered: Jan 2003
Posts: 7

Original Poster
Rep: Reputation: 0
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?
 
Old 01-07-2003, 01:32 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
no...
Code:
PATH1=`dirname $1`
 
Old 01-07-2003, 01:58 AM   #5
Ripper951
LQ Newbie
 
Registered: Jan 2003
Posts: 7

Original Poster
Rep: Reputation: 0
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.
 
Old 01-07-2003, 03:54 AM   #6
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Rep: Reputation: 58
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)

Last edited by J_Szucs; 01-07-2003 at 04:58 AM.
 
Old 01-07-2003, 08:10 AM   #7
Ripper951
LQ Newbie
 
Registered: Jan 2003
Posts: 7

Original Poster
Rep: Reputation: 0
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
 
Old 01-07-2003, 10:38 PM   #8
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Rep: Reputation: 58
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.

Last edited by J_Szucs; 01-07-2003 at 11:21 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular expressions in script elmu Programming 1 11-28-2005 08:51 AM
Regular Expressions markjuggles Programming 2 05-05-2005 11:39 AM
Regular Expressions overbored Linux - Software 3 06-24-2004 02:34 PM
help with REGULAR EXPRESSIONS ner Linux - General 23 10-31-2003 11:09 PM
Regular expressions aromes Linux - General 1 10-15-2003 12:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration