简体   繁体   中英

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 the ./ 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 ... find -name 'reg' | xargs ... should think twice before complicating the matters for OP.


use sed

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM