Checking input, diff(ing) output, checking against and fixing
Checking input, diff(ing) output, checking against and fixing
am 13.09.2007 13:37:56 von dsphunxion
I currently have two files
#sed -n '1p' fileA
1234,56
#sed -n '1p' fileB
exten => 1234,1,Dial(Zap/57/r,20)
I need to be able to take (line by line) the data from fileA and make
sure 1234 and 56 match in fileB and if not fix it. So fileB would read
as 1234,1,Dial(Zap/56/r,20)
I got it to work in a butchered sed to awk to paste do diff to perl to
sh one/two/three liner that butchered my eyes... Was wondering if
someone had a method to do this more effectively.
Input:
fileA
1234,56
1122,34
2233,56
fileB
exten => 1234,1,Dial(Zap/57/r,20)
exten => 1122,1,Dial(Zap/35/r,20)
exten => 2233,1,Dial(Zap/56/r,20)
Output:
fileC (unless I can inline edit fileB via perl -pi -e or so then a new
file isn't needed)
exten => 1234,1,Dial(Zap/56/r,20)
exten => 1122,1,Dial(Zap/34/r,20)
PITA trying to change about 4000+ telephone extensions and making sure
they are where they need to be. Note 2233 was ommitted since there was
nothing wrong with it. I did what I needed to do, was a pain though so
I'm just wondering how others would have handled something like this
for the learning experience. My solution consisted of sed, awk, paste,
diff, back to awk with a perl -pi -e embedded in print piped to sh
awk '{print "perl -pi -e '\''s/"$1,$2,$3"/"$4,$5,$6"/g'\'' fileB"}'
mismatched |grep -vi "/ / /"|sh
Where mismatched was the output of lines that differed in files fileA
and fileB
Re: Checking input, diff(ing) output, checking against and fixing
am 13.09.2007 14:16:04 von Janis Papanagnou
On 13 Sep., 13:37, sil wrote:
> I currently have two files
>
> #sed -n '1p' fileA
> 1234,56
>
> #sed -n '1p' fileB
> exten => 1234,1,Dial(Zap/57/r,20)
>
> I need to be able to take (line by line) the data from fileA and make
> sure 1234 and 56 match in fileB and if not fix it. So fileB would read
> as 1234,1,Dial(Zap/56/r,20)
>
> I got it to work in a butchered sed to awk to paste do diff to perl to
> sh one/two/three liner that butchered my eyes... Was wondering if
> someone had a method to do this more effectively.
>
> Input:
>
> fileA
> 1234,56
> 1122,34
> 2233,56
>
> fileB
> exten => 1234,1,Dial(Zap/57/r,20)
> exten => 1122,1,Dial(Zap/35/r,20)
> exten => 2233,1,Dial(Zap/56/r,20)
>
> Output:
> fileC (unless I can inline edit fileB via perl -pi -e or so then a new
> file isn't needed)
> exten => 1234,1,Dial(Zap/56/r,20)
> exten => 1122,1,Dial(Zap/34/r,20)
>
> PITA trying to change about 4000+ telephone extensions and making sure
> they are where they need to be. Note 2233 was ommitted since there was
> nothing wrong with it. I did what I needed to do, was a pain though so
> I'm just wondering how others would have handled something like this
> for the learning experience.
I might have used GNU awk, for something like...
BEGIN { FS="[,/]| => " }
NR==FNR { a[$1]="/"$2"/"; next }
## To print all lines (modified or not):
# ($2 in a) && sub(/\/.*\//,a[$2])
## or to print just the non-matching modified lines:
($2 in a) && !match($0,a[$2]) { sub(/\/.*\//,a[$2]); print }
Janis
> My solution consisted of sed, awk, paste,
> diff, back to awk with a perl -pi -e embedded in print piped to sh
>
> awk '{print "perl -pi -e '\''s/"$1,$2,$3"/"$4,$5,$6"/g'\'' fileB"}'
> mismatched |grep -vi "/ / /"|sh
>
> Where mismatched was the output of lines that differed in files fileA
> and fileB
Re: Checking input, diff(ing) output, checking against and fixing
am 13.09.2007 15:22:12 von Ed Morton
sil wrote:
> I currently have two files
>
> #sed -n '1p' fileA
> 1234,56
>
> #sed -n '1p' fileB
> exten => 1234,1,Dial(Zap/57/r,20)
>
> I need to be able to take (line by line) the data from fileA and make
> sure 1234 and 56 match in fileB and if not fix it. So fileB would read
> as 1234,1,Dial(Zap/56/r,20)
>
> I got it to work in a butchered sed to awk to paste do diff to perl to
> sh one/two/three liner that butchered my eyes... Was wondering if
> someone had a method to do this more effectively.
>
> Input:
>
> fileA
> 1234,56
> 1122,34
> 2233,56
>
> fileB
> exten => 1234,1,Dial(Zap/57/r,20)
> exten => 1122,1,Dial(Zap/35/r,20)
> exten => 2233,1,Dial(Zap/56/r,20)
>
> Output:
> fileC (unless I can inline edit fileB via perl -pi -e or so then a new
> file isn't needed)
> exten => 1234,1,Dial(Zap/56/r,20)
> exten => 1122,1,Dial(Zap/34/r,20)
>
> PITA trying to change about 4000+ telephone extensions and making sure
> they are where they need to be. Note 2233 was ommitted since there was
> nothing wrong with it. I did what I needed to do, was a pain though so
> I'm just wondering how others would have handled something like this
> for the learning experience. My solution consisted of sed, awk, paste,
> diff, back to awk with a perl -pi -e embedded in print piped to sh
>
> awk '{print "perl -pi -e '\''s/"$1,$2,$3"/"$4,$5,$6"/g'\'' fileB"}'
> mismatched |grep -vi "/ / /"|sh
>
> Where mismatched was the output of lines that differed in files fileA
> and fileB
>
awk -F'[ ,/]' 'NR==FNR{a[$3","$6];next} !($0 in a){printf "exten =>
%s,1,Dial(Zap/%s/r,20)\n",$1,$2}' fileB fileA
Regards,
Ed.
Re: Checking input, diff(ing) output, checking against and fixing
am 13.09.2007 16:36:17 von dsphunxion
On Sep 13, 9:22 am, Ed Morton wrote:
> sil wrote:
> > I currently have two files
>
> > #sed -n '1p' fileA
> > 1234,56
>
> > #sed -n '1p' fileB
> > exten => 1234,1,Dial(Zap/57/r,20)
>
> > I need to be able to take (line by line) the data from fileA and make
> > sure 1234 and 56 match in fileB and if not fix it. So fileB would read
> > as 1234,1,Dial(Zap/56/r,20)
>
> > I got it to work in a butchered sed to awk to paste do diff to perl to
> > sh one/two/three liner that butchered my eyes... Was wondering if
> > someone had a method to do this more effectively.
>
> > Input:
>
> > fileA
> > 1234,56
> > 1122,34
> > 2233,56
>
> > fileB
> > exten => 1234,1,Dial(Zap/57/r,20)
> > exten => 1122,1,Dial(Zap/35/r,20)
> > exten => 2233,1,Dial(Zap/56/r,20)
>
> > Output:
> > fileC (unless I can inline edit fileB via perl -pi -e or so then a new
> > file isn't needed)
> > exten => 1234,1,Dial(Zap/56/r,20)
> > exten => 1122,1,Dial(Zap/34/r,20)
>
> > PITA trying to change about 4000+ telephone extensions and making sure
> > they are where they need to be. Note 2233 was ommitted since there was
> > nothing wrong with it. I did what I needed to do, was a pain though so
> > I'm just wondering how others would have handled something like this
> > for the learning experience. My solution consisted of sed, awk, paste,
> > diff, back to awk with a perl -pi -e embedded in print piped to sh
>
> > awk '{print "perl -pi -e '\''s/"$1,$2,$3"/"$4,$5,$6"/g'\'' fileB"}'
> > mismatched |grep -vi "/ / /"|sh
>
> > Where mismatched was the output of lines that differed in files fileA
> > and fileB
>
> awk -F'[ ,/]' 'NR==FNR{a[$3","$6];next} !($0 in a){printf "exten =>
> %s,1,Dial(Zap/%s/r,20)\n",$1,$2}' fileB fileA
>
> Regards,
>
> Ed.- Hide quoted text -
>
> - Show quoted text -
[self@home tmp]# cat fileA fileB
4455,678
exten => 4455,1,Dial(Zap/678/r,20)
[self@home tmp]# awk -F'[ ,/]' 'NR==FNR{a[$3","$6];next} !($0 in a)
{printf "exten => %s,1,Dial(Zap/%s/r,20)\n",$1,$2}' fileA fileB
exten => exten,1,Dial(Zap/=>/r,20)
Janis:
[self@home tmp]# cat fileA fileB
4455,678
exten => 4455,1,Dial(Zap/678/r,20)
[self@home tmp]# gawk 'BEGIN { FS="[,/]| => " } NR==FNR
{ a[$1]="/"$2"/"; next } ($2 in a) && !match($0,a[$2]) { sub(/\/.*
\//,a[$2]); print }' fileA fileB
[self@home tmp]#
Re: Checking input, diff(ing) output, checking against and fixing
am 13.09.2007 16:39:29 von Ed Morton
sil wrote:
> On Sep 13, 9:22 am, Ed Morton wrote:
>
>>sil wrote:
>>
>>>I currently have two files
>>
>>>#sed -n '1p' fileA
>>>1234,56
>>
>>>#sed -n '1p' fileB
>>>exten => 1234,1,Dial(Zap/57/r,20)
>>
>>>I need to be able to take (line by line) the data from fileA and make
>>>sure 1234 and 56 match in fileB and if not fix it. So fileB would read
>>>as 1234,1,Dial(Zap/56/r,20)
>>
>>>I got it to work in a butchered sed to awk to paste do diff to perl to
>>>sh one/two/three liner that butchered my eyes... Was wondering if
>>>someone had a method to do this more effectively.
>>
>>>Input:
>>
>>>fileA
>>>1234,56
>>>1122,34
>>>2233,56
>>
>>>fileB
>>>exten => 1234,1,Dial(Zap/57/r,20)
>>>exten => 1122,1,Dial(Zap/35/r,20)
>>>exten => 2233,1,Dial(Zap/56/r,20)
>>
>>>Output:
>>>fileC (unless I can inline edit fileB via perl -pi -e or so then a new
>>>file isn't needed)
>>>exten => 1234,1,Dial(Zap/56/r,20)
>>>exten => 1122,1,Dial(Zap/34/r,20)
>>
>>>PITA trying to change about 4000+ telephone extensions and making sure
>>>they are where they need to be. Note 2233 was ommitted since there was
>>>nothing wrong with it. I did what I needed to do, was a pain though so
>>>I'm just wondering how others would have handled something like this
>>>for the learning experience. My solution consisted of sed, awk, paste,
>>>diff, back to awk with a perl -pi -e embedded in print piped to sh
>>
>>>awk '{print "perl -pi -e '\''s/"$1,$2,$3"/"$4,$5,$6"/g'\'' fileB"}'
>>>mismatched |grep -vi "/ / /"|sh
>>
>>>Where mismatched was the output of lines that differed in files fileA
>>>and fileB
>>
>>awk -F'[ ,/]' 'NR==FNR{a[$3","$6];next} !($0 in a){printf "exten =>
>>%s,1,Dial(Zap/%s/r,20)\n",$1,$2}' fileB fileA
>>
>>Regards,
>>
>> Ed.- Hide quoted text -
>>
>>- Show quoted text -
>
>
>
> [self@home tmp]# cat fileA fileB
> 4455,678
> exten => 4455,1,Dial(Zap/678/r,20)
> [self@home tmp]# awk -F'[ ,/]' 'NR==FNR{a[$3","$6];next} !($0 in a)
> {printf "exten => %s,1,Dial(Zap/%s/r,20)\n",$1,$2}' fileA fileB
> exten => exten,1,Dial(Zap/=>/r,20)
I said:
awk '...' fileB fileA
not:
awk '...' fileA fileB
> Janis:
>
> [self@home tmp]# cat fileA fileB
> 4455,678
> exten => 4455,1,Dial(Zap/678/r,20)
> [self@home tmp]# gawk 'BEGIN { FS="[,/]| => " } NR==FNR
> { a[$1]="/"$2"/"; next } ($2 in a) && !match($0,a[$2]) { sub(/\/.*
> \//,a[$2]); print }' fileA fileB
> [self@home tmp]#
>
>
>
Re: Checking input, diff(ing) output, checking against and fixing
am 13.09.2007 16:55:58 von dsphunxion
On Sep 13, 10:39 am, Ed Morton wrote:
> I said:
>
> awk '...' fileB fileA
>
> not:
>
> awk '...' fileA fileB
*puts on the dunce cap and sits in the corner*
Works so much better than the eyebusting sed 's/\//\\\//g;s/\t/\//g;s/
$/\/g/g;s/^/perl \-pi \-e '\''s\//g;s/$/'\'' */g;s/(/\Dial\(/g;s/)/\\)/
g' via awk through paste through etc., etc.
If I posted what *I DID* to get it working... Some of you might hunt
me down to hit me with an awk stick. Thanks Mr. Morton, Janis
Re: Checking input, diff(ing) output, checking against and fixing
am 13.09.2007 17:06:49 von Joachim Schmitz
"sil" schrieb im Newsbeitrag
news:1189695358.727478.168830@22g2000hsm.googlegroups.com...
> On Sep 13, 10:39 am, Ed Morton wrote:
>
>> I said:
>>
>> awk '...' fileB fileA
>>
>> not:
>>
>> awk '...' fileA fileB
>
> *puts on the dunce cap and sits in the corner*
>
> Works so much better than the eyebusting sed 's/\//\\\//g;s/\t/\//g;s/
> $/\/g/g;s/^/perl \-pi \-e '\''s\//g;s/$/'\'' */g;s/(/\Dial\(/g;s/)/\\)/
> g' via awk through paste through etc., etc.
>
> If I posted what *I DID* to get it working... Some of you might hunt
> me down to hit me with an awk stick. Thanks Mr. Morton, Janis
sed expressions get much easier if you use some other separator than '/',
e.g. ':':
sed 's:/:\/:g;s:\t:/:g;s:$:/g:g;s .....
Bye, Jojo
Re: Checking input, diff(ing) output, checking against and fixing
am 13.09.2007 18:32:39 von William James
On Sep 13, 6:37 am, sil wrote:
> I currently have two files
>
> #sed -n '1p' fileA
> 1234,56
>
> #sed -n '1p' fileB
> exten => 1234,1,Dial(Zap/57/r,20)
>
> I need to be able to take (line by line) the data from fileA and make
> sure 1234 and 56 match in fileB and if not fix it. So fileB would read
> as 1234,1,Dial(Zap/56/r,20)
>
> I got it to work in a butchered sed to awk to paste do diff to perl to
> sh one/two/three liner that butchered my eyes... Was wondering if
> someone had a method to do this more effectively.
>
> Input:
>
> fileA
> 1234,56
> 1122,34
> 2233,56
>
> fileB
> exten => 1234,1,Dial(Zap/57/r,20)
> exten => 1122,1,Dial(Zap/35/r,20)
> exten => 2233,1,Dial(Zap/56/r,20)
>
> Output:
> fileC (unless I can inline edit fileB via perl -pi -e or so then a new
> file isn't needed)
> exten => 1234,1,Dial(Zap/56/r,20)
> exten => 1122,1,Dial(Zap/34/r,20)
>
> PITA trying to change about 4000+ telephone extensions and making sure
> they are where they need to be. Note 2233 was ommitted since there was
> nothing wrong with it. I did what I needed to do, was a pain though so
> I'm just wondering how others would have handled something like this
> for the learning experience. My solution consisted of sed, awk, paste,
> diff, back to awk with a perl -pi -e embedded in print piped to sh
>
> awk '{print "perl -pi -e '\''s/"$1,$2,$3"/"$4,$5,$6"/g'\'' fileB"}'
> mismatched |grep -vi "/ / /"|sh
>
> Where mismatched was the output of lines that differed in files fileA
> and fileB
#!ruby
ext = Hash[ *gets(nil).split(/\D+/) ]
ARGF.each{|s|
a = s.split( /(\D+)/ )
if ext[ a[2] ] != a[6]
a[6] = ext[ a[2] ]
puts a.join
end }