String substitution on find results inside exec/xargs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String substitution on find results inside exec/xargs
# 1  
Old 05-17-2008
String substitution on find results inside exec/xargs

What I'm trying to do is perform a copy, well a ditto actually, on the results of a find command, but some inline string substitution needs to happen.

So if I run this code
Code:
find ./ -name "*.tif"

I get back these results.

.//1234567.tif
.//abcdefg.tif

Now the action from exec or xargs I want would be these two things....

ditto .//1234567.tif /some/path/1/2/3/
ditto .//abcdefg.tif /some/path/a/b/c/

So as you can see it's taking the first three characters from each found result and using those to construct a path.

Any ideas I'm sure this can be done easier in a actual script, but i need to keep this as an actual "one line" command.

Any ideas?
# 2  
Old 05-17-2008
Code:
find . -name '*.tif' |
sed -e 's%\(.//*\)\(.\)\(.\)\(.\)\(.*\)%ditto \1\2\3\4\5 /some/path/\2/\3/\4/%'
| sh

It's a thin line between a one-liner and an "actual script".

Last edited by era; 05-17-2008 at 09:56 AM.. Reason: Optionally allow more than one slash
# 3  
Old 05-17-2008
Thanks era, works great! Time to spend some time with the sed docs though... I knew it could do it, but admittedly I'm not very familiar with it Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Exec and xargs mvoe file to directory

Hello, I am trying to move all the file listed by below command to /tmp/testing directory find ./ -maxdepth 1 -type f -mtime +3 I tried using -exec and xargs - none of the combination is working? Please, help (3 Replies)
Discussion started by: saurabh84g
3 Replies

2. Shell Programming and Scripting

Difference b/w xargs and "-exec" in Find

Hi, What is the difference between the following commands find . -type f -exec grep 'abc' {} \; and find . -type f | xargs grep 'abc' Appreciate your help. (2 Replies)
Discussion started by: bobbygsk
2 Replies

3. Shell Programming and Scripting

xargs vs exec with find:

Hi All, i'm trying to create a tar of all the .txt files i find in my dir . I've used xargs to acheive this but i wanted to do this with exec and looks like it only archives the last file it finds . can some one advice what's wrong here : find . -type f -name "*.txt" -print0 | xargs -0... (9 Replies)
Discussion started by: Irishboy24
9 Replies

4. Programming

difference bewteen pipe, xargs, and exec

I have read several docs on these on the web and looked at examples. I can't figure out the difference. In some cases you use one or the other or you combine them. can someone help me understand this? (1 Reply)
Discussion started by: guessingo
1 Replies

5. Shell Programming and Scripting

Display find results, and pipe to xargs

I have an overnight script which runs across a large directory to repair permissions and ownership. I also have this command output the list of files affected so that cron can email these as a log file. Previously I had the command in the form: find /path/to/files -not -user myname -print -exec... (4 Replies)
Discussion started by: mij
4 Replies

6. UNIX for Dummies Questions & Answers

How to find a file with specific string inside it.

Hi , Is there any way i can find a file with specific word inside it. For example if i want to find a file which has some text written inside it. How would i form a command to search them? (3 Replies)
Discussion started by: pinga123
3 Replies

7. Solaris

Piping results of 'ls' to 'find' using 'xargs'

I'm trying to get a count of all the files in a series of directories on a per directory basis. Directory structure is like (but with many more files): /dir1/subdir1/file1.txt /dir1/subdir1/file2.txt /dir1/subdir2/file1.txt /dir1/subdir2/file2.txt /dir2/subdir1/file1.txt... (4 Replies)
Discussion started by: MartynAbbott
4 Replies

8. Shell Programming and Scripting

How to find a string inside files

Hi, I would like to know how to get a list of files that contain a specific string inside them. Thanks (12 Replies)
Discussion started by: yoavbe
12 Replies

9. Shell Programming and Scripting

MV files with xargs or -exec

Hi I need to move multiple (say 10 files) from one location to another location. My selection would be like this... ls -ltr *.arc | head ---> Need to move top 10 files with single command without iterating in loop. I know we can move files like this with find command but not sure if I can... (4 Replies)
Discussion started by: malaymaru
4 Replies

10. UNIX for Dummies Questions & Answers

Difference between xargs and exec

Hi, I have tried both the options in small dummy scripts, but somehow i can't differentiate between the two. find . -name H* -exec ls -l {} \; find . -name H* | xargs ls -l Both work the ditto way. Any help is appreciated. (19 Replies)
Discussion started by: vibhor_agarwali
19 Replies
Login or Register to Ask a Question