gpt4 book ai didi

linux - 从 variable1 中查找占位符 {} sed 并放入 variable2

转载 作者:太空宇宙 更新时间:2023-11-04 11:04:24 25 4
gpt4 key购买 nike

然后我用这个脚本

for line in "`cat fromDirs.txt`"; 
do
find "$line" -type f \( -name '*good*' -o -exec grep -F "(NODES_'TASK')" {} \; \) -exec cp {} /tmp/ \;;
done

我在文件夹/tmp 中只从复制它们的地方获取文件名,但我需要这个文件名包含它们来自的完整路径,我厌倦了尝试与 sed 战斗,请帮忙

所以我只需要获取每个 {} 值并将斜杠 (/) 替换为减号 (-)

我尝试了很多变体但都不好,这段代码也不起作用

for line in "`cat fromDirs.txt`"; 
do
find "$line" -type f \( -name '*good*' -o -exec grep -F "(NODES_'TASK')" {} \; \) -exec cp {} /tmp/$(sed "s/\//-/g" <<< {}) \;;
done

来自Dirs.txt的文件包含

/home/orders/
/etc/bin/school/

没有输出,只是什么都没有,也许是因为我使用了 sh?我在系统上根本没有 bash

我认为问题出在 sed 中,因为它将占位符 {} 读取为文件而不是字符串,所以如果 {} =/home/orders/good.php 然后 sed 打开这个文件并将所有斜杠更改为减号,但我只需要在文件名中更改斜杠,所以 /home/orders/good.php -> -home-orders-good.php 然后 cp 到 /tmp/-home-orders-good.php

最佳答案

我猜你遇到了问题,因为你双引号文件的输出。
尝试更改:

for line in "`cat fromDirs.txt`";

到:

for line in `cat fromDirs.txt`; 

或更好(删除旧的和过时的背部抽动):

for line in $(cat fromDirs.txt); 

best(使用 while 读取文件):

#!/bin/bash
while read line
do
find "$line" -type f \( -name '*good*' -o -exec grep -F "(NODES_'TASK')" {} \; \) -exec cp {} /tmp/$(sed "s/\//-/g" <<< {}) \;;
done < fromDirs.txt

关于linux - 从 variable1 中查找占位符 {} sed 并放入 variable2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25825721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com