BASH 与 sed 的回声

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19858538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:24:39  来源:igfitidea点击:

BASH echo with sed

bashsedwhile-loopecho

提问by merlin.metso

I have:

我有:

Location.txt (content)

Location.txt(内容)

some_line1
some_line2
some_line3

region.txt (content)

region.txt(内容)

region1
region2
region3

uri.txt (content)

uri.txt(内容)

/some_text/some_text1/***/
/some_text/***/some_text2/
/***/some_text/some_text3/

I need to create files

我需要创建文件

region1
region2
region3

and each of them have to fill with the following (name of the region instead of stars)

并且他们每个人都必须填写以下内容(地区名称而不是星星)

Example of file region1

文件 region1 示例

/some_text/some_text1/region1/
some_line1
some_line2
some_line3

/some_text/region1/some_text2
some_line1
some_line2
some_line3

/region1/some_text/some_text3
some_line1
some_line2
some_line3

Example of file region2 (name of the region instead of stars)

文件 region2 示例(区域名称而不是星星)

/some_text/some_text1/region2/
some_line1
some_line2
some_line3

/some_text/region2/some_text2
some_line1
some_line2
some_line3

/region2/some_text/some_text3
some_line1
some_line2
some_line3

Now I have

我现在有

#!/bin/bash

while read region
    do
        while read uri
        do
            echo $uri >> "/home/user/regions/$region"
        done < uri.txt
    done < region.txt

This script creates files with names of lines from region.txtand fill it with lines from uri.txt.

此脚本使用 from 的行名称创建文件,region.txt并使用 from的行填充它uri.txt

But I have to make a lot of files. Each file should be filled with many locations but one line in each location should be changed to line from uri.txt.

但是我必须制作很多文件。每个文件应填充多个位置,但每个位置中的一行应更改为 line from uri.txt

Looks like I have to use

看起来我必须使用

cat $file|sed 's/^was/now/'

but I dont know how to use it.

但我不知道如何使用它。

Any help please.

请任何帮助。

采纳答案by csikos.balint

If I understand correctly your problem than 3 nested for cycle can do the job.

如果我正确理解您的问题,那么 3 个嵌套 for 循环就可以完成这项工作。

for reg in $(cat region.txt )
do
  echo -n > $reg.txt
    for uri in $(cat uri.txt )
    do
      echo "$uri" | sed -e "s/\*\*\*/$reg/g" | tee -a $reg.txt
        for loc in $(cat Location.txt )
        do
          echo $loc | tee -a $reg.txt
        done
    done
done

Result:

结果:

$ cat region1.txt 
/some_text/some_text1/region1/
some_line1
some_line2
some_line3
/some_text/region1/some_text2/
some_line1
some_line2
some_line3
/region1/some_text/some_text3/
some_line1
some_line2
some_line3

I hope this will help!

我希望这个能帮上忙!

回答by glenn Hymanman

I would use awk for this:

我会为此使用 awk:

awk -v loc="$(< Location.txt)" '
    NR==FNR {region[]=1;next}
    {for (reg in region) {
        sub(/\*\*\*/, reg)
        f = reg ".txt"
        print > f
        print loc > f
    }}
' region.txt uri.txt

回答by gniourf_gniourf

@user2874004: your script, a little better written:

@user2874004:你的脚本,写得更好一点:

#!/bin/bash

mkdir -p configs # make directory for configs if it doesnt exist

while IFS= read -r reg # read next line from reg.txt
do
    while IFS= read -r uri # read next line from uri.txt
    do
        echo "    ${uri//\*\*\*/$reg} {" # make changes
        while IFS= read -r loc # read next line from location.txt
        do
            echo "        $loc" # fill in the location 
        done < location.txt
    done < uri.txt > "./config/$reg.txt"
done < region.txt

(I have no idea what it does, though :)).

(虽然我不知道它是做什么的:))。

回答by merlin.metso

Finaly I used the following, and it works.

最后我使用了以下内容,它有效。

#!/bin/bash

mkdir -p configs # make directory for configs if it doesnt exist

while read reg # read next line from reg.txt
do
  if [ -f ./configs/$reg.txt ]; then # if file allready exist then 
    rm ./configs/$reg.txt # delete it
  fi
    while read  uri # read next line from uri.txt
    do
        echo "    $uri {" | sed -e "s/\*\*\*/$reg/g"  >> ./configs/$reg$ # make changes
        while read loc # read next line from location.txt
        do
                echo "        $loc" >> ./configs/$reg.txt # fill in the location 
        done < location.txt
    done < uri.txt
done < region.txt

Thank you all for your help.

谢谢大家的帮助。