问题描述
我想在下面的代码中命名每个.tar文件,并根据包含名称的列表文件来命名它,但是我不想弄混排除标签.我有一个列表文件,当时我正在考虑使用文本编辑器,并将cvf添加到列表中每一行的开头,然后使用sed替换字符串 cvf ,从而添加标志和然后是名称.
I want to name each .tar file in the code below and name it based on a list-file that contains the names, but I don't want to mess with the exclusion tag. I have a list file and I was thinking of using a text editor and adding cvf to the beginning of each line I have in a list and then use sed to replace the string cvf, thus adding the flag and then the name follows.
即cvf名称1cvf名称2cvf名称3
我尝试使用 sed's/cvf/word2/g'input.file
,并且按预期,它仅将cvf替换为替换单词.我希望替换单词(word2)更改并成为列表文件中的每一行.
I tried using sed 's/cvf/word2/g' input.file
and as expected it only replaces cvf with the replacement word. I want the replacement word (word2) to change and to be each line from a list file.
我要修改的代码:阶段('Zip Repo340'){脚步 {sh"tar --exclude ='*.tar'-cvf .tar *"阶段('Zip Repo341'){脚步 {sh"tar --exclude ='*.tar'-cvf .tar *"阶段('Zip Repo342'){脚步 {sh"tar --exclude ='*.tar'-cvf .tar *"
我有340个这样的存储库,我想根据我拥有的列表文件来命名它们.
I have 340 of these repositories and I want to name them based on a list file that I have.
列表文件:
名称1名称2名称3
所需的输出:
阶段('Zip Repo340'){脚步 {sh"tar --exclude ='*.tar'-cvf名称1.tar *"阶段('Zip Repo341'){脚步 {sh"tar --exclude ='*.tar'-cvf name2.tar *"阶段('Zip Repo342'){脚步 {sh"tar --exclude ='*.tar'-cvf名称3.tar *"
我可以将cvf添加到列表文件的每一行,但是如果有更优雅的解决方案,我将不胜枚举.我遇到的最大问题是运行sed替换命令,并且替换单词来自列表文件.
I can add cvf to each line of my list file, but if there's a more elegant solution I'm all ears. The biggest issue I'm having is running the sed replacement command and have the replacement word come from a list file.
推荐答案
做到这一点.假设 File_1.txt
有您的代码,而 File_2.txt
具有要替换的名称.
This'll do it. Assuming File_1.txt
has your code and File_2.txt
has the names to replace.
while IFS= read -r line; do
sed -i "0,/cvf \.tar/{s|cvf \.tar|cvf ${line}\.tar|}" File_1.txt
done < File_2.txt
这是怎么回事?
-
sed -i< gobledegook>File_1.txt
我们正在对File_1.txt进行更改.有什么变化?让我们探索sed
脚本,它是< gobledegook>
-
"0,/这表示我们将搜索某物
的第一出现 -
"0,/cvf \ .tar/< gobledegook>
我们要搜索的东西将是cvf .tar
我们不得不说\.tar
是因为.
是特殊字符" -
"0,/cvf \ .tar/{s |< gobledegook>}}
在这里我们说的是,我们的搜索字符串存在,所以我们想做另一个sed脚本.sed脚本是在{}
中定义的,我决定使用|
作为第二个sed脚本的分隔符,以帮助区分两者. -
"0,/cvf \ .tar/{s | cvf \ .tar |}
我们将用 something 替换cvf .tar
em> -
"0,/cvf \ .tar/{s | cvf \ .tar | cvf $ {line} \.tar |}"
我们将替换cvf .tar
和cfv $ {line} .tar
,其中$ {line}
是File_2.txt
的当前行
sed -i <gobledegook> File_1.txt
We are making changes to File_1.txt. What changes? Lets explore thesed
script that is the<gobledegook>
"0,/<gobledegook>
This says we will be searching for the first occurrence of something"0,/cvf \.tar/<gobledegook>
That something that we will be searching for will becvf .tar
We had to say\.tar
because.
is a "special character""0,/cvf \.tar/{s|<gobledegook>}
We are saying here that on the line that our search string exists we want to do another sed script. The new sed script is defined in{}
I decided to use|
as a delimiter for the second sed script to help distinguish the two."0,/cvf \.tar/{s|cvf \.tar|}
We are going to replacecvf .tar
with something"0,/cvf \.tar/{s|cvf \.tar|cvf ${line}\.tar|}"
We are going to replacecvf .tar
withcfv ${line}.tar
where${line}
is the current line fromFile_2.txt
这篇关于用列表文件的每一行替换每次出现的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!