简体   繁体   English

Bash脚本可能使用sed将动态文件路径附加到现有.ini文件中?

[英]Bash script to append dynamic file path to existing .ini file possibly using sed?

i have a commonly named .sqlite file contained within many unique user's home folders with file structure: /home/user/unique-ip-address/folder/file.sqlite I've decided to move all of these .sqlite files to a tmpfs mount and have already done so maintaining the full directory structure, so each .sqlite file is now in: /mnt/tmpfs/home/user/unique-ip-address/folder/file.sqlite 我在一个具有以下文件结构的唯一用户主文件夹中包含一个通用命名的.sqlite文件:/home/user/unique-ip-address/folder/file.sqlite我决定将所有这些.sqlite文件移至tmpfs挂载并且已经这样做了,因此可以维护完整的目录结构,因此每个.sqlite文件现在位于:/mnt/tmpfs/home/user/unique-ip-address/folder/file.sqlite

I'm looking for a way to edit a commonly named .ini file in each users home folder with a unique file path (ex: /home/user/unique-ip-address/folder/file.ini), i figure using find and sed should do the trick but i'm not sure how as the find results need to match the user to the correct folder in /mnt/tmpfs. 我正在寻找一种方法来编辑每个用户主文件夹中具有唯一文件路径(例如:/home/user/unique-ip-address/folder/file.ini)的通用.ini文件,我使用find查找和sed应该可以解决问题,但是我不确定查找结果如何将用户匹配到/ mnt / tmpfs中的正确文件夹。 I would like to append the new .sqlite location used in /dev/shm to file.ini in their home folders after dir= in the file.ini. 我想将在/ dev / shm中使用的新.sqlite位置附加到file.ini中dir =之后的主文件夹中的file.ini中。 Thanks! 谢谢!

I believe 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/}'

That command doesn't change anything, it just prints out everything. 该命令不会更改任何内容,只会打印出所有内容。 If it looks like it does what you want, just add a -i.bak to the very end and it will make the changes in place. 如果看起来像您想要的那样,只需在最后添加-i.bak即可进行更改。 The original file will be renamed to file.ini.bak. 原始文件将重命名为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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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