unix ksh: replace

Hi experts, a question on replacing (mass change).

A file holds definitions like this:


workstation#jobname1
script: -job jobname1 -user username1 -jobid jobnumber1

workstation#jobnameX
script: -job jobnameX -user usernameX -jobid jobnumberX

...




Jobname1 on the first line needs replaced and with that also the -job value, the -user value, the -jobid value on the second line.
JobnameX on the first line needs replaced and with that also the values connected to it on the second line.
Etc.


Those new values are read from a space seperated file.


oldname newname newuser newjobid
jobname1 jobname2 username2 jobnumber2
jobnameX jobnameY usernameY jobnumberY
...

Please check if I can hope to get this achieved.
This is related to this question:
http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Q_27490923.html

As always, yours gratefully.
WatnogAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MikeOM_DBACommented:



Assuming the definitions are in file "def.txt" and the new values in file chg.txt, you could try this:

awk 'BEGIN{ while ((getline line[++i] < "chg.txt") > 0)}
{
 out=$0
 for (i in line){
  split(line[i],c);
  if (out ~ c[1]){
    if($1 ~ /work/)
      gsub(c[1],c[2],out);
    else
      out="script -job "c[2]" -user "c[3]" -jobid "c[4];
    }
  }
 print out;
}' <def.txt

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WatnogAuthor Commented:
Thanks, but

workstation#jobname1
script: -job jobname1 -user username1 -jobid jobnumber1

becomes

script: -job jobname1 -user username1 -jobid jobnumber1
script: -job jobname1 -user username1 -jobid jobnumber1

simon3270 got pretty far on this in this ticket:
http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Q_27490923.html
Your help there is welcome too (awk exact match).

Thanks for  your efforts.
0
MikeOM_DBACommented:

Mine works:
==> cat def.txt
workstation#jobname1
script -job jobname1 -user username1 -jobid jobnumber1

workstation#jobnameX
script -job jobnameX -user usernameX -jobid jobnumberX

==> cat chg.txt
#oldname newname newuser newjobid
jobname1 jobname2 username2 jobnumber2
jobnameX jobnameY usernameY jobnumberY

==> cat k0
#
awk 'BEGIN{ while ((getline line[++i] < "chg.txt") > 0)}
{
 out=$0
 for (i in line){
  split(line[i],c);
  if (out ~ c[1]){
    if($1 ~ /work/)
      gsub(c[1],c[2],out);
    else
      out="script -job "c[2]" -user "c[3]" -jobid "c[4];
    }
  }
 print out;
}' <def.txt

==> ./k0
workstation#jobname2
script -job jobname2 -user username2 -jobid jobnumber2

workstation#jobnameY
script -job jobnameY -user usernameY -jobid jobnumberY

Open in new window

0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

WatnogAuthor Commented:
Not for me I'm afraid.
There something I did not tell, and that might be what confuses your code.
The full definiton is:

PR2-028#MMEX-US-M-ADR20-INVY-0048
 SCRIPTNAME "/  -job MMEX-US-M-ADR20-INVY-0048 -user US022211 -i 10404301 -c C"
 STREAMLOGON maestro
 DESCRIPTION "ATS PLANT 0962 MONTHLY RECON JOB"
 TASKTYPE SAP
 RECOVERY STOP

So consists of 6 lines not just 2.
THe lines below SCRIPTNAME do not change, that's why I omitted them.

The out put I get is like this:

script -job MMMM-US-D-USSALES-DIST-UPDATE -user VNXT3299 -jobid 5005000
script -job MMMM-US-D-USSALES-DIST-UPDATE -user VNXT3299 -jobid 5005000
 STREAMLOGON maestro
 TASKTYPE SAP
 RECOVERY STOP
0
MikeOM_DBACommented:

Don't know what you are doing, but it still works for me:

==> cat def.txt
PR2-001#JOBNAME1
SCRIPTNAME "/  -job JOBNAME1 -user USERNAME1 -i JOBNUMBER1  -c C"
STREAMLOGON maestro
DESCRIPTION "ATS PLANT 0961 MONTHLY RECON JOB"
TASKTYPE SAP
RECOVERY STOP

PR2-00X#JOBNAMEX
SCRIPTNAME "/  -job JOBNAMEX -user USERNAMEX -i JOBNUMBER1X -c C"
STREAMLOGON maestro
DESCRIPTION "ATS PLANT 096X MONTHLY RECON JOB"
TASKTYPE SAP
RECOVERY STOP

==> cat chg.txt
oldname newname newuser newjobid
JOBNAME1 JOBNAME2 USERNAME2 JOBNUMBER2
JOBNAMEX JOBNAMEY USERNAMEY JOBNUMBERY

==> cat k0
#
awk 'BEGIN{ while ((getline line[++i] < "chg.txt") > 0)}
{
 out=$0
 for (i in line){
  split(line[i],c);
  if (out ~ c[1]){
    if($1 ~ /^SCRIPTNAME/)
      out="SCRIPTNAME \"/  -job "c[2]" -user "c[3]" -i "c[4]" -c C\"";
    else
      gsub(c[1],c[2],out);
    }
  }
 print out;
}' <def.txt


==> ./k0
PR2-001#JOBNAME2
SCRIPTNAME "/  -job JOBNAME2 -user USERNAME2 -i JOBNUMBER2 -c C"
STREAMLOGON maestro
DESCRIPTION "ATS PLANT 0961 MONTHLY RECON JOB"
TASKTYPE SAP
RECOVERY STOP

PR2-00X#JOBNAMEY
SCRIPTNAME "/  -job JOBNAMEY -user USERNAMEY -i JOBNUMBERY -c C"
STREAMLOGON maestro
DESCRIPTION "ATS PLANT 096X MONTHLY RECON JOB"
TASKTYPE SAP
RECOVERY STOP

Open in new window

0
WatnogAuthor Commented:
Thank you very much.
This is doing a very good job.
:-D
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.