PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Better use of awk/sed?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Better use of awk/sed?

Réponse
 
LinkBack Outils de la discussion
Vieux 05/09/2007, 19h06   #1
Salve Håkedal
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Better use of awk/sed?

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.
--
Salve Håkedal
  Réponse avec citation
Vieux 05/09/2007, 19h54   #2
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Better use of awk/sed?

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

<snip>
> 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.


Let's try to clean that up a bit to try to figure out what it does.

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/'

Hmm. No, I still don't entirely get it. Let's simplify and correct it a bit:

account=$(echo "$accounts" |
awk -v acc_nr="$acc_nr" '$1==acc_nr{print $2, $3, $4}')
files=$(echo $@ | sed 's/_term\.cln/,/g')
echo "$account for $files term" |
sed -e 's/\(, \)\([1-6][a-z][a-z]*\), term/ and \2 term/ \
-e 's/, term/ term/'

That last sed is a puzzle. You seem to be inserting commas on the first
sed then taking them out again on the second one. I THINK what you want
is to take your list of input files, change the suffix to a comma and
add "and" between the final 2 files plus a terminating word "term". If
so, all you need is:


account=$(echo "$accounts" |
awk -v acc_nr="$acc_nr" '$1==acc_nr{print $2, $3, $4}')
files=$(echo "$@" |
awk '{$NF="and " $NF " term";gsub(/_term\.cln/,",")}1')
echo "$account for $files"

Untested. If it doesn't work, provide some details on what you're really
trying to do.

Ed.
  Réponse avec citation
Vieux 05/09/2007, 20h05   #3
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Better use of awk/sed?

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) }"

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
  Réponse avec citation
Vieux 05/09/2007, 20h05   #4
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Better use of awk/sed?

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) }"

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
  Réponse avec citation
Vieux 05/09/2007, 20h23   #5
Salve Håkedal
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Better use of awk/sed?

On 2007-09-05, Ed Morton <morton@lsupcaemnt.com> wrote:
> 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

><snip>
>> 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.

>
> Let's try to clean that up a bit to try to figure out what it does.


Please just try to run the script-portion of my posting, and you'll see
what it does, I think!

>
> 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/'
>
> Hmm. No, I still don't entirely get it. Let's simplify and correct it
> a bit:
>
> account=$(echo "$accounts" |
> awk -v acc_nr="$acc_nr" '$1==acc_nr{print $2, $3, $4}')
> files=$(echo $@ | sed 's/_term\.cln/,/g')
> echo "$account for $files term" |
> sed -e 's/\(, \)\([1-6][a-z][a-z]*\), term/ and \2 term/ \
> -e 's/, term/ term/'
>
> That last sed is a puzzle. You seem to be inserting commas on the first
> sed then taking them out again on the second one. I THINK what you want
> is to take your list of input files, change the suffix to a comma and
> add "and" between the final 2 files plus a terminating word "term". If
> so, all you need is:


Yes, and if there is only one input file, there should be no comma or
"and".

>
>
> account=$(echo "$accounts" |
> awk -v acc_nr="$acc_nr" '$1==acc_nr{print $2, $3, $4}')
> files=$(echo "$@" |
> awk '{$NF="and " $NF " term";gsub(/_term\.cln/,",")}1')
> echo "$account for $files"
>
> Untested. If it doesn't work, provide some details on what you're really
> trying to do.


As stated above, I _can_ do it whith my script above, but I think it can be
done with better code than I have used! But thank you!

>
> Ed.

  Réponse avec citation
Vieux 05/09/2007, 20h43   #6
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, Ed Morton <morton@lsupcaemnt.com> wrote:
>
>>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

>>
>><snip>
>>
>>>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.

>>
>>Let's try to clean that up a bit to try to figure out what it does.

>
>
> Please just try to run the script-portion of my posting, and you'll see
> what it does, I think!


Not unless I run it multiple times passing the various combinations of
arguments and input values that you expect. It's much easier for you to
just tell us what it does than have us try to figure it out from reading
the code and coming up with multiple sample inputs to test it and from
those test results synthesize the purpose of the script.

>
>> 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/'
>>
>>Hmm. No, I still don't entirely get it. Let's simplify and correct it
>>a bit:
>>
>> account=$(echo "$accounts" |
>> awk -v acc_nr="$acc_nr" '$1==acc_nr{print $2, $3, $4}')
>> files=$(echo $@ | sed 's/_term\.cln/,/g')
>> echo "$account for $files term" |
>> sed -e 's/\(, \)\([1-6][a-z][a-z]*\), term/ and \2 term/ \
>> -e 's/, term/ term/'
>>
>>That last sed is a puzzle. You seem to be inserting commas on the first
>>sed then taking them out again on the second one. I THINK what you want
>>is to take your list of input files, change the suffix to a comma and
>>add "and" between the final 2 files plus a terminating word "term". If
>>so, all you need is:

>
>
> Yes, and if there is only one input file, there should be no comma or
> "and".


OK, now that's useful.

>
>>
>> account=$(echo "$accounts" |
>> awk -v acc_nr="$acc_nr" '$1==acc_nr{print $2, $3, $4}')
>> files=$(echo "$@" |
>> awk '{$NF="and " $NF " term";gsub(/_term\.cln/,",")}1')
>> echo "$account for $files"
>>
>>Untested. If it doesn't work, provide some details on what you're really
>>trying to do.

>
>
> As stated above, I _can_ do it whith my script above, but I think it can be
> done with better code than I have used! But thank you!


I understand that and I think the code I provided is what you're looking
for. Now that you've added a detail, I'd modify it to:

account=$(echo "$accounts" |
awk -v acc_nr="$acc_nr" '$1==acc_nr{print $2, $3, $4}')
files=$(echo "$@" |
awk -v sfx="_term\.cln" '{sub(sfx"$"," term")}
NF > 1 {gsub(sfx" ",", ");$NF="and "$NF} 1')
echo "$account for $files"

Regards,

Ed.
  Réponse avec citation
Vieux 05/09/2007, 20h43   #7
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, Ed Morton <morton@lsupcaemnt.com> wrote:
>
>>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

>>
>><snip>
>>
>>>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.

>>
>>Let's try to clean that up a bit to try to figure out what it does.

>
>
> Please just try to run the script-portion of my posting, and you'll see
> what it does, I think!


Not unless I run it multiple times passing the various combinations of
arguments and input values that you expect. It's much easier for you to
just tell us what it does than have us try to figure it out from reading
the code and coming up with multiple sample inputs to test it and from
those test results synthesize the purpose of the script.

>
>> 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/'
>>
>>Hmm. No, I still don't entirely get it. Let's simplify and correct it
>>a bit:
>>
>> account=$(echo "$accounts" |
>> awk -v acc_nr="$acc_nr" '$1==acc_nr{print $2, $3, $4}')
>> files=$(echo $@ | sed 's/_term\.cln/,/g')
>> echo "$account for $files term" |
>> sed -e 's/\(, \)\([1-6][a-z][a-z]*\), term/ and \2 term/ \
>> -e 's/, term/ term/'
>>
>>That last sed is a puzzle. You seem to be inserting commas on the first
>>sed then taking them out again on the second one. I THINK what you want
>>is to take your list of input files, change the suffix to a comma and
>>add "and" between the final 2 files plus a terminating word "term". If
>>so, all you need is:

>
>
> Yes, and if there is only one input file, there should be no comma or
> "and".


OK, now that's useful.

>
>>
>> account=$(echo "$accounts" |
>> awk -v acc_nr="$acc_nr" '$1==acc_nr{print $2, $3, $4}')
>> files=$(echo "$@" |
>> awk '{$NF="and " $NF " term";gsub(/_term\.cln/,",")}1')
>> echo "$account for $files"
>>
>>Untested. If it doesn't work, provide some details on what you're really
>>trying to do.

>
>
> As stated above, I _can_ do it whith my script above, but I think it can be
> done with better code than I have used! But thank you!


I understand that and I think the code I provided is what you're looking
for. Now that you've added a detail, I'd modify it to:

account=$(echo "$accounts" |
awk -v acc_nr="$acc_nr" '$1==acc_nr{print $2, $3, $4}')
files=$(echo "$@" |
awk -v sfx="_term\.cln" '{sub(sfx"$"," term")}
NF > 1 {gsub(sfx" ",", ");$NF="and "$NF} 1')
echo "$account for $files"

Regards,

Ed.
  Réponse avec citation
Vieux 05/09/2007, 20h47   #8
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Better use of awk/sed?

Chris F.A. Johnson wrote:

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


Don't use double quotes to encapsulate awk scripts, it's not worth the
grief when you forget to escape every other important character. This is
one way you could really do the above:

echo "$accounts" |
awk -v acc_nr=" $acc_nr " '$0 ~ acc_nr { printf "%s for term\n",
substr($0,5) }'

Regards,

Ed.
  Réponse avec citation
Vieux 05/09/2007, 20h47   #9
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Better use of awk/sed?

Chris F.A. Johnson wrote:

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


Don't use double quotes to encapsulate awk scripts, it's not worth the
grief when you forget to escape every other important character. This is
one way you could really do the above:

echo "$accounts" |
awk -v acc_nr=" $acc_nr " '$0 ~ acc_nr { printf "%s for term\n",
substr($0,5) }'

Regards,

Ed.
  Réponse avec citation
Vieux 05/09/2007, 20h51   #10
Salve Håkedal
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Better use of awk/sed?

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

--
Salve Håkedal
Thanks for trying!
  Réponse avec citation
Vieux 05/09/2007, 20h52   #11
Salve Håkedal
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Better use of awk/sed?

On 2007-09-05, Ed Morton <morton@lsupcaemnt.com> wrote:
> Salve Håkedal wrote:
>> On 2007-09-05, Ed Morton <morton@lsupcaemnt.com> wrote:
>>
>>>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
>>>
>>><snip>
>>>
>>>>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.
>>>
>>>Let's try to clean that up a bit to try to figure out what it does.

>>
>>
>> Please just try to run the script-portion of my posting, and you'll see
>> what it does, I think!

>
> Not unless I run it multiple times passing the various combinations of
> arguments and input values that you expect. It's much easier for you to
> just tell us what it does than have us try to figure it out from reading
> the code and coming up with multiple sample inputs to test it and from
> those test results synthesize the purpose of the script.
>
>>
>>> 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/'
>>>
>>>Hmm. No, I still don't entirely get it. Let's simplify and correct it
>>>a bit:
>>>
>>> account=$(echo "$accounts" |
>>> awk -v acc_nr="$acc_nr" '$1==acc_nr{print $2, $3, $4}')
>>> files=$(echo $@ | sed 's/_term\.cln/,/g')
>>> echo "$account for $files term" |
>>> sed -e 's/\(, \)\([1-6][a-z][a-z]*\), term/ and \2 term/ \
>>> -e 's/, term/ term/'
>>>
>>>That last sed is a puzzle. You seem to be inserting commas on the first
>>>sed then taking them out again on the second one. I THINK what you want
>>>is to take your list of input files, change the suffix to a comma and
>>>add "and" between the final 2 files plus a terminating word "term". If
>>>so, all you need is:

>>
>>
>> Yes, and if there is only one input file, there should be no comma or
>> "and".

>
> OK, now that's useful.
>
>>
>>>
>>> account=$(echo "$accounts" |
>>> awk -v acc_nr="$acc_nr" '$1==acc_nr{print $2, $3, $4}')
>>> files=$(echo "$@" |
>>> awk '{$NF="and " $NF " term";gsub(/_term\.cln/,",")}1')
>>> echo "$account for $files"
>>>
>>>Untested. If it doesn't work, provide some details on what you're really
>>>trying to do.

>>
>>
>> As stated above, I _can_ do it whith my script above, but I think it can be
>> done with better code than I have used! But thank you!

>
> I understand that and I think the code I provided is what you're looking
> for. Now that you've added a detail, I'd modify it to:
>
> account=$(echo "$accounts" |
> awk -v acc_nr="$acc_nr" '$1==acc_nr{print $2, $3, $4}')
> files=$(echo "$@" |
> awk -v sfx="_term\.cln" '{sub(sfx"$"," term")}
> NF > 1 {gsub(sfx" ",", ");$NF="and "$NF} 1')
> echo "$account for $files"
>
> Regards,
>
> Ed.

OK. Se my answer to Cris!

--
Salve
  Réponse avec citation
Vieux 05/09/2007, 20h52   #12
Salve Håkedal
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Better use of awk/sed?

On 2007-09-05, Ed Morton <morton@lsupcaemnt.com> wrote:
> Salve Håkedal wrote:
>> On 2007-09-05, Ed Morton <morton@lsupcaemnt.com> wrote:
>>
>>>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
>>>
>>><snip>
>>>
>>>>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.
>>>
>>>Let's try to clean that up a bit to try to figure out what it does.

>>
>>
>> Please just try to run the script-portion of my posting, and you'll see
>> what it does, I think!

>
> Not unless I run it multiple times passing the various combinations of
> arguments and input values that you expect. It's much easier for you to
> just tell us what it does than have us try to figure it out from reading
> the code and coming up with multiple sample inputs to test it and from
> those test results synthesize the purpose of the script.
>
>>
>>> 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/'
>>>
>>>Hmm. No, I still don't entirely get it. Let's simplify and correct it
>>>a bit:
>>>
>>> account=$(echo "$accounts" |
>>> awk -v acc_nr="$acc_nr" '$1==acc_nr{print $2, $3, $4}')
>>> files=$(echo $@ | sed 's/_term\.cln/,/g')
>>> echo "$account for $files term" |
>>> sed -e 's/\(, \)\([1-6][a-z][a-z]*\), term/ and \2 term/ \
>>> -e 's/, term/ term/'
>>>
>>>That last sed is a puzzle. You seem to be inserting commas on the first
>>>sed then taking them out again on the second one. I THINK what you want
>>>is to take your list of input files, change the suffix to a comma and
>>>add "and" between the final 2 files plus a terminating word "term". If
>>>so, all you need is:

>>
>>
>> Yes, and if there is only one input file, there should be no comma or
>> "and".

>
> OK, now that's useful.
>
>>
>>>
>>> account=$(echo "$accounts" |
>>> awk -v acc_nr="$acc_nr" '$1==acc_nr{print $2, $3, $4}')
>>> files=$(echo "$@" |
>>> awk '{$NF="and " $NF " term";gsub(/_term\.cln/,",")}1')
>>> echo "$account for $files"
>>>
>>>Untested. If it doesn't work, provide some details on what you're really
>>>trying to do.

>>
>>
>> As stated above, I _can_ do it whith my script above, but I think it can be
>> done with better code than I have used! But thank you!

>
> I understand that and I think the code I provided is what you're looking
> for. Now that you've added a detail, I'd modify it to:
>
> account=$(echo "$accounts" |
> awk -v acc_nr="$acc_nr" '$1==acc_nr{print $2, $3, $4}')
> files=$(echo "$@" |
> awk -v sfx="_term\.cln" '{sub(sfx"$"," term")}
> NF > 1 {gsub(sfx" ",", ");$NF="and "$NF} 1')
> echo "$account for $files"
>
> Regards,
>
> Ed.

OK. Se my answer to Cris!

--
Salve
  Réponse avec citation
Vieux 05/09/2007, 21h15   #13
Salve Håkedal
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Better use of awk/sed?

On 2007-09-05, Ed Morton <morton@lsupcaemnt.com> wrote:
> 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

><snip>
>> 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.

>
> Let's try to clean that up a bit to try to figure out what it does.
>
> 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/'
>
> Hmm. No, I still don't entirely get it. Let's simplify and correct it a bit:
>
> account=$(echo "$accounts" |
> awk -v acc_nr="$acc_nr" '$1==acc_nr{print $2, $3, $4}')
> files=$(echo $@ | sed 's/_term\.cln/,/g')
> echo "$account for $files term" |
> sed -e 's/\(, \)\([1-6][a-z][a-z]*\), term/ and \2 term/ \
> -e 's/, term/ term/'


The last line is for taking the comma away if there is only one
file-name given as argument.

>
> That last sed is a puzzle. You seem to be inserting commas on the first
> sed then taking them out again on the second one. I THINK what you want
> is to take your list of input files, change the suffix to a comma and
> add "and" between the final 2 files plus a terminating word "term". If
> so, all you need is:
>
>
> account=$(echo "$accounts" |
> awk -v acc_nr="$acc_nr" '$1==acc_nr{print $2, $3, $4}')
> files=$(echo "$@" |
> awk '{$NF="and " $NF " term";gsub(/_term\.cln/,",")}1')
> echo "$account for $files"
>
> Untested. If it doesn't work, provide some details on what you're really
> trying to do.
>
> Ed.

  Réponse avec citation
Vieux 05/09/2007, 21h28   #14
Glenn Jackman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Better use of awk/sed?

At 2007-09-05 02:06PM, "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='

[...]
> '
> 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/'\



account=$(echo "$accounts" | awk -v a="$acc_nr" '$1 == a {print substr($0,5)}')
if [ -z "$account" ]; then
printf "no such account: %s\n" "$acc_nr"
else
terms=$(echo "$@" | sed -e 's/_term.cln//g' \
-e 's/ /, /g' \
-e 's/^\(.*\),/\1 and/' )
printf "%s for %s term\n" "$account" "$terms"
fi


--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
  Réponse avec citation
Vieux 05/09/2007, 21h28   #15
Glenn Jackman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Better use of awk/sed?

At 2007-09-05 02:06PM, "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='

[...]
> '
> 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/'\



account=$(echo "$accounts" | awk -v a="$acc_nr" '$1 == a {print substr($0,5)}')
if [ -z "$account" ]; then
printf "no such account: %s\n" "$acc_nr"
else
terms=$(echo "$@" | sed -e 's/_term.cln//g' \
-e 's/ /, /g' \
-e 's/^\(.*\),/\1 and/' )
printf "%s for %s term\n" "$account" "$terms"
fi


--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
  Réponse avec citation
Vieux 05/09/2007, 21h57   #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
Vieux 05/09/2007, 21h57   #17
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
Vieux 06/09/2007, 05h07   #18
Salve Håkedal
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Better use of awk/sed?

On 2007-09-05, Ed Morton <morton@lsupcaemnt.com> wrote:
> 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"
>


Thank you. That does it!

--
Salve Håkedal
  Réponse avec citation
Vieux 06/09/2007, 05h30   #19
Salve Håkedal
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Better use of awk/sed?

On 2007-09-05, Glenn Jackman <glennj@ncf.ca> wrote:
> At 2007-09-05 02:06PM, "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='

> [...]
>> '
>> 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