Bash script to add dynamic path to existing .ini file, perhaps using sed?

I have a well known .sqlite file contained in many unique user home folders with a file structure: /home/user/unique-ip-address/folder/file.sqlite I decided to move all these .sqlite files to a tmpfs mount and already done this, while maintaining the complete directory structure, so every .sqlite file is now in the directory: /mnt/tmpfs/home/user/unique-ip-address/folder/file.sqlite

I'm looking for a way to edit a file with a common name .ini in each user's home folder with a unique file path (eg: /home/user/unique-ip-address/folder/file.ini), I figure using find and sed should do the trick, but I'm not sure how the search results should match the user with the correct folder in / mnt / tmpfs. I would like to add the new .sqlite location used in / dev / shm to file.ini in my home folders after dir = in the .ini file. Thanks!

+2


a source to share


1 answer


I believe that something like this should do the trick:


find /home/*/unique-ip-address/folder/file.ini | xargs -n1 perl -pe 'BEGIN{$infile="/mnt/tmpfs/$ARGV[0]"} {s/dir=/$&$infile/}'

      



This command doesn't change anything, it just prints everything out. If this looks like what you want, just add -i.bak to the very end and it will bring the changes in place. The original file will be renamed to file.ini.bak.






find /home/*/unique-ip-address/folder/file.ini | xargs -n1 perl -pe 'BEGIN{$infile="/mnt/tmpfs/$ARGV[0]"} {s/dir=/$&$infile/} -i.bak'

      



+1


a source







All Articles