replace the contents of first column of file "X" with second Column of file "X" in file "Y"

Hi!
I am having 02 files.
In first file" X" I am having 02 Columns

TCP-5100 Sybase_5100
TCP-5600 Sybase_5600

Second file "Y" for example--

:services (
				:AdminInfo (
					:chkpf_uid ("{A2F79713-B67D-4409-83A4-A90804E983E9}")
					:ClassName (rule_services)
				)
				:compound ()
				:op ()
				: (ReferenceObject
					:Name (TCP-5100)
					:Table (services)
					:Uid ("{A262C8D2-A98F-4AD4-9534-F52C1603475D}")
				)
				: (ReferenceObject
					:Name (TCP-5600)
					:Table (services)
					:Uid ("{6E2BFB06-5AF2-4F71-921C-924CD7074C21}")
				)
				: (ReferenceObject
					:Name (telnet)
					:Table (services)
					:Uid ("{97AEB3CF-9AEA-11D5-BD16-0090272CCB30}")

I want to make Changes in an second file by replacing TCP-5100 with Sybase_5100 and TCP-5600 with Sybase_5600.
To elaborate, I want to replace the contents of first column of file "X"with second Column of file "X" in file "Y".
This has to be done for 1000 entries of file X in file Y.
Any code in awk or sed

Hi try something like:

awk 'NR==FNR{A[$1]=$2; next} /:Name/ && $2 in A { $2="(" A[$2] ")"}1' X FS='[)(]' OFS="" Y
1 Like

Thanks
Will this code continue for till 1000 records of X file. Will it introduce any space or extra lines
I have checked for 5 records.
Thanks again:b:

---------- Post updated at 12:34 AM ---------- Previous update was at 12:13 AM ----------

It is including 1 extra line
the output file is having extra line with the replaced element.
Is it to do with input file X as it is having spaces after each row.
The file is config file and it will stop working

Try also

awk 'FNR==NR {T["(" $1 ")"]=$2; next} $2 in T {sub ($2,T[$2])} 1' X Y
1 Like

Both the codes are moving ")" to next line
ex

Name (TCP-5600)

has been changed to

Name (Sybase_5600
)

Please help

---------- Post updated at 01:14 AM ---------- Previous update was at 01:03 AM ----------

last replacement is ok i.e. without any extra lines.
I think it is bcos after the last line in X file there is no line breaks.

---------- Post updated at 11:59 AM ---------- Previous update was at 01:14 AM ----------

code in file to be replaced say "Y"
code->

:compound ()
				:op ()
				: (ReferenceObject
					:Name (MAS-PRS-Router-Zone_A-Primary)
					:Table (network_objects)
					:Uid ("{B3246F7C-D590-4417-8F9E-11340A8AD510}")
				)
				: (ReferenceObject
					:Name (CHK_MGMT-PR)
					:Table (network_objects)
					:Uid ("{317BF66F-A2C6-1B45-95DE-8C622D2062F7}")
				)

Second file "X"with replacement to. First Column is "Object to be Searched" Second Column is "Object to be replaced with"
code->

MAS-PRS-Router-Zone_A-Primary A_Primary
CHK_MGMT-PR Madras-MGMT_Primary
B3246F7C-D590-4417-8F9E-11340A8AD510 60146ACB-1B56-47F0-B14E-035EED4D81D6
317BF66F-A2C6-1B45-95DE-8C622D2062F7 27BECF52-869A-4127-9AC4-530EFD70D696

--------
There ar 1000 objects to be replaced in Name & UID field of file "Y"

---------- Post updated at 02:15 PM ---------- Previous update was at 11:59 AM ----------

It is first time I am posting.
I will make a note of it in future.
Please reply to my Query.

Here is an adaptation to the script in post #2. See if this works out for you:

awk 'NR==FNR{A[$1]=$2; next} $2 in A{sub($2,A[$2])}1' X FS='[(]("{)?|(}")[)]' OFS="" Y
1 Like

Perhaps a bit more robust alternative.

#!/usr/bin/perl

#shahid1632.pl
use strict;
use warnings;

# get 2 files from command line or exist
my $xfile = shift || die;
my $yfile = shift || die;

# load patterns from first file into a hash database
my %patterns;
open my $fh, '<', $xfile or die "Could not open $xfile: $!\n";
while(<$fh>){
    chomp;
    my ($key, $val) = split;
    $patterns{$key} = $val;
}
close $fh;

# act upon second file
open $fh, '<', $yfile or die "Could not open $yfile: $!\n";
while(<$fh>){
    # create alphanumeric with hyphens tokens from line
    my @blocks = /([\w-]+)/g;
    # the first token is Name or Uid, check for a possible swap of value
    if($blocks[1] and $blocks[0]=~/^Name|^Uid/){
       s/$blocks[1]/$patterns{$blocks[1]}/ if exists $patterns{$blocks[1]};
    }
    print;
}
close $fh;
cat file.x2
MAS-PRS-Router-Zone_A-Primary A_Primary
CHK_MGMT-PR Madras-MGMT_Primary
B3246F7C-D590-4417-8F9E-11340A8AD510 60146ACB-1B56-47F0-B14E-035EED4D81D6
317BF66F-A2C6-1B45-95DE-8C622D2062F7 27BECF52-869A-4127-9AC4-530EFD70D696
cat file.y2
:compound ()
                :op ()
                : (ReferenceObject
                    :Name (MAS-PRS-Router-Zone_A-Primary)
                    :Table (network_objects)
                    :Uid ("{B3246F7C-D590-4417-8F9E-11340A8AD510}")
                )
                : (ReferenceObject
                    :Name (CHK_MGMT-PR)
                    :Table (network_objects)
                    :Uid ("{317BF66F-A2C6-1B45-95DE-8C622D2062F7}")
                )

Run as:

perl shahid1632.pl file.x2 file.y2
:compound ()
                :op ()
                : (ReferenceObject
                    :Name (A_Primary)
                    :Table (network_objects)
                    :Uid ("{60146ACB-1B56-47F0-B14E-035EED4D81D6}")
                )
                : (ReferenceObject
                    :Name (Madras-MGMT_Primary)
                    :Table (network_objects)
                    :Uid ("{27BECF52-869A-4127-9AC4-530EFD70D696}")
                )
1 Like

tried for perl script also but of no use.

If any one can help me with changing the UIDs only without changing the format of the file.

---------- Post updated at 01:27 AM ---------- Previous update was at 01:03 AM ----------

why can't we search a string in a file and substitute with a string from another file.
Why is it we have to make a regular expression containing Name / Uid.
why can't a string be searched line by line in a file without any dependency.

I am sorry the Perl script is of no use to you. If you post in which way it failed I might be able to know why.

The answer to all your "why(s)" is this. I do not know how your real data looks like, I can only go by what you have posted, and as I have shown you in post #7,
according to the information you have given, the result is as requested. You do not want to allow the substitution to occur in lines that it is not intended for, inadvertently.
If you think that's the problem, please, just remove the extra condition:

Change if($blocks[1] and $blocks[0]=~/^Name|^Uid/){ to if($blocks[1]){

Please, show us how the format is changed. My script does not change any format. It leaves spaces and symbols and newlines as found.

To reply to your three "why"s:
1) !t can be and has been done; any of the solutions provided does exactly this.
2) For safety reasons. If you're absolutely sure there won't be any false positives, the name/uid check can be dropped.
3) see above.

As the solutions provided DO work when tested locally, I suspect your file has unusual structures, e.g. hidden characters like (but not limited to) the DOS <CR> (0x0D, \r, ^M) line terminators.

Thanks all of you, but i found a simple solution to my problem :

Firstly I added

 sed -i -e 's/ column 1 of file 'X' / column 2 of file 'X'/g' file name Y 

to all the rows in the excel file X. Then made a text file X.
Then made a script myscript and executed ./myscript

Hi shahid1632

You never mentioned before that file X was an excel file. If this means that it came from a msdos world, your file X does need to have the hidden \r removed. Before using it, it should be ran through something like:

tr -d '\r' < fileX > patternsX

Now you can use file patternsX with any of the scripts previously given to you.

Concerning your "found" solution:

You do not need the g at the end of the sed expression, unless you expect column 1 of file 'X' to appear several times in the same line in file name Y

Also, the only way that it might have worked is if you manually created an entry for each of the 1000 patterns in file X , which begs the question why not to manually do the replacement in file Y . It does not make any sense to fabricate 1000 shovels to dig a 1000 holes.

Sorry, Its my fault. I didn't told you that

 the file X, contains many occurrences of column 1 value.

Second, The file X was a text file which was manually created by copying the contents from excel file.
Third, as you must be aware that in excel there is an option of dragging the contents to the last row so there was negligible effort involved in making the mysript from already file X which was in excel.
Thanks to all of you once again.
For the sake of other readers you can wrap up by posting the perl script once again.