3

I have:

Location.txt (content)

some_line1
some_line2
some_line3

region.txt (content)

region1
region2
region3

uri.txt (content)

/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

/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)

/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.txt and fill it with lines 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.

Looks like I have to use

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

but I dont know how to use it.

Any help please.

merlin.metso
  • 107
  • 1
  • 1
  • 8
  • 1
    Where does `location.txt` come into play? You listed it in the beginning of your description and mentioned it in the following paragraph, but you don't reference it anywhere else, so it's unclear what content you are looking for in all of the `/home/user/regions/$region` files. – lurker Nov 08 '13 at 12:10
  • maybe add a small sample of some files used and the expected result in one of them. – NeronLeVelu Nov 08 '13 at 12:42
  • Sorry. I have edited my post – merlin.metso Nov 08 '13 at 13:02

4 Answers4

2

I would use awk for this:

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
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
2

@user2874004: your script, a little better written:

#!/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 :)).

gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
1

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

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!

csikos.balint
  • 1,107
  • 2
  • 10
  • 25
0

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.

merlin.metso
  • 107
  • 1
  • 1
  • 8
  • 1
    Your thing will break if there are spaces in file names. Use more quotes! In fact there's a lot of wrong stuff in your script... too many for me to explain them here in a comment. – gniourf_gniourf Nov 11 '13 at 21:03
  • Thanks for your comment. I am begginer in scripts.But I will grow :) File names are made by me so there could not be spaces. Could you explain why should I use more quotes? – merlin.metso Nov 12 '13 at 07:01
  • 1
    Just, as a habit always use quotes, even if you're sure the filenames don't contain space. This is the best habit you can take: you'll then program robustly without even thinking! – gniourf_gniourf Nov 12 '13 at 09:22