LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
LinkBack Search this Thread
Old 09-16-2013, 11:44 AM   #1
123raajesh
LQ Newbie
 
Registered: Sep 2013
Posts: 12

Rep: Reputation: Disabled
match source pattern in destination and substitute in destination usin AWK


Please help with this. I have two files rspfile and myfile as below:

rspfile

#
wsdm_USERID=
#
WASGRP_GROUPID=
#
ND_HOSTNAME=
#
BASE_NODENAME=
#
BASE_HOSTNAME=
#
APP_NAME_1=
#APP_NAME_2=
#APP_NAME_3=
#
CLUSTER_NAME_1=
#CLUSTER_NAME_2=
#CLUSTER_NAME_3=


myfile

wsdm_USERID=500
WASGRP_GROUPID=500
ND_HOSTNAME=abcdefgh.was.tb.dsdc
BASE_NODENAME=abcdefgh
BASE_HOSTNAME=abcdefgh.was.tb.dsdc
APP_NAME_1=CCCCC01DD
APP_NAME_2=CCCCC02DD
CLUSTER_NAME_1=CCC01DD
CLUSTER_NAME_2=CCC02DD

Using awk like to have result as below:

rspfile

#
wsdm_USERID=500
#
WASGRP_GROUPID=500
#
ND_HOSTNAME=abcdefgh.was.tb.dsdc
#
BASE_NODENAME=abcdefgh
#
BASE_HOSTNAME=abcdefgh.was.tb.dsdc
#
APP_NAME_1=CCCCC01DD
APP_NAME_2=CCCCC02DD
#APP_NAME_3=

#
CLUSTER_NAME_1=CCC01DD
CLUSTER_NAME_2=CCC02DD
#CLUSTER_NAME_3=

I tried this using sed but its lengthy.
sed -e '/wsdm_USERID=/ c wsdm_USERID=500' -e '/WASGRP_GROUPID=/ c WASGRP_GROUPID=500' . . . /home/welcome/rspfile

Please suggest thanks in advance.
 
Old 09-16-2013, 02:09 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,360

Rep: Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909
Code:
awk 'BEGIN { FS = OFS = "=" } FNR == NR { _[$1] = $2 } FNR < NR { sub(/^#/,"",$1); if ( $1 in _ ) print $1, _[$1]; else print "#" $0 }' myfile rspfile
The expression FNR == NR ensures that the rule is applied only to the first file (myfile) whereas the expression FNR < NR ensures the rule is applied to the second file. Here is a more readable version of the code:
Code:
awk 'BEGIN {

  FS = OFS = "="

}

FNR == NR {

  _[$1] = $2 

}

FNR < NR {

  sub(/^#/,"",$1)

  if ( $1 in _ )
    print $1, _[$1]
  else
    print "#" $0

}' myfile rspfile
 
1 members found this post helpful.
Old 09-17-2013, 01:05 PM   #3
123raajesh
LQ Newbie
 
Registered: Sep 2013
Posts: 12

Original Poster
Rep: Reputation: Disabled
match source pattern in destination and substitute in destination using AWK

Hi Colucix,

Thanks it worked for me. Could I know where I can learn AWK?

Like to add more my query.
1) How can I update the result in destination file itself(rspfile).
2) Could it be possible to restrict to update the rspfile, if the file is already updated with values = $2.

I would still be interested to learn awk.

Thank you
 
Old 09-17-2013, 02:34 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,360

Rep: Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909
Quote:
Originally Posted by 123raajesh View Post
1) How can I update the result in destination file itself(rspfile).
Inside the awk program you can redirect the output of the print statements to FILENAME, which is an internal variable containing the name of the file that awk is processing:
Code:
  ...
  if ( $1 in _ )
    print $1, _[$1] > FILENAME
  else
    print "#" $0 > FILENAME
  ...
However this doesn't work for long input files (I don't know what is the limit, anyway). In that case you have to redirect the output to a new file and then rename it as the original input file.
Quote:
2) Could it be possible to restrict to update the rspfile, if the file is already updated with values = $2.
Yes. You have to add a further condition, that is if the second field is not empty, then update the value, otherwise print the line without modification (see the part added in red)
Code:
  ...
  if ( $1 in _ )
    if ( ! $2 )
      print $1, _[$1]
    else
      print
  else
    print "#" $0
  ...
Finally to learn awk I suggest the official GNU Awk User's Guide, where the first 11 chapters are the basics, the rest is somewhat for advanced users.
 
1 members found this post helpful.
Old 09-18-2013, 01:01 PM   #5
123raajesh
LQ Newbie
 
Registered: Sep 2013
Posts: 12

Original Poster
Rep: Reputation: Disabled
Hi colucix,

Thank you very much.
 
Old 09-18-2013, 02:50 PM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,360

Rep: Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909
You're welcome!
 
1 members found this post helpful.
Old 11-13-2013, 12:13 AM   #7
123raajesh
LQ Newbie
 
Registered: Sep 2013
Posts: 12

Original Poster
Rep: Reputation: Disabled
[SOLVED] match source pattern in destination and substitute in destination usin AWK

Hi colucix,

Have a query as u mentioned earlier the limit of input file matters. Now I have same issue, I have a file of 604L, 16481C and the its failing.

Could you please suggest any other way to get the result?

Thanks

Raajesh
 
Old 11-13-2013, 04:22 AM   #8
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,435
Blog Entries: 11

Rep: Reputation: 181Reputation: 181
You could use split to get smaller files
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] printing a degenerate pattern match in awk? rare_aquatic_badger Programming 5 03-13-2012 02:13 PM
simple pattern match with awk, sed alenD Linux - Newbie 10 03-10-2008 02:31 PM
destination unavailable W/ vpn. All other net destination o.k. MikeOfAustin Linux - Networking 1 04-07-2007 04:42 PM
iptables - source ? destination ? From where ? Dek Linux - Networking 3 04-30-2003 11:43 PM
Martion Source/Destination bfloeagle Linux - Networking 2 06-13-2001 06:42 AM


All times are GMT -5. The time now is 02:23 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration