awk read in file1, gsub in file2, print to file3


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk read in file1, gsub in file2, print to file3
# 1  
Old 12-08-2013
awk read in file1, gsub in file2, print to file3

I'm trying to use awk to do the following. I have file1 with many lines, each containing 5 fields describing an individual set. I have file2 which is a template config file with variable space holders to be replaced by the values in file1. I would like to substitute each set of values in file1 with the variable place holders in file2 and then print to a unique file3 for each row in file1:
file1
Code:
sample1 300 150 100 23
sample2 320 150 90 20
sample3 340 160 95 21

file2
...
Code:
<general>
execute=var1
range=var2
sub=var3
mu=var4
sigma=var5
</general>

...

file3.1
...
<general>
Code:
execute=sample1
range=300
sub=150
mu=100
sigma=23

</general>
...

and I would end up with file3.2 and file3.3 as well.

I am close with awk
Code:
'NR==FNR{a1=$1; a2=$2; a3=$3; next} {gsub(/var1/,a1); gsub(/var2/,a2); gsub(/var3/,a3)}1' file1 file2

But it only prints out to stdout with the last record values replaced.


Thanks for any help!

Last edited by jim mcnamara; 12-08-2013 at 07:53 PM..
# 2  
Old 12-08-2013
Here is an awk approach:
Code:
awk '
        NR == FNR {
                A[++c] = $0
                next
        }
        {
                f = "file3." ++k
                print A[1] > f
                for ( i = 1; i <= NF; i++ )
                {
                                sub ( /=.*/, "="$i, A[i+1] )
                                print A[i+1] > f
                }
                print A[c] > f
                close(f)
        }
' file2 file1

# 3  
Old 12-17-2013
Thank you for your quick response and my apologies for taking so long to do the same! Your code works just as advertised for my example, but I didn't give a good explanation. I needed to expand the file2 context. Here that is:
Code:
<general>
type=all
execute=var1
num_threads=8
</general>

<detection>
split=1
window=var2
step=var3
</detection>

<filter>
filter=1
order=1
final_score_threshold=0.4
mu_length=var4
sigma_length=var5
</filter>

<colorcode>
grey = 2,2
green = 5,5
</colorcode>


Inserting var1-var5 was the best option I could think of as place holders in the template file, but whatever works.

Thanks again!

Last edited by Franklin52; 12-18-2013 at 03:26 AM.. Reason: Please use code tags
# 4  
Old 12-17-2013
Please always use code tags for posting code fragments or data samples.

Try this awk program:
Code:
awk '
        NR == FNR {
                A[++c] = $0
                next
        }
        {
                for ( i = 1; i <= NF; i++ )
                {
                        while ( ++j <= c )
                        {
                                if ( A[j] ~ /var/ )
                                {
                                        t = A[j]
                                        sub ( /=.*/, "="$i, t )
                                        print t
                                        ++i
                                }
                                else
                                {
                                        print A[j]
                                }
                        }
                }
                j = 0
        }
' file2 file1

# 5  
Old 12-17-2013
Beautiful, that did the trick! Thanks!!

Code:
awk

# 6  
Old 12-18-2013
Given your var n are found in ascending order in file2, you can avoid scanning through them for every single output line:
Code:
awk     'NR==FNR        {T[++MAX]=$0; next}
                        {vi=1; Ln=0; FN="file3."FNR
                         while (++Ln < MAX)
                          if (match (T[Ln], "var"vi)) {
                                  print substr (T[Ln], 1, RSTART-1) $vi > FN
                                  vi++                } 
                             else print T[Ln] > FN 
                         close (FN)
                        }
        ' file2 file1

# 7  
Old 01-02-2014
Thanks RudiC, works like a charm. Happy New Year!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk- Indexing a list of numbers in file2 to print certain rows in file1

Hi Does anyone know of an efficient way to index a column of data in file2 to print the coresponding row in file1 which corresponds to the data in file2 AND 30 rows preceding and after the row in file1. For example suppose you have a list of numbers in file2 (single column) as follows:... (6 Replies)
Discussion started by: Geneanalyst
6 Replies

2. Shell Programming and Scripting

awk to search field2 in file2 using range of fields file1 and using match to another field in file1

I am trying to use awk to find all the $2 values in file2 which is ~30MB and tab-delimited, that are between $2 and $3 in file1 which is ~2GB and tab-delimited. I have just found out that I need to use $1 and $2 and $3 from file1 and $1 and $2of file2 must match $1 of file1 and be in the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Shell Programming and Scripting

awk or any other means to find IP (File1 / MAC (File2)) entries and putting them on File3

Hi everyone, I would like to complete the following could you please find some time and help me to achieve below: File 1 has a list of IP address (more than 1k) File1:1.1.1.1 2.2.2.2 1.1.1.2 3.3.3.3 2.3.3.2File 2 has content like this:Internet 11.165.91.244 0 Incomplete ... (4 Replies)
Discussion started by: redred
4 Replies

4. UNIX for Dummies Questions & Answers

Compare file1 and file2, print matching lines in same order as file1

I want to print only the lines in file2 that match file1, in the same order as they appear in file 1 file1 file2 desired output: I'm getting the lines to match awk 'FNR==NR {a++}; FNR!=NR && a' file1 file2 but they are in sorted order, which is not what I want: Can anyone... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

5. UNIX for Dummies Questions & Answers

Copy(append ) the contents of file1 and file2 to file3

Platform : Oracle linux 6.5 I have two log files with the following contents # ls -l total 8 -rw-r--r--. 1 root root 75 Dec 10 20:55 myLogfile1.log -rw-r--r--. 1 root root 51 Dec 10 20:57 myLogfile2.log # # cat myLogfile1.log hello world jaded zombies acted quaintly but kept driving... (9 Replies)
Discussion started by: kraljic
9 Replies

6. Shell Programming and Scripting

Performance issue with fgrep -vf file1 file2>file3

Hi all, My requirement is that i have two files file1 and file2. file1 content is present in file2. i want to have a final which will have file2 contents but not the contents of file1 so I am running below command. fgrep -vf file1 file2>file3 no of records in file1 is 65282 no of records... (10 Replies)
Discussion started by: Lakshman_Gupta
10 Replies

7. UNIX for Dummies Questions & Answers

if matching strings in file1 and file2, add column from file1 to file2

I have very limited coding skills but I'm wondering if someone could help me with this. There are many threads about matching strings in two files, but I have no idea how to add a column from one file to another based on a matching string. I'm looking to match column1 in file1 to the number... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

8. UNIX for Dummies Questions & Answers

cat file1 file2 > file3

file1 has pgap500 500 file2 has bunch of data cat file1 file2 > file3 cp file2 file3.dat then vi pgap500 500 onto 1st line compare file3 and fil3.dat, they are not the same. any idea ? the 1st line, i want to put pg500 xxx ---------- Post updated at 07:35 AM ---------- Previous... (2 Replies)
Discussion started by: tjmannonline
2 Replies

9. Shell Programming and Scripting

AWK: read values from file1; search for values in file2

I have read another post about this issue and am wondering how to adapt it to my own, much simpler, issue. I have a file of user IDs like so: 333333 321321 546465 ...etc I need to take each number and use it to print records wherein the 5th field matches the user ID pulled from the... (2 Replies)
Discussion started by: Bubnoff
2 Replies

10. UNIX for Advanced & Expert Users

print contents of file2 for matching pattern in file1 - AWK

File1 row is same as column 2 in file 2. Also file 2 will either start with A, B or C. And 3rd column in file 2 is always F2. When column 2 of file 2 matches file1 column, print all those rows into a separate file. Here is an example. file 1: 100 103 104 108 file 2: ... (6 Replies)
Discussion started by: i.scientist
6 Replies
Login or Register to Ask a Question