Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

process 3 files and substitution 1

Status
Not open for further replies.

frankli

Technical User
Apr 6, 2005
44
CA
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
 
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/"[!]old[/!][$i]"/"new[$i]"/g\" "$1[!]".old >[/!] "$1
cmd3="rm "$1".old"
[!]system([/!]cmd1[!]"[/!]; [!]"[/!]cmd2[!]"[/!]; [!]"[/!]cmd3[!])[/!]
}
' $file1 $file2 $file3

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
THANKS PH!

I ran it after modification, got below error ... still under troubleshooting.

It can mv file to file.old, all the values are in the array, when did sed it will empty the file instead of substitute the certain value ...

when I tried with the value in sed manually it works

sed -e "s/v1/v1new/g" path1/file1.old > path1/file1

not sure if there is any problem with cmd2, I need to take some time to look into it.

cmd2="sed -e \"s/"old[$i]"/"new[$i]"/g\" "$1".old > "$1


sed: -e expression #1, char 0: no previous regular expression
sed: -e expression #1, char 0: no previous regular expression
sed: -e expression #1, char 0: no previous regular expression
sed: -e expression #1, char 0: no previous regular expression
 
when I print cmd2 got

sed -e "s///g" path1/file1.old > path1/file1

it seems old[$i] new[$i] can not be passed to sed,

Any idea? Thanks!
 
forgot a pair of curly brackets, it works, however, got another problem ... some of the $v_nameX do have "/" in side, e.g. v_name8=10.1.1.2/swe/eai:2003, if have any suggestion please comment. 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/"old[$i]"/"new[$i]"/g\" "$1".old > "$1
cmd3="rm "$1".old"
system(cmd1"; "cmd2"; "cmd3)
}
}
' $file1 $file2 $file3
 
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[!]![/!]"old[$i]"[!]![/!]"new[$i]"[!]![/!]g\" "$1".old > "$1
cmd3="rm "$1".old"
system(cmd1"; "cmd2"; "cmd3)
}
}
' $file1 $file2 $file3

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top