IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New 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]
Expand Edited by boxley April 27, 2005, 08:17:59 AM EDT
Expand Edited by boxley April 27, 2005, 12:08:48 PM EDT
New 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!
Expand Edited by pwhysall April 27, 2005, 11:56:51 AM EDT
New WTF are you doing up at this ungodly hour? Go back to bed!
New 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!
New Shudder.
New 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!
New 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)
New 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]
New 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)
New 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]
     scripting skills are muy rusty(edited for fsckin tttags) - (boxley) - (9)
         Flippin' 'eck, box. - (pwhysall) - (4)
             WTF are you doing up at this ungodly hour? Go back to bed! -NT - (CRConrad) - (3)
                 Up'n'at 'em, that's me! -NT - (pwhysall) - (2)
                     Shudder. -NT - (CRConrad) - (1)
                         Pervert. -NT - (pwhysall)
         Use Perl. - (ben_tilly) - (3)
             have time for some education? - (boxley) - (2)
                 Here is the same code, commented - (ben_tilly) - (1)
                     thank you very much - (boxley)

Live long and prosper.
81 ms