0

after using find I need to iterate the files

var=`find -name "reg"`
#replace ./ for newline
for file in $var; do 
    #something
done

edit: SOLVED with ${string#*/} it takes away the ./ I can survive with that I think

2
  • Much easier: find -name "reg" -exec <your code> \;. Within "your code", you refer to the current findee as {}. Even better is to use find -name "reg" -print | xargs <your command>, if your command supports that style. Commented Aug 3, 2011 at 16:45
  • 2
    (1.) -execdir is safer than -exec. (2.) find ... -print0 | xargs -0 ... is safer than -print ... Commented Aug 3, 2011 at 17:04

4 Answers 4

2

EDIT:

I am not concerned about how you get the $file, using command line or whatever suits you. I am much more concerned about the #do something part of your question, which I believe is the main question and all those who are posting various find -name 'reg' | xargs ... should think twice before complicating the matters for OP.


use sed

var=`find -name "reg"`
#replace ./ for newline
for file in $var; do 
    sed -i 's|\./|\n|g' $file
done

remove the -i options to get output on the screen, if satisfied output is obtained use -i to actually change the line.
On second read, maybe I am replacing the other way round, perhaps you want to replace newline with ./ ? Its bit complicated

sed -i ':a;N;$!ba;s|\n|./|g' $file

as always, test without -i option and then decide if you want to modify the file.
For explanation of second sed magic, read this SO . Its exactly same, except for the ./ character for replace string.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks I solved it with ${string#*/} I'll try to survive with this
0
find -name "reg" | while read file; do ...; done

Comments

0
while IFS= read -r line; do
  var="${line#./}"
  echo "$var"
done < <(find . -name reg -print)

Comments

0

ok I finally solved it guys, i could delete the ./
string=./blabla/bla

string=${string#*/} #delete stuff until the first / included

echo $string #got blabla/bla

2 Comments

and what if your string contains blah./blabla/bla ? you will lose the first 'blah.' , and it removes the substring, NOT replace it, I think you did NOT explained your question properly
it will never start with something before ./ find returns ./some/file ./some/other/file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.