|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
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! |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#13 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#14 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#15 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#16 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#17 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#18 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#19 |
|
Messages: n/a
Hébergeur: |
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 |