Post #204,898
4/26/05 10:34:16 PM
4/27/05 12:08:48 PM
|
scripting skills are muy rusty(edited for fsckin tttags)
having been fixing scripts the last few years instead of writing the have a test file that contains the following
foo=bigasslong name foo1=3 foo2=12 foo3=7 foo4=8
now I need to take a value from foo1 and stuff it into foo4. case statements in ksh work sorts
ksh on read line case foo) echo $line >>foo.out foo1) echo "foo4=`cat $line| cut -d: -f2`" >>foo.out esac done <foo.in ksh off
this allows me to reload the file foo.out with new (corrected) values for foo4
but if there is no foo1 I need to insert the contents of foo3 or foo2 if foo1 is not available instead. Think I need to get into a snakes nest of nested if statements? best practices? my choices are shells and perl 5.4 thanx, bill
All tribal myths are true, for a given value of "true" Terry Pratchett [link|http://boxleys.blogspot.com/|http://boxleys.blogspot.com/]
Any opinions expressed by me are mine alone, posted from my home computer, on my own time as a free american and do not reflect the opinions of any person or company that I have had professional relations with in the past 48 years. meep questions, help? [link|mailto:pappas@catholic.org|email pappas at catholic.org]
Edited by boxley
April 27, 2005, 08:17:59 AM EDT
Edited by boxley
April 27, 2005, 12:08:48 PM EDT
|
Post #204,906
4/27/05 12:55:04 AM
4/27/05 11:56:51 AM
|
Flippin' 'eck, box.
At least wrap your code fragments in TT tags so that we can tell where the ksh ends and the box begins.
USE THE TT TAG. IT IS YOUR FRIEND. THE TT TAG LOVES YOU.
Peter [link|http://www.ubuntulinux.org|Ubuntu Linux] [link|http://www.kuro5hin.org|There is no K5 Cabal] [link|http://guildenstern.dyndns.org|Home] Use P2P for legitimate purposes!
Edited by pwhysall
April 27, 2005, 11:56:51 AM EDT
|
Post #204,907
4/27/05 12:56:59 AM
|
WTF are you doing up at this ungodly hour? Go back to bed!
|
Post #204,909
4/27/05 1:03:58 AM
|
Up'n'at 'em, that's me!
Peter [link|http://www.ubuntulinux.org|Ubuntu Linux] [link|http://www.kuro5hin.org|There is no K5 Cabal] [link|http://guildenstern.dyndns.org|Home] Use P2P for legitimate purposes!
|
Post #204,911
4/27/05 1:11:52 AM
|
Shudder.
|
Post #204,913
4/27/05 1:32:39 AM
|
Pervert.
Peter [link|http://www.ubuntulinux.org|Ubuntu Linux] [link|http://www.kuro5hin.org|There is no K5 Cabal] [link|http://guildenstern.dyndns.org|Home] Use P2P for legitimate purposes!
|
Post #204,957
4/27/05 2:27:08 PM
|
Use Perl.
Are they always in order and the values guaranteed to be true? If so, then the following hack will work: \nperl -pe 'if (/^foo(\\d+)=(.*\\n)/) {$r ||= $2; $_="foo4=$r" if 4==$1;}' testdata\n If the values are not guaranteed to be true, then you have to check whether $r is defined before assigning to it. If they are not always in order then I'd need to run 2 passes and would want to write a simple script to do it. Cheers, Ben
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
|
Post #205,134
4/28/05 2:45:30 PM
|
have time for some education?
perl -pe 'if (/^foo(\\d+)=(.*\\n)/) {$r ||= $2; $_="foo4=$r" if 4==$1;}' testdata notice I am using ttffp tags now I want to be sure I clearly understand what is going on. I throw my file at this string perl -pPrint -eEdit single tick to resolve the following enclosed actions
if (what is the / before the first foo? ^foo followed by a decimal place holder and has a .* followed by a newline and the closing /? $r is replace || (why the 2 pipes? $2 being the second field $_"foo4=$r(eplaced value) if 4 is exactly equal to $1 the first field. ending tick
corrections and information gladly accepted, thanx, bill
All tribal myths are true, for a given value of "true" Terry Pratchett [link|http://boxleys.blogspot.com/|http://boxleys.blogspot.com/]
Any opinions expressed by me are mine alone, posted from my home computer, on my own time as a free american and do not reflect the opinions of any person or company that I have had professional relations with in the past 48 years. meep questions, help? [link|mailto:pappas@catholic.org|email pappas at catholic.org]
|
Post #205,169
4/28/05 6:47:41 PM
|
Here is the same code, commented
\n#!/usr/bin/perl -p\n# The -p switch, as explained in perlrun, reads each\n# line into $_, runs your code, then prints $_.\n# More precisely it puts a loop like this around your\n# code:\n#\n# LINE:\n# while (<>) {\n# ... # your program goes here\n# } continue {\n# print or die "-p destination: $!\\n";\n# }\n#\n# The -p switch allows you to edit lines on the fly by\n# editing $_.\n#\n# Previously I used -e to allow the program to go on\n# the command line, but this time around I am not\n# doing that.\n\nif (\n / # This regular expression matches:\n ^ # the start of the string\n foo # then foo\n (\\d+) # then some digits, put in $1\n = # an = sign\n (.*\\n) # other junk, including a return\n # that go into $2.\n /x # /x turns on commenting.\n # (since no variable is bound to the\n # match it matches $_ - which is\n # conveniently your current line.)\n) {\n # OK, we matched. If we have not yet come up with\n # a replacement for foo4, then set it to $2.\n $r ||= $2;\n # If this is foo4, then replace $_ (the new value is\n # what we'll print).\n $_ = "foo4$r" if 4 == $1;\n}\n Cheers, Ben
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
|
Post #205,178
4/28/05 7:58:31 PM
|
thank you very much
knowing how it works sort of is no where near as good as knowing exactly why it works so can put that to use elsewhere, thanx, bill
All tribal myths are true, for a given value of "true" Terry Pratchett [link|http://boxleys.blogspot.com/|http://boxleys.blogspot.com/]
Any opinions expressed by me are mine alone, posted from my home computer, on my own time as a free american and do not reflect the opinions of any person or company that I have had professional relations with in the past 48 years. meep questions, help? [link|mailto:pappas@catholic.org|email pappas at catholic.org]
|