Afficher un message
Vieux 05/09/2007, 22h57   #16
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Better use of awk/sed?

Salve Håkedal wrote:
> On 2007-09-05, Chris F.A. Johnson <cfajohnson@gmail.com> wrote:
>
>>On 2007-09-05, Salve Håkedal wrote:
>>
>>>I script a simple accounting system using the old Spreadsheet calculator
>>>program SC. The following (part of a) script is to be called with one or
>>>more of these filenames:
>>>$ script 1st_term.cln 2nd_term.cln 3rd_term.cln 4th_term.cln
>>>
>>>#!/bin/bash
>>>accounts='
>>>INNTEKTSKONTOER
>>>~~~~~~~~~~~~~~~
>>> 11 Reparasjon/Varer
>>> 12 Nytt instrument
>>> 13 Utleie
>>> 16 Eksport
>>>
>>>UDGIFTSKONTOER
>>>~~~~~~~~~~~~~~
>>>Vareinnkjøb
>>> 21 Import
>>> 22 Import u/mva
>>> 25 Norske varer
>>>
>>>Lokaleomkostninger
>>> 31 Faste udgifter
>>> 33 Rep. og vedl.
>>>
>>>Kontordrift
>>> 41 Trykksaker o.l.
>>> 42 Trykksaker u/mva
>>> 43 Kontorudstyr
>>> 45 Dataudstyr
>>> 47 Nettsted/Reklame
>>>
>>>Diverse
>>> 51 Porto/Frakt
>>> 53 Bankgebyr
>>> 55 Utleggsprovisjon
>>> 57 Kontingenter
>>> 60 Diverse u/mva
>>>'
>>>echo "$accounts"
>>>read -p 'type account number > ' acc_nr
>>>
>>>echo "$(echo "$accounts"\
>>> | awk '/'$acc_nr'/ { print $2, $3, $4 }'\
>>> | sed 's/[ ]*$//') for $(echo $@\
>>> | sed 's/_term\.cln/,/g') term"\
>>> | sed 's/\(, \)\([1-6][a-z][a-z]*\), term/ and \2 term/'\
>>> | sed 's/, term/ term/'\
>>>
>>>_____________________
>>>
>>>This does exactly what I want it to do, but I'm shure the last 6 lines
>>>can be written much better! I want to use only bash / awk / sed.

>>
>>echo "$accounts" |
>> awk "/ $acc_nr / { printf \"%s for term\n\", substr(\$0,5) }"
>>

>
> I don't think that does what I want it to.
> The script-part of my posting can be cut out run, to see what I want,
> but here is an explanation:
>
> If I run the script with one filename:
> $ script.sh 1st_term.cln
> And give for example "11" to the prompt,
> the return is:
> $ Reparasjon/Varer for 1st term
>
> If I run the script with two filenames:
> $ script.sh 1st_term.cln 2nd_term.cln
> And give for example "11" to the prompt,
> the return is:
> $ Reparasjon/Varer for 1st and 2nd term
>
> If I run the script with three filenames:
> $ script.sh 1st_term.cln 2nd_term.cln 3rd_term.cln
> And give for example "11" to the prompt,
> the return is:
> $ Reparasjon/Varer for 1st, 2nd and 3rd term
>


OK, then do this:

account=$(echo "$accounts" | awk -v pat="^ $acc_nr " 'sub(pat,"")')
files=$(echo "$@" | awk -v sfx="_term.cln" '
{sub(sfx" "$NF"$"," and "$NF);gsub(sfx" ",", ");sub(sfx," term")}1')
echo "$account for $files"

Regards,

Ed.
  Réponse avec citation
 
Page generated in 0,10681 seconds with 9 queries