Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BASH echo with sed

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.

like image 615
merlin.metso Avatar asked Nov 18 '25 11:11

merlin.metso


1 Answers

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
like image 166
glenn jackman Avatar answered Nov 21 '25 02:11

glenn jackman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!