用列表文件的每一行替换每次出现的单词 [英] Replace every occurrence of a word with each line from an list-file

查看:39
本文介绍了用列表文件的每一行替换每次出现的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在下面的代码中命名每个.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

这是怎么回事?

  1. sed -i< gobledegook>File_1.txt 我们正在对File_1.txt进行更改.有什么变化?让我们探索 sed 脚本,它是< gobledegook>
  2. "0,/这表示我们将搜索某物
  3. 第一出现
  4. "0,/cvf \ .tar/< gobledegook> 我们要搜索的东西将是 cvf .tar 我们不得不说 \.tar 是因为.是特殊字符"
  5. "0,/cvf \ .tar/{s |< gobledegook>}} 在这里我们说的是,我们的搜索字符串存在,所以我们想做另一个sed脚本.sed脚本是在 {} 中定义的,我决定使用 | 作为第二个sed脚本的分隔符,以帮助区分两者.
  6. "0,/cvf \ .tar/{s | cvf \ .tar |} 我们将用 something 替换 cvf .tar em>
  7. "0,/cvf \ .tar/{s | cvf \ .tar | cvf $ {line} \.tar |}" 我们将替换 cvf .tar cfv $ {line} .tar ,其中 $ {line} File_2.txt
  8. 的当前行
  1. sed -i <gobledegook> File_1.txt We are making changes to File_1.txt. What changes? Lets explore the sed script that is the <gobledegook>
  2. "0,/<gobledegook> This says we will be searching for the first occurrence of something
  3. "0,/cvf \.tar/<gobledegook> That something that we will be searching for will be cvf .tar We had to say \.tar because . is a "special character"
  4. "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.
  5. "0,/cvf \.tar/{s|cvf \.tar|} We are going to replace cvf .tar with something
  6. "0,/cvf \.tar/{s|cvf \.tar|cvf ${line}\.tar|}" We are going to replace cvf .tar with cfv ${line}.tar where ${line} is the current line from File_2.txt

查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆