3

我有:

Location.txt(内容)

some_line1
some_line2
some_line3

region.txt(内容)

region1
region2
region3

uri.txt(内容)

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

我需要创建文件

region1
region2
region3

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

文件 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

文件 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

我现在有

#!/bin/bash

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

此脚本创建具有行名称的文件,region.txt并用来自 的行填充它uri.txt

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

看起来我必须使用

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

但我不知道如何使用它。

请提供任何帮助。

4

4 回答 4

2

@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

(不过,我不知道它的作用:))。

于 2013-11-11T21:16:42.907 回答
2

我会为此使用 awk:

awk -v loc="$(< Location.txt)" '
    NR==FNR {region[$1]=1;next}
    {for (reg in region) {
        sub(/\*\*\*/, reg)
        f = reg ".txt"
        print > f
        print loc > f
    }}
' region.txt uri.txt
于 2013-11-08T14:16:22.253 回答
1

如果我正确理解了您的问题,那么 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

结果:

$ 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

我希望这个能帮上忙!

于 2013-11-08T13:47:39.723 回答
0

最后我使用了以下,它的工作原理。

#!/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

谢谢大家的帮助。

于 2013-11-11T20:44:20.080 回答