开发者

bash: replace in a string './' for newline

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 th开发者_运维技巧e ./ I can survive with that I think


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.


find -name "reg" | while read file; do ...; done


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜