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
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
#!/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;
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.
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.
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.