taking the end off a path


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting taking the end off a path
# 8  
Old 06-06-2004
thanks to anarchie for the tip with double quotes.
Fixes this particular problem nicely.
# 9  
Old 06-06-2004
Quote:
This particular problem
Do you have another problem for the forum!!!? PS - Glad to help anyway I [we] can.
# 10  
Old 06-15-2004
If you are using KSH try the following. Its really easy to use.

path=`print ${parent%/*}`
filename=`print ${parent##*/}`

echo $path
/foo/bar

echo $filename
thing

In short the code for 'path' will print everything before the last /
The code for 'filename' will print eveything after the last /
It doesnt matter how many sub directories you have in the path name, it will always work.

Try it, Its probably one of my most used pieces of code!

Hope this helps
Helen
# 11  
Old 06-24-2005
Question

hi,

I would need a basename like action (or the '##*/') but probably in combination with awk.

I need it to transform ftp commandfiles from put to get syntax.

If this is the original file :
open 10.1.91.15
user caluwaek xxxxxxx
cd /export/home/caluwaek/ftp
put /export/home/caluwaek/ftp_bestanden/bestand.txt
put /export/home/caluwaek/ftp_bestanden/bestand2.txt bestand2_copy.txt
bye

I use sed to change put in get, as wel as to change the filenames from left to right. Result :
open 10.1.91.15
user caluwaek cindy75
cd /export/home/caluwaek/ftp
get /export/home/caluwaek/ftp_bestanden/bestand.txt
get bestand2_copy.txt /export/home/caluwaek/ftp_bestanden/bestand2.txt
bye

The only thing that remains to do is to drop the paths. Only on the lines which start with get and only if the file is on the right side.

Anybody some advice ?

Thanks!

Kris

Last edited by Perderabo; 07-07-2005 at 12:17 PM.. Reason: Censor password
# 12  
Old 06-27-2005
If you are already using sed it is easier to do everything in sed (otherwise i would have suggested the variable expansion in the ksh too):

first:

Code:
/^get/ {
          ...command...
          }

issues commands only on lines starting with "get".

second:

Code:
\(\/[^<space>]*\/\)

is the regular expression to match anything between the first and the last "/" character in one "word" (1) and group it.

Hope this helps.

bakunin


(1) "a sequence of nonblank characters separated by blanks", according to an IBM OS/370 manual explaining what a "word" is, ROFL
# 13  
Old 06-28-2005
Code:
$ cat file1
open 10.1.91.15
user caluwaek cindy75
cd /export/home/caluwaek/ftp
put /export/home/caluwaek/ftp_bestanden/bestand.txt
put /export/home/caluwaek/ftp_bestanden/bestand2.txt bestand2_copy.txt
bye

$ awk 'sub(/put/,"get",$1)&&$3{x=$2;$2=$3;$3=x};1' file1 > file2

$ cat file2
open 10.1.91.15
user caluwaek cindy75
cd /export/home/caluwaek/ftp
get /export/home/caluwaek/ftp_bestanden/bestand.txt
get bestand2_copy.txt /export/home/caluwaek/ftp_bestanden/bestand2.txt
bye

Login or Register to Ask a Question

Previous Thread | Next Thread

9 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. UNIX for Advanced & Expert Users

Command to see the logical volume path, device mapper path and its corresponding dm device path

Currently I am using this laborious command lvdisplay | awk '/LV Path/ {p=$3} /LV Name/ {n=$3} /VG Name/ {v=$3} /Block device/ {d=$3; sub(".*:", "/dev/dm-", d); printf "%s\t%s\t%s\n", p, "/dev/mapper/"v"-"n, d}' Would like to know if there is any shorter method to get this mapping of... (2 Replies)
Discussion started by: royalibrahim
2 Replies

3. Shell Programming and Scripting

Adding to the end of the path in .profile

Hi there, I have the following in my path: export PATH=/usr/bin:/usr/sbin But I want to change it programmatically(for the purpose of JBoss automatic(ansible) installs) to: export PATH=/usr/bin:/usr/sbin:/usr/jdk/jdk1.8.0_60 and after that I need to insert the JAVA_HOME programmatically,... (7 Replies)
Discussion started by: hvdalsen
7 Replies

4. Solaris

Test program running taking much more time on high end server T5440 than low end server T5220

Hi all, I have written the following program and run on both T5440 and T5220 on same OS version. I found that T5540 server takes more time than T5220. Please find below the details. test1.cpp #include <iostream> #include <pthread.h> using namespace std; #define NUM_OF_THREADS 20... (17 Replies)
Discussion started by: sanjay_singh85
17 Replies

5. UNIX for Dummies Questions & Answers

Ensuring / at end of directory path

I have a variable name storing a directory path set dirpath = /home/chrisd/tatsh/branches/terr0.50/darwin I want to ensure there is always a "/" at the end, and if there are more than one "/", that I reduce it to just one. ---------- Post updated at 08:16 AM ---------- Previous update was... (3 Replies)
Discussion started by: kristinu
3 Replies

6. Shell Programming and Scripting

Use of Begin IF ,END IF END not working in the sql script

Hi I have written a script .The script runs properly if i write sql queries .But if i use PLSQL commands of BEGIN if end if , end ,then on running the script the comamds are getting printed on the prompt . Ex :temp.sql After connecting to the databse at the sql prompt i type... (1 Reply)
Discussion started by: isha_1
1 Replies

7. Shell Programming and Scripting

taking a part of string from a given path

Hi I have two path as follows system/console/bin/code/sample/testfile.txt system/console/bin/database/files/new/dbfile.txt I need the output as code/sample in first case database/files/new in second case That is I am omitting system/console/bin and the filename(s) in both... (2 Replies)
Discussion started by: ssuresh1999
2 Replies

8. Shell Programming and Scripting

Need UNIX shell scripting end to end information

Hi, I would like to learn shell scripting in UNIX. Can any one please give me the support and share the information/documents with me. If any documents please post it to aswanikumar_nimmagadda@yahoo.co.in Thanks in advance...!!! (3 Replies)
Discussion started by: aswani_n
3 Replies

9. UNIX for Dummies Questions & Answers

Taking a name out of $PATH

so here is my path: /usr/bin:/bin:/usr/sbin:/sbin:/Users/a:/usr/local/bin:/usr/X11/bin how would I remove only the "a" or name string within it? any help appreciated (7 Replies)
Discussion started by: cleansing_flame
7 Replies
Login or Register to Ask a Question