首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >逐行读取并替换字符串递归

逐行读取并替换字符串递归
EN

Stack Overflow用户
提问于 2020-03-25 14:09:28
回答 1查看 307关注 0票数 0

我有一个文件,其中包含多个lines.Here示例

etc/list.txt

代码语言:javascript
复制
MLA1234,MLA2345
MLA1235,MLA2345

我需要在文件夹中搜索递归,搜索这个字符串和remplace。示例

etc/json/other/file2.json

代码语言:javascript
复制
{id:"MLA1234" , product_name:"blablabla"

取代string MLA1234的预期前哨

etc/json/其他/file2.json(最终)

代码语言:javascript
复制
{id:"MLA2345" , product_name:"blablabla"

我做了这样的事,但没有对手

代码语言:javascript
复制
#!/bin/bash

csvfile='list.txt'

# Make sure the file exists
if [ ! -f $csvfile ]
then
    echo "ERROR: file $csvfile does not exist."
    exit 1
fi

# Read the input file line per line
while read line
do

 # Capture the information out of the line, fields separated by ;
    IFS=',' read old new <<< $line



grep -rl $old . | xargs sed -i "s/$oldstring/$new/g"
EN

回答 1

Stack Overflow用户

发布于 2020-03-25 16:12:50

他为我工作!我修好密码了!

代码语言:javascript
复制
#!/bin/bash

csvfile='list.txt'

# Make sure the file exists
if [ ! -f $csvfile ]
then
    echo "ERROR: file $csvfile does not exist."
    exit 1
fi

# Read the input file line per line
sed '1d' $csvfile | while read line
do
    # Capture the information out of the line, fields separated by ;
    IFS=',' read new old  <<< $line


grep -rl "$old" . | xargs sed -i "s/$old/$new/g"


done```
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60850563

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档