Hello list,
Let me explain what I want to do first ...
the goal is to substitute several old values to several new values in a file.
the matrix file records the filename (first column) and the variables need to be substituted
cat vfile.matrix
path1/file1 v_name1 v_name2 v_name3 na v_name5 na v_name7
path2/file2 v_name1 na v_name3 na v_name5 v_name6 na
path3/file3 v_name1 v_name2 na na na na na
below two files record the value in the variable
cat venv.old
v_name1=v1
v_name2=v2
v_name3=v3
v_name4=v4
v_name5=v5
v_name6=v6
v_name7=v7
cat venv.new
v_name1=v1new
v_name2=v2new
v_name3=v3new
v_name4=v4new
v_name5=v5new
v_name6=v6new
v_name7=v7new
totally 3 files.
from what I learned from my previous post,
1. read the venv.old to an array
2. read the venv.new to another array
3. if $i!="na" use sed to substitute the value (outside awk),
problem met,
1. could not use NR==FNR, because there are 3 files, so I m thinking of using FILENAME==$file1, I understood must combine with "getline" just don't know how.
2. seems I could not run the OS command like PHV did last time, I don't know what is the trick in it.
I knew below has tons of problem, please comment so that I can start correcting it. Thanks!
file1=venv.old
file2=venv.new
file3=vfile.matrix
awk '
FILENAME==$file1 {split($0,a,"=");old[a[1]]=a[2];next}
FILENAME==$file2 {split($0,b,"=");new[b[1]]=b[2];next}
{ for(i=2;i<NF;++i)
if($i!="na")
cmd1="mv "$1" "$1".old"
cmd2="sed -e \"s/"base[$i]"/"new[$i]"/g\" "$1"\".old\" > "$1
cmd3="rm "$1".old"
cmd1; cmd2; cmd3
}
' $file1 $file2 $file3
Let me explain what I want to do first ...
the goal is to substitute several old values to several new values in a file.
the matrix file records the filename (first column) and the variables need to be substituted
cat vfile.matrix
path1/file1 v_name1 v_name2 v_name3 na v_name5 na v_name7
path2/file2 v_name1 na v_name3 na v_name5 v_name6 na
path3/file3 v_name1 v_name2 na na na na na
below two files record the value in the variable
cat venv.old
v_name1=v1
v_name2=v2
v_name3=v3
v_name4=v4
v_name5=v5
v_name6=v6
v_name7=v7
cat venv.new
v_name1=v1new
v_name2=v2new
v_name3=v3new
v_name4=v4new
v_name5=v5new
v_name6=v6new
v_name7=v7new
totally 3 files.
from what I learned from my previous post,
1. read the venv.old to an array
2. read the venv.new to another array
3. if $i!="na" use sed to substitute the value (outside awk),
problem met,
1. could not use NR==FNR, because there are 3 files, so I m thinking of using FILENAME==$file1, I understood must combine with "getline" just don't know how.
2. seems I could not run the OS command like PHV did last time, I don't know what is the trick in it.
I knew below has tons of problem, please comment so that I can start correcting it. Thanks!
file1=venv.old
file2=venv.new
file3=vfile.matrix
awk '
FILENAME==$file1 {split($0,a,"=");old[a[1]]=a[2];next}
FILENAME==$file2 {split($0,b,"=");new[b[1]]=b[2];next}
{ for(i=2;i<NF;++i)
if($i!="na")
cmd1="mv "$1" "$1".old"
cmd2="sed -e \"s/"base[$i]"/"new[$i]"/g\" "$1"\".old\" > "$1
cmd3="rm "$1".old"
cmd1; cmd2; cmd3
}
' $file1 $file2 $file3