Extract name from path and change output


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Extract name from path and change output
# 1  
Old 02-15-2016
Extract name from path and change output

Dear All,

I would like to extract the file name without extension form a variable...
In particular I have a command like this one:

Code:
for file in path/to/file/example_number.ext
do something -input $file -output ${file%_number.ext}.new
done

means that in variable $file are saved all the path and the files.

Now, the output
Code:
-output  ${file%_number.ext}.new

save the output file
Code:
example.new

in:
Code:
path/to/file/

What I would like instead is to extract just the file name and output the result in a new/path

Code:
for file in path/to/file/example_number.ext
do something -input $file -output "new/path/example.new"
done

so the output file will be in

Code:
new/path/

and name
Code:
example.new

Obviously I have multiple files in the folder.

Hope is clear
Thanks for any help!

Best


Giuliano
# 2  
Old 02-15-2016
You'll need an intermediate step:
Code:
FN=path/to/file/example_number.ext
TMP=${FN%_number.ext}
echo ${TMP##*/}
example

to which you then can add the new path.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 02-15-2016
Thank you for you suggestion...

do you know how to add the indtermediated step inside the command above?

Best

Giuliano
# 4  
Old 02-15-2016
Code:
for file in path/to/file/example_number.ext
  do    TMP=${file%_number.ext} 
        something -input $file -output "new/path/${TMP}.new" 
  done

This User Gave Thanks to RudiC For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

How to change Absolute path to Relative path

Hello, I have a doubt:- --------------------- Current script:- ################################################################################################ prefix=user@my-server: find . -depth -type d -name .git -printf '%h\0' | while read -d "" path ; do ( cd "$path" || exit $?... (4 Replies)
Discussion started by: sahil_jammu
4 Replies

3. Shell Programming and Scripting

how can i change my PATH

1. that you are calling the POSIX UNIX commands located under /usr/xpg4/bin instead of those under /usr/bin 2. that you can run programs from the current directory 3. that your scripts located in the bin directory under your HOME directory can be found and executed from any... (1 Reply)
Discussion started by: vpatel44
1 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. UNIX for Dummies Questions & Answers

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 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... (4 Replies)
Discussion started by: Samuel Gordon
4 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. 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

10. UNIX for Dummies Questions & Answers

extract a part of a path like (input: /etc/exp/home/bin ====> output: exp)

Hi, I need to make some extraction . with the following input to get the right output. input: /etc/exp/home/bin ====> output: exp and input: aex1234 ===> output: ex Thanks for your help, (4 Replies)
Discussion started by: yeclota
4 Replies
Login or Register to Ask a Question